I have several hundred posts which include a mix of single image and gallery blocks. All of them currently link to media file. What's the fastest way to change this to attachment page instead? (And back to media file, as an "undo")?
I found this but for some reason the answer selected has negative votes which makes me unsure about running it. It is also only for gallery blocks but not single image blocks. How to link Gutenberg Gallery images To Media file automatically?
I have several hundred posts which include a mix of single image and gallery blocks. All of them currently link to media file. What's the fastest way to change this to attachment page instead? (And back to media file, as an "undo")?
I found this but for some reason the answer selected has negative votes which makes me unsure about running it. It is also only for gallery blocks but not single image blocks. How to link Gutenberg Gallery images To Media file automatically?
Share Improve this question asked Jan 13, 2020 at 5:52 ArkuenArkuen 632 silver badges16 bronze badges 1- The answer you've linked to wouldn't do what you need. It creates a block template, so that whenever you add a new Post, it adds a Gallery block linked to media. It can't affect any existing Posts, and is specific to the Gallery block. – WebElaine Commented Jan 28, 2020 at 21:25
1 Answer
Reset to default 1Fastest way in my opinion would be to apply filter on the_content
with preg_replace_callback
to find media URLs and replace them with anything else, like attachment pages. Attachment ID is available as part of <img>
class value as well as wp:image
attribute in Gutenberg comment. So you just need some smart regexp to match each image block in content and replace it. No undo needed. However this is maybe not the best solution performance wise, as replacents will be done on each page request (unless caching is used).
Better solution would be to run some PHP code once (wp-cli command or otherwise) to loop over all posts and replace post_content value with replaced URLs. Save DB backup before this for undo or create reverse replacement function as well.