Occasionally I change the name of a media file in Wordpress, but the filename (see screenshot) does not change in the process. I see a handful of plugins online that purport to change this. I'm just wondering if there are other non-plugins methods to carry this out.

Is one (the only?) option to directly edit my database?


As Rarst has explained, you can't really directly edit your database, or you'll risk killing the serialized data.

Your options are thus to:

  • delete the file and re-upload it with its new name (obviously has the downside of having to reinsert it everywhere it was being used), or to
  • use one of the media renaming / replacing plugins, which through one method or another take care of updating all links to the file.

If your concern is having yet another plugin installed, you can always sit it there deactivated and just activate it when you need it. I use this for a variety of media "tool" plugins, because I only really need them once in a while.

  • thanks. That all sounds reasonable. I'm asking in-part because I like to minimize the number of plugins. But also these questions help me learn more about Wordpress inner workings, capabilities, etc. Do you have a plugin you might recommend to carry this out?
    – cag8f
    Jul 13 '16 at 17:23
  • @cag8f Sure, I use this one often. It's more about replacing media so isn't exactly what you're after but it does handle renames too.
    – Tim Malone
    Jul 13 '16 at 19:11
  • Everywhere I look for answers to WP problems, the answer is "you need plugin X". I was kind of hoping stackexchange might get to the next level, and actually answer technical questions, but oh well.
    – fortboise
    Sep 15 '16 at 13:56
  • @fortboise It would make a really long answer to go into all the technical aspects of something like this. SE generally works better with questions that can be answered in a few paragraphs (although there are exceptions). Having said that, there's plenty of non-plugin answers here - given plugin recommendations are off-topic, technical answers are our bread and butter! You're welcome to ask a question about this topic if you want to explore it further than this question does.
    – Tim Malone
    Sep 15 '16 at 19:32

WP stores considerable amount of attachment data in post meta and serialized form.

Raw data looks something like this:

_wp_attached_file 2016/04/golden-ragnaros.jpg

_wp_attachment_metadata a:5:{s:5:"width";i:488;s:6:"height";i:744;s:4:"file";s:27:"2016/04/golden-ragnaros.jpg";s:5:"sizes";a:2:{s:9:"thumbnail";a:4:{s:4:"file";s:27:"golden-ragnaros-150x150.jpg";s:5:"width";i:150;s:6:"height";i:150;s:9:"mime-type";s:10:"image/jpeg";}s:6:"medium";a:4:{s:4:"file";s:27:"golden-ragnaros-197x300.jpg";s:5:"width";i:197;s:6:"height";i:300;s:9:"mime-type";s:10:"image/jpeg";}}s:10:"image_meta";a:12:{s:8:"aperture";i:0;s:6:"credit";s:0:"";s:6:"camera";s:0:"";s:7:"caption";s:0:"";s:17:"created_timestamp";i:0;s:9:"copyright";s:0:"";s:12:"focal_length";i:0;s:3:"iso";i:0;s:13:"shutter_speed";i:0;s:5:"title";s:0:"";s:11:"orientation";i:0;s:8:"keywords";a:0:{}}}

This is extremely inconvenient to just edit by hand and it's trivial to break serialized data altogether if you change it directly.

That's essentially the point of dedicated tools for it existing. The operation is too unfriendly to easily perform by hand or even basic code.

  • Thanks, that all makes sense. Ragnaros image--are you running a WoW site???
    – cag8f
    Jul 13 '16 at 17:21
  • No, I just upload whatever lies around into my local dev install and I play too much Hearthstone. :)
    – Rarst
    Jul 13 '16 at 18:34
  • Ah right, Hearthstone. I'm a WoW player so I recognize Ragnaros.
    – cag8f
    Jul 14 '16 at 1:56

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.