Overwrite image name when importing images for a friendly URL

Closed

Import WP Addon - Advanced Custom Fields Updated 8 months ago 10 Replies

Laurent asked 8 months ago on April 18, 2024 at 1:27 pm

Hello,

I’m using “Attachment” as field type (image), and need to overwrite the URL of my imported images, in the same way that we can apply alt or caption text from a custom field (the custom post title for example).
As you can imagine, I don’t want to have to manually change the names of the image files before import.

Example :

Instead of having an image URL like :

https://mywbsite/wp-content/uploads/2024/04/jks5zr5rt-unfriendly-url-qsd5jq5dsj.jpeg

We would have :

https://mywbsite/wp-content/uploads/2024/04/custom-post-clean-and-friendly-url.jpeg

Would you see a way, if not possible with the plugin, to do that ?

Thank you,

Regards,

L

LC

Laurent replied privately

8 months ago on April 18, 2024 at 1:47 pm

J

James replied Support Agent

8 months ago on April 18, 2024 at 3:16 pm

Hi Laurent,

This is not possible out of the box, however it should be possible with few lines of code.

To help me figure out the best solution, how many images are being imported per record? as we need somewhere to store the custom image slugs to transform the image name, possibly in a custom field, if there are multiple images we would need some way to connect them.

This would also require that you have not previosuly imported the images as it would not be able to update names of existing image that are already in the media library.

James

LC

Laurent replied

8 months ago on April 18, 2024 at 4:07 pm

Hi James,

Ok, so here's my classic use case & settings :

Image URL
- Imported image URL already existing : Will never happen
- Renamed image URL already existing in media library : Possible. It won't be a problem if a suffix is added to the new imported ones' urls ('-1', then '-2'... for example).

Permissions
- only 1 image per record (and maximum of 20 records per import)
- unique identifier : a (custom title) template field
- Allowed creation and Update of new records when no unique identifer match has been found, for All fields
- Deletion not allowed

Let me know if you need more details, thanks,

Regards,
L

J

James replied Support Agent

8 months ago on April 19, 2024 at 7:28 pm

Hi Laurent,

I have attached a custom plugin that will take the value from a custom field with the name "_custom_attachment_filename", and replace any attachments with that name , this custom name should not include the file extension.

Extra screenshots are also included to show the full setup and results.

James

LC

Laurent replied

8 months ago on April 19, 2024 at 7:53 pm

Hello James, thanks a lot. I'll test it fully and come back to you.
At first sight on the attachments I cannot be sure of all the process, but I'll tell you how I manage it.
Have a great week-end.
L

LC

Laurent replied

8 months ago on April 20, 2024 at 10:55 am

Hello James,I've tested, and it works fine !But, as shown in your screenshot, when "Field Type" = 'Text' only.And in this case, we loose special settings related to an 'Attachment' :- Is featured- Enable Meta : Alt, Title, Caption and Description textI've tried by putting "Field Type" = 'Attachment' and let previous these settings that worked fine before this new plugin, and in this case, the image is not imported at all.To complete :In the "old" screenshot : "Images & Attachments" is empty.And in the "new" screeshots : "Images & Attachments" > "Location" is valued with the imported image URL identifier (as shown in your screenshot).Do you think you could see how to maintain "is featured" and "Meta fields" which have important value ? Or maybe it is already possible, and I have not applied the good settings ?Thank you,L
J

James replied Support Agent

8 months ago on April 20, 2024 at 11:14 am

Hi Laurent,

The _custom_attachment_filename custom field of type text is to be used along side how you were previously importing attachments (not to replace it).

As you can see from the previously attached screenshot (re-attached), the attachment is being imported via the "Images & attachments" section, and the new field is just to set the filename and nothing else.

James

LC

Laurent replied

8 months ago on April 21, 2024 at 3:10 pm

Hi James,

Previously I was importing attachments without "Images & Attachments" section, but just by using a "Custom field" (Name = 'showimg' ; Value = {2} ; Field Type = 'Attachment' : Enable Meta = 'Yes' > Alt-Title-Caption-Description Text = {4}).
With this method, it worked perfectly, except for the URL customization, the reason why I've created this ticket.

Now, by trying with this additional custom plugin, it worked fine, but I've lost the customization of "Meta" > "Alt-Title-Caption-Description Text" (as the field type is "Text").

Then, you say me :

The _custom_attachment_filename custom field of type text is to be used along side how you were previously importing attachments (not to replace it).

=> So I keep what I did until now (described above), but I guess that I have to swap 'showimg' with 'spectacle', since I've replaced '_custom_attachment_filename custom' in the additional plugin code by 'spectacle'.
=> I do it right ?
=> Nevertheless I do not see how to allow customization of "Meta" > "Alt-Title-Caption-Description Text" if the field type has to remain "Text". Do you see my point ?

As you can see from the previously attached screenshot (re-attached), the attachment is being imported via the "Images & attachments" section, and the new field is just to set the filename and nothing else.

=> Yes, as said in my previous message, that's what I've done : ""Images & Attachments" > "Location" is valued with the imported image URL identifier : {2}".

I hope my explaination are clearer, I can detail again if necessary.
As you have all the parameters and values I need to use, if you prefer, you can tell me what I have to put in each field.

Thanks a lot,
L

J

James replied Support Agent

8 months ago on April 21, 2024 at 5:58 pm

Hi,

I have attached a newer version of the custom code, as this now works with either attachments imported via custom fields or the attachment and images section.

Sorry, based on your previous response i think i have not been clear enough with the proposed solution.

Step 1:

Create the custom field that you previously had working to import an attachment ( but just by using a "Custom field" (Name = 'showimg' ; Value = {2} ; Field Type = 'Attachment' : Enable Meta = 'Yes' > Alt-Title-Caption-Description Text = {4}).), then test that the importer is importing the attachment correctly by importing a single record, ignoring that the imported attachment has the unmodified filename as we want to get the attachment imported.

If successful, delete the file from the media library.

Step 2: 

Keep the existing configuration and create an extra custom field with the name "_custom_attachment_filename", and the value of the filename you would like to overwrite the imported attachment with.

Now when running the import the attachment should be imported with the filename entered in the "_custom_attachment_filename" value.

James

LC

Laurent replied

8 months ago on April 22, 2024 at 12:14 pm

It works perfectly, bravo !
Thanks a lot
L