Rename Media

download

Download: Rename Media
Version: 0.1.2
Supports: WordPress 2.9 – 3.1.1
Other: WordPress.org | SVN

Enhances the WordPress media library by allowing you to rename media files.

All resized thumbnails are also renamed.

Why would you use this plugin?

You upload an image from your digital camera with a filename IMG_0112.jpg.

Not a great name really.

Afterwards you change the title of the image in WordPress to ‘Sunset With Banana’.

Much more interesting, but the filename of the image is still IMG_0112.jpg.

Using this plugin the filename will also change to sunset-with-banana.jpg, which as well as looking nicer is a lot better for SEO.

Installation

Installation is like any WordPress plugin:

  • Download rename-media.zip
  • Unzip
  • Upload rename-media directory to /wp-content/plugins on your server
  • Activate the plugin

You can find full details of installing a plugin on the plugin installation page.

How does it work?

It is very simple to use the plugin. When editing a media item just change the title of the item and the filename and all associated thumbnails will be renamed. That’s it!

Caveats

The plugin will not update any direct references to a media item that you may have – you will need to change these yourself.

If you use a WordPress gallery then the images and attachment pages will automatically update for you.

If you use a caching plugin such as WP Super Cache then you may need to clear your cache after changing a media name.

Please be aware that your web server will require correct write permissions to your upload directory. If you can upload files then this is likely the case, but the plugin will silently fail otherwise.

Support & Bugs

Support and feature requests should be made through the Rename Media support forum. You can contact me directly but I spend a very limited amount of time on direct support and you stand a better chance of being answered through the forum.

20 comments

  1. Does it save the original name anywhere (post meta)? If not, that would be a useful addition. You might even be able to auto-filter references to the old filename to the new one on the fly.

    1. No, but that’s a good suggestion. I was planning on adding some auto-redirect feature, but decided to release it first and then look into that afterwards.

  2. I use WordPress (3.03) on a Windows Server (IIS), and I had 2 issues with your plugin (because I’m using Windows). I fixed them by changing the lines 2 and 3 of your plugin (I’m lucky, it was easy) and by adding a little function.

    1. The file was renamed wrongly by PHP and therefore all the links were killed. Using the UNIX directory separator did the trick:
    $new = dirname( $old ) . '/' . strtolower( $new ); // (line 2)

    2. The title of my images sometimes contain apostrophes. This wasn’t modified by the wp_unique_filename() unfortunately, so I inserted a sanitize function there:
    $new = wp_unique_filename(dirname($old), sanitize($attachment['post_title'], true) . '.' . $ext); // (line 3)

    Here is the function:

    function sanitize($string = '', $is_filename = FALSE)
    {
    $string = preg_replace('/[^\w\-'. ($is_filename ? '~_\.' : ''). ']+/u', '-', $string);
    return mb_strtolower(preg_replace('/--+/u', '-', $string), 'UTF-8');
    }

    I never worked on a wordpress plugin, so I’m not sure that what I did is the best solution. But it works 🙂

  3. Great idea for a plug! I got a strange error though:

    Warning: Invalid argument supplied for foreach() in /home/propuls/public_html/wp-content/plugins/rename-media/rename-media.php on line 52 Warning: Cannot modify header information - headers already sent by (output started at /home/propuls/public_html/wp-content/plugins/rename-media/rename-media.php:52) in /home/propuls/public_html/wp-includes/pluggable.php on line 890

  4. So weird. It does actually rename the file on the server, but then
    I get the above error, and the “File Name” on the Edit Media screen
    changes to “?attachment_id=” followed by it’s ID number… Any
    ideas?

  5. I truly love the concept. I installed it and it did exactly as expected, so thank you. I just have one slight problem which may or may not be related. As the image is linked to a larger version of itself, the HREF goes to the original filename, not the new filename. Since others don’t seem to notice, it may be interference by other plug-ins. I haven’t tested it yet, but I simply changed the rename($old,$new) to copy($old,$new) so hopefully that will do it.

    1. Yep, the plugin won’t change any HREFs you have – this is something you’ll need to change manually. If you do a copy() then you’ll end up with duplicate images with different names.

      1. How much work would it be to have the plugin trigger Redirection’s Modified Posts so that a 301 is automatically created for the changed image name?

  6. Hi

    Great idea, when it works it’s ok but as it’s still in beta it’s risky.
    It doesn’t always rename all photos I upload together and just today it renamed my uploads folder to the name it was supposed to rename the uploads.

    At first I thought all my photos were deleted as everything on my site disappeared. After restoring my database from backup and uploading my uploads folder I noticed another folder with the name of the post I was working on when everything went bad. After looking inside I realised it was my uploads folder and it was renamed by this plug in.

    This happened after I uploaded five and deleted the file that didn’t get renamed in the bunch that I last uploaded. It was the first file that didn’t get renamed, I guess it renamed my uploads folder instead.

    Hope this helps someone to find the bug if it is one. Or perhaps this can help someone else in this situation.

    1. The plugin shouldn’t rename folders, just files. If you have any more details then I can look into it, but at the moment there’s not much to go on!

      1. Hi John,

        I just experienced the same problem yesterday.

        In my WP network install, the blog in question typically has it’s files at
        …/wp-content/blogs.dir/8/files

        Now suddenly there was an additional folder named:
        …/wp-content/blogs.dir/8/dscn1328_2_2/

        And all files went there!
        See
        …/wp-content/blogs.dir/8]$ du -chsx *
        151M dscn1328_2_2
        904K files
        152M total

        I deleted the folder “files” and renamed the “dscn1328_2_2” to “files”.

        Now the images are back.

        It was unnecessary to use a backup.

        I hope this helps debugging!
        Dice

  7. I had a problem caused by multiple thumbnails having the same size. This is a quirk of some themes and needs to be accounted for in your renaming loop. Since all the files have the same name they appear in your loop multiple times and only the first succeeds.

    The fix is to add an if statement that checks that the old name exists before trying to change the name of a thumbnail image. Here’s the corrected loop:

    foreach ( (array)$meta[‘sizes’] AS $size => $meta_size ) {
    $old_file = dirname( $old ).DIRECTORY_SEPARATOR.$meta[‘sizes’][$size][‘file’];

    $meta[‘sizes’][$size][‘file’] = str_replace( $old_filename, $new_filename, $meta[‘sizes’][$size][‘file’] );

    $new_file = dirname( $old ).DIRECTORY_SEPARATOR.$meta[‘sizes’][$size][‘file’];
    if ( file_exists( $old_file ) === true ) // Be sure old file hasn’t already been renamed.
    rename( $old_file, $new_file );
    }

    Thanks for a very handy plugin.

    -Stu

  8. I have recently istalled this plugin and it worked on 2 of my images but when i tried to us it the images disappeared on my site and just left a ? I tried again and then images that I had not changed the filenames of disappeared as well. They are in the same section on my site. Not sure what to do now as I cant get them back, any ideas??

Leave a Reply to Tárhely Cancel reply

Your email address will not be published. Required fields are marked *