Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

RegEx needed which doesnt replace text in -tags WordPress


The plugin "SEO Friendly Images" uses a regex which adds <strong>title="..."</strong> and <strong>alt="..."</strong> to all <img>-tags. This is causing my plugin "Leaflet Maps Marker" to break as I would need <strong>title=\"...\"</strong> and <strong>alt=\"...\"</strong>.

As there is no easy way for me to change this within my plugin, I would like to change the regular expression of "SEO Friendly Images", which is currently this one:

function seo_friendly_images($content) {
return preg_replace_callback('/<img[^>]+/', 'seo_friendly_images_process', $content);
add_filter('the_content', 'seo_friendly_images', 100);

I would like to change the regex above to be not executed if the <img>-tag is within <script>-tags (respectively <script language="...">, meaning <script*>)

How would the above code have to be modified in order to achieve this?
Thanks for your answers!

Answers (4)


Martin Pham answers:

Hey Robert,
please insert this into functions.php

if ( function_exists( 'seo_friendly_images_install' ) ) {
remove_filter('the_content', 'seo_friendly_images', 100);
function seo_friendly_images_fixed($content) {
$content = preg_replace_callback( '#<script(.*?)>(.*?)<\/script>#si', create_function( '$matches', 'return str_replace( array("<img", "<IMG"), "&lt;img", $matches[0]);'), $content );
$content = preg_replace_callback('/<img[^>]+/', 'seo_friendly_images_process', $content);
return str_replace( '&lt;img', '<img', $content);
add_filter('the_content', 'seo_friendly_images_fixed', 100);

Robert Harm comments:

your solution works - unfortunately only when adding to functions.php in my template file.
But I need to add it to my plugin file - if I do that, I get the following error:

Warning: call_user_func_array() expects parameter 1 to be a valid callback, array must have exactly two members in /home/htdocs/wp-current/wp-includes/plugin.php on line 170

Do you have any idea what has to be considered if added to a plugin?

Robert Harm comments:

I found a solution - just added the code to another subfunction...


Hai Bui answers:

I think it may be easier to change the code within your plugin. Can you post the code in your plugin where you have <img> tags?

Robert Harm comments:

thanks for the offer, but I need a changed regex as changing my code involves too many dependencies...


Plugarized answers:

Try the following RegEx




Robert Harm comments:

this may be the solution - what is the difference between these two versions?

Plugarized comments:

Please see the following link for positive and negative assertions also

Plugarized comments:

?= indicates a positive look ahead and ?! indicates a negative look ahead. Positive look ahead meaning "Foo is followed by Bar", Negative look ahead meaning "Foo is not followed by Bar".

Robert Harm comments:

unfortunately, your solution doesnt work.

I tried the following regex:

return preg_replace_callback('/<img[^>]+/', 'seo_friendly_images_process', $content);
return preg_replace_callback('/<img[^>]+(?!<script[^>]+>)/', 'seo_friendly_images_process', $content);
return preg_replace_callback('/<img[^>]+(?=<script[^>]+>)/', 'seo_friendly_images_process', $content);
return preg_replace_callback('/<script[^>]+>(?!<img[^>]+)/', 'seo_friendly_images_process', $content);
return preg_replace_callback('/<script[^>]+>(?=<img[^>]+)/', 'seo_friendly_images_process', $content);

To make it clear: <img> tags outside of <script*>-tags should be changed by the regex (which as I understood your proposed solution is not considered).

Can you please take a look again?


John Cotton answers:

Just a thought Robert....

Although the rogue plugin handles this function with one line of code, that does not necessarily mean you have to.

If you struggle to come up with a suitable regex to skip the changes within script tags, then how about - instead of returning the result of the preg_replace_callback - capture is and restore the \" within a secondary one.