某些时候,用工具采集得到的文章会带上源网站的一些代码,比如版权信息或者样式,如果需要将这些代码去掉,一个一个来太麻烦,想要一劳永逸的解决这个问题,我们就需要改造主题文件夹下的functions.php文件了。代码如下:
/*批量替换文章中的代码或字段*/
add_filter('the_content', 'replace_text_wps');
add_filter('the_excerpt', 'replace_text_wps');
function replace_text_wps($text){
$replace = array(
// 'WORD TO REPLACE' => 'REPLACE WORD WITH THIS'
'style="max-width:600px;"' => '', //将这个样式替换为空
'wordpress"' => '<a href="http://www.wordpress.com">wordpress</a>', //将这个文本统一加上超链接
);
$text = str_replace(array_keys($replace), $replace, $text);
return $text;
}
演示代码的意思是将 max-width:600px; 替换为空。
现在到前台刷新网页,所有不必要的信息或希望改造的信息都已经生效。
高级一点的带条件判断的代码见另一篇教程:http://www.redren.net/7741.html