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

Question

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?

0
, , , Robert Sinclair 10 months 0 Answers 123 views 0

Leave an answer