help me with replace text on the post via functions.php

Posted in: Newspaper
Post count: 27

Hi, can you help me with replace text on the post via functions.php

Example i have code like this
<img class="logo" src="https://domain.com/photo.jpg" style="with:100px" >

I tried to edit this code like this
<img src="https://img.domain.com/photo.jpg?w=696&quality=80&strip=all">

So i add this code to functions.php, but something was wrong! can you help please

function replace_text($text) {
$text = str_replace(‘<img(.*?)src=”(http://|https://)(.*?)(jpg|png|gif)(.*?)>’, ‘<img src=”https://img\1?w=696&quality=80&strip=all’, $text);
return $text;
}
add_filter(‘the_content’, ‘replace_text’);

  • This topic was modified 8 years by nhatkymua.
Post count: 22421

Hello,
The function you use is to replace strings of text, not html markup. Html is replaced differently.
Try this method instead: https://www.inboundhorizons.com/filter-replace-html-wordpress-content-loop/
We have not tested it though.

Thanks.

Post count: 27

Yes, its working, thanks you so much

Viewing 3 posts - 1 through 3 (of 3 total)
The forum ‘Newspaper’ is closed to new topics and replies.