WordPress.org

Ready to get started?Download WordPress

Forums

Auto renaming of featured image filenames in WordPress posts..? (1 post)

  1. popokolok
    Member
    Posted 1 year ago #

    Please tell me, - is there a code I can use to automatically copy the title of a post and rename the filename of that post's featured image according to that title, putting dashes instead of spaces, removing special characters, and converting to lowercase, - basically the usual stuff that WordPress does to post names automatically... And all that at the moment that post is published..?

    I got this answer on StackOverflow, but am not sure exactly as to how to implement it...

    Hook into save_post to get the event when a post is saved
    add_action( 'save_post', 'update_file_image_name' );
    Then you can use the function update_file_image_name

    This will pass you the ID of the post in question
    function update_file_image_name($post_id){}
    Use
    wp_get_attachment_url($post_id);
    To get the full path to the image. From here, you're just going to use that url by splitting and taking what you need to walk the directory, find the file, sanitize the string as you'd mentioned, and then return out when done.

    Thank you in advance!

Topic Closed

This topic has been closed to new replies.

About this Topic