I have a wordpress website and the images that come from the the_content()
function have the <img>
tag, however for the AMP pages they should be written like this: <amp-img>
. What's the correct way of doing that in WP?
Try with this:
add_filter('the_content', 'change_img', 99);
function change_img( $content )
{
return str_replace('<img', '<amp-img', $content);
}
I would build a php function which replaces "img" with "amp-img".
<?php
function parseTagsIntoAmpTagsFromString($string)
{
$search = ["<img", "</img>"];
$replace = ["<amp-img", "</amp-img>"];
return str_replace($search, $replace, $string);
}
?>
As the code above shows, you will need to match starting and closing tags. The replacement is done on the found keys in $search and $replace.