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.

2

An other solution can be use this plugin: Phoenix Media Rename (Warning, it's not working with wpml)

This plugin do not allow to make a bulk action to slugify all files.

But there is a trick that you can do:

  • After installing and activating the plugin, go to the media library
  • Select the display in "list" mode and display 999 items per pages
  • Open the javascript console
  • Paste a "slugify" function in the console for example the contents of this file (slugifyjs) 5) Then execute a jQuery function to modify all input content of phoenix media rename
jQuery('.phoenix-media-rename input').each(function(){
    let filename = $(this).val();
    filename = slugify(currentVal).toLowerCase()
    $(this).val(filename)
})
  • Finally check all checkbox and select "rename" in bulk action dropdown and click on apply
  • Wait for all media to display a checkmark

note: it is possible that this operation returns an error of type "PHP Fatal error: Allowed memory size...". If this is the case try to see with your web host if it is possible to temporarily increase the memory allocated to PHP.

1
  • You saved me! I needed to remove whitespace from many uploaded pictures and found myself in veru desperate situation... – Ján Janočko May 6 at 15:20

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.