Wordpress过滤器 - 将“src”替换为“load-src”

时间:2017-04-14 22:29:50

标签: php regex wordpress

我正在使用简单的Wordpress延迟加载插件。

我需要使用"src"更改"load-src"属性,这样我就可以在需要时使用jQuery加载图片。我知道我应该使用过滤器。

add_filter('the_content', 'filter');
function filter($content) {
    return preg_replace_callback('/(<\s*img[^>]+)(src\s*=\s*"[^"]+")([^>]+>)/i', 'noidea', $content);
}

你能帮助我使用正则表达式,我真的不明白吗?

1 个答案:

答案 0 :(得分:2)

忘记正则表达式,只需使用str_replace

add_filter('the_content', 'filter');
function filter($content) {
    return str_replace('src="', 'load-src="', $content);
}
相关问题