Rename existing old uploaded images containing accented French characters (not during upload)

Need to update 1,000+ images which have French characters in them.

The following worked for a test post:

  • Created a copy of the image without the FR characters
  • Updated file name inside the blog post’s body using wp_update_post (WP function)
  • Updated ‘attachment meta data’ using wp_update_attachment_metadata (WP function)
  • Updated ‘attached file’ using update_attached_file (WP function)

It looks like I can just skip the:

  • Updated ‘attachment meta data’ using wp_update_attachment_metadata (WP function)
  • Updated ‘attached file’ using update_attached_file (WP function)

and just change the post content so it references the new images using the following code:

// Array used to update the post
$my_post = array(
  'ID' => $post_ID,
  'post_content' => $content_with_updated_file_name
);

// Update post content using the array above
wp_update_post( $my_post, true );
if (is_wp_error($post_ID)) {
    $errors = $post_ID->get_error_messages();
    foreach ($errors as $error) {
        echo $error;
    }
}

Is that the right way or should I keep updating the ‘attachment meta data’ and ‘attached file’ for every image I’m renaming?

3 Answers
3

Well all depends und your use case – can the renaming be done in bulk using regex? then go and apply the same regex to the Database, by e.g using http://wp-cli.org/commands/search-replace/

If you need to do it manually maybe take a look at https://wordpress.org/plugins/media-file-renamer/

Good Luck!

Leave a Comment