Support

Account

Home Forums Backend Issues (wp-admin) Rename media on post save

Solving

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

    • First you would need to make sure that the other fields are completed before an image can be added, and potentially that the fields cannot be altered.
    • You will need to add custom JS and use the ACF JS API using an undocumented hook in the API “prepare_for_ajax” data = acf.applyFilters('prepare_for_ajax', data);
    • Using a filter you would have to get the values of the other fields and add them to this ajax data.
    • This data will then be available in $_POST during the file upload process and then you can use these values in the processed described in some of those posts.

    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.

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.