2

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?

2

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!

2

I presume you used a script to rename 1000+ images, so you could probably apply your same rules to a MySQL update query on the wp_postmeta and wp_posts tables using a REGEX or a series of REPLACE() statements.

You can do this with a SQL GUI tool, in phpMyAdmin or using WP CLI

Saves messing about with Wordpress functions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.