Updating References & Links

I am currently editing this page based on users’ requests and questions, sorry if it is a bit messy, I will make it clearer and clearer.

There are many themes and plugins available. Some of them use images, created their own meta hidden somewhere in your database or at some uncanny locations. Unfortunately, Media File Renamer cannot guess all those. If you rename a file, one of those references might get broken.

Recommendations

Before using the plugin, backup. This is the most obvious issue you could encounter when using a plugin that alters your files or database. Well, you should have made a backup before so restore broken. If you don’t understand how technically they could get broken, the answer is in the section below “Using The Renamer’s Actions & Filters”.

Timeouts, Big Installs, Slow servers

If you have a big install and/or a slow server, then you might experience timeouts. The plugin runs 2 to 4 queries on your database. They basically replace the old filename with the new filename.

Here are a few workarounds:

  • Disable Updating References in the plugin’s options
  • Increase the time before it timeouts (Increase limits)
  • Optimize the SQL queries specifically for your install (that should be done by a good developer around you); have a look at the action_update_posts and action_update_postmeta functions in the plugin
  • Any other idea? Please let a comment 🙂

I have been thinking of an alternative that could work: making the renaming asynchronous and renaming the files in the posts, parsing them one by one. Of course that would be a bad alternative for those with no such issues, and the plugin would be more painful to use. Unfortunately, there are no other alternatives.

Maybe it is not broken: The Cache Issue

WordPress installs are very cached nowadays. By plugins, by hosting services, CDN, etc. After renaming your files, you need to clear your cache. Not only your browser cache, but also your hosting service’s cache. And if you are using plugins to make your website faster, they will also have an option to reset your cache.

Sometimes the cache is cleared automatically, and you might know that. But for example, a plugin like WP Rocket (that does cache) doesn’t react when the meta for a post is updated, it does only clear its cache when the post itself is updated. So this is important: don’t believe the caching plugins are perfect, and clear your cache regularly.

Repair your WordPress

If have no backup, then there are two things you can do:

  • Un-Rename those files (= undo). There is an Undo option in the Advanced Settings of the plugin. Enable it and visit your Media Library. In the Rename column, you will notice a red arrow to come back, to undo. Click on it. You can bulk Undo in the Renamer dashboard.
  • Install the Regenerate Thumbnails plugin and run it on those images (or all your images).

Using The Renamer’s Actions & Filters

If you know how to update the references, you can use the “mfrh_url_renamed” (if you want to handle the new URL for this filename) or the “mfrh_media_renamed” (if you want to handle the filename on your server).

For instance, here is the code to update the post meta. This is actually already part of the plugin.

add_action( 'mfrh_url_renamed', 'action_update_postmeta', 10, 3 );
 
function action_update_postmeta( $post, $orig_image_url, $new_image_url ) {
  global $wpdb;
  $query = $wpdb->prepare( "UPDATE $wpdb->postmeta SET meta_value = '%s' WHERE meta_key <> '_original_filename' AND (TRIM(meta_value) = '%s' OR TRIM(meta_value) = '%s');", $new_image_url, $orig_image_url, str_replace( ' ', '%20', $orig_image_url ) );
  $wpdb->query( $query );
}

I recommend you to use WPEngine or SiteGround. They are both excellent hosting services and work perfectly with all my plugins.