Home › Forums › Backend Issues (wp-admin) › Rename media on post save
Hello,
I have 2 “image fields” and I would like to be able to automatically rename the image files when I save the post with the values of 2 other fields.
Is it possible to do?
If so, what would be the safest way to do this?
Thanks
This topic has come up several times here.
[SEARCH RESULTS]
Thx @John Huebner but that about renaming during Upload.
I’m looking to rename during saving the post.
All images are yet uploaded.
You cannot rename during the saving of the post. ACF uses the WP native upload. Files and images are uploaded and named when they are added.
I do not have any examples of doing what you want but here is an outline of what would need to be done in ACF to rename the file on upload in ACF
data = acf.applyFilters('prepare_for_ajax', data);
I honestly don’t know how you would rename the files in WP each time the other fields are changed and the post is updated. I have looked and I can’t find much in the way of examples of doing this. If I had to do this I would probably look at the code in other plugins that make it possible to rename a file after it is uploaded and see how they do it and adapt that for use in an acf/save_post action.
This looks somewhat promising https://stackoverflow.com/questions/64990515/wordpress-rename-attachment-file-with-working-thumbnails#answer-65065944
I think I expressed myself badly. 🙂
I never will upload an image during the post creation. All images are uploaded before the post is created.
The Images fields are just used to select images already uploaded.
ACF will never have to manage the addition of images.
I understand why you are trying to rename an image when selected on a post?
Do you want to rename the same image every time it is selected for each post it is selected on?
I have 2 images fields, Front and Back.
I have a Title field and a Artist field.
I have a custom specific and unique ID field
I’m looking to rename the front and the back like this
Artist_Title_Front_ID.webp
Artist_Title_Back_ID.webp
Each image will only be use in one custom post type.
I will have more than 70,000 images to manage so I would like to take advantage of the creation of the post to modify the names of the images in order to better classify them in the Media folder.
I would suggest starting with the link I provided above or look for how to rename files from PHP after they have been uploaded in WP. The only place where ACF is involved is doing whatever needs to be done with an acf/save_post action.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.