Allow renaming of file names in Media Library

Users should be able to rename files as needed inside Media Library easily.


Read-only archive: https://petitions.classicpress.net/posts/153/allow-renaming-of-file-names-in-media-library

Author: Viktor Nagornyy

Vote count: 14

Status: open


Comments

I think there is some confusion here.

You can rename the File Name perfectly fine (if we assume the file name to be the same as a post name, which is the SLUG)

What you cannot do, is rename the URL of the file.
And that has its reasons, it is used potentially throughout hundreds of thousands of posts, featured images, custom fields, etc.
This is like the GUID, and GUIDs are never edited, not even when you migrate a website (usually).

Also it represents a physical file location on a server, not like a Post Name does represent a rewritten link (and the real link in fact is a ?var=val that gets a content from the database, not from the file system)

The very least that needs to happen if we allow renaming the files is a big warning that now all the files references will break.
That is, I assume, the reason they chose to not allow that, because it forces you to upload a new one and thus, also to edit all references to that file (which as said cannot just be in post body, they can be in featured image, sliders, custom fields, etc etc etc, potentially, on some sites I have helped build, we speak about half a million posts, each with its featured image etc)
The query to update those instances is irrational, so it couldn’t be done on the fly

Creating a more dynamic system where instead of URLs you call attachments somehow else, would require a whole new feature, that possibly wouldn’t work with existing image fields, featured image, upload mechanisms implemented into plugins…
Even if using image IDs, that wont work, because IDs can and do change all the time (for example if exporting images into a new website using a import/export tool, the ID will change, not so the actual slug/permalink of the image)
All code inside a post content referring to ID and imported in parallel with said image, would then fail.

That is why you should never use IDs to get things from the database, even if it is thought so by mainstream it is wrong. What really never changes on export/import and is only changed by users, is the slug of an item (post name, slug, it has many names)

Thus… I am not sure we should allow this. Probably, not?

2 Likes

I think the code to handle this already exists in the Regenerate Thumbnails plugin. I haven’t read the code, but it does what would be needed for a rename. It might not handle all possible places that an image can be referenced, such as options or post meta (outside of the normal image post meta).

2 Likes

So why isn’t the slug the primary key?

Because the ID auto incrementative? It’s short? It is easier and more performs than to query by short numeric values than long strings (thinking of get meta where id is, etc etc etc)

The fact that the ID is primary is however not determining that it’s the best to use in all cases
As a matter of facts a slug also is unique just like an ID when created
Thus it truly could be used both as a the primary key

But slugs don’t change in migration process, IDs do, because auto increments and thus start at 1.
Unless of course you replace the database 1:1, but that’s not what happens usually

This is my petition, so I’ll add a few more points.

There’s a lot of things in CP that we allow users to do that will break their entire website or cause many problems already. For example, updating site/home URLs on the settings page, file editor enabled by default to edit theme/plugin code, changing permalinks on an existing post/page will result in 404 using old URL, etc.

Giving users the ability to change file names isn’t much different and technically safer. You can’t break a website with a broken image. A warning should be enough that changing the file name will not change it for all posts/pages where this file is used. It has to be done manually. There’s a plugin that does this.

2 Likes

Changing a permalink shouldn’t give you a 404, since the old slug is redirected to the new one by core.
We’ve mentioned two plugins that do it.
If it were in core, I would think it would be a function with a new action like ‘media_file_renamed’, giving the old and the new names, so that plugins could hook in and update whatever they need to update in addition to fixing the attachment meta data and find/replace in content.

1 Like

This doesn’t mean we should add more!

If this is implemented then it should include a system to find and fix references to renamed images in posts/pages/etc (with a warning that this replacement can never be perfect due to aforementioned “other places that an image can be referenced”).

Due to the complexity involved (especially if there is a large number of posts), this should be attempted and polished as a plugin first.

1 Like