since a while I am looking for a way to

  1. add a "link to" option to the media library in the backend (this is working already - using the filter 'attachment_fields_to_edit')
  2. if there is a link defined for a media file, add the link MEDIA FILE around the media.

I found this post, but that doesn't really help. How to add a link to media file for every image inserted in content?

The filter "wp_get_attachment_image_src" also didn't work (yet?).

And I am stuck with it.

The extension Gallery custom links does what I need (https://de.wordpress.org/plugins/gallery-custom-links/), but is using jquery and I would like to get rid of it.

Anyone an idea? Thanks a lot!

  • that's not how links worked, they aren't connected. To do what you want to do those images would need to be embedded as reusable blocks so that you could modify the reusable block and have every instance of it updated at the same time. If you were to build a new feature to do what you were hoping for it would only work on small sites as it requires a full search replace of all content to implement which can't be done on a lot of sites within the time PHP gives you. Filters will only help you with images displayed dynamically, but not for post content or existing content
    – Tom J Nowell
    Oct 12, 2022 at 14:18
  • But the way I do it, they would be connected cause they are related to the media files? Maybe I don't understand it right (or I didn't explain what I want to do) but when the images are loaded, I "just" need to put an <a> tag around it. In the plugin I was talking about it is working somehow, I just can't figure out how. :-/
    – Waldgeist
    Oct 14, 2022 at 10:55
  • when you insert an image into a post, the link is generated there and then and saved as text/HTML, it isn't dynamically pulled from the attachment/media post when the article is displayed. So if you insert an image and link it to the attachment, then go to that attachment and change its URL, it won't go back and update it as there's no link between the two. Much in the same way if you created a link in text to example.com, it won't update the URL if that site changes to a different URL, there's no ongoing connection, and there's no field in the attachment specifying what it should be
    – Tom J Nowell
    Oct 15, 2022 at 14:18
  • What the gallery custom links plugin you shared lets you do is it gives you an additional option for where to link a gallery item to when you create that gallery, but it doesn't auto-update content, and galleries are generated at runtime so they're not the same as other images. Even then, it's the gallery block/shortcode that determines the link, not the media/media library, so you'd still have to go into every article with a gallery and update the URL if you wanted to change them. The important part is that you specified wanting to change the media and it updating everywhere
    – Tom J Nowell
    Oct 15, 2022 at 14:20
  • Thanks @TomJNowell , now I get what you mean. Let's say it would not be a problem, if the links change after inserting the block - how would you solve that? For me it is more important, that the link is always the same (instead of changing the links of media files). So there won't be any changes.
    – Waldgeist
    Oct 18, 2022 at 10:52


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.