记录一些常用的函数和瞎折腾的过程……

用函数实现替换wordpress中的字符

提醒:本文最后更新于 2372 天前,文中所描述的信息可能已发生改变,请谨慎使用。

对于wordpress文章中出现的一些文字或是关键字,我们想替换成其它的,或是给它加上超链接和颜色之类的,千万不要想着去整数据库,那样太麻烦了,写一个替换函数,再通过对应在勾子,就能轻松实现。

//替换字符
function to_replace_text($text){
	$replace = array(
		// '替换前的' => '替换后的'
		'hello' => '<a href="https://www.psay.cn">hello</a>',
		);
	$text = str_replace(array_keys($replace), $replace, $text);
	return $text;
}
add_filter('the_content', 'to_replace_text'); //正文替换
//add_filter('the_excerpt', 'to_replace_text'); //摘要替换
//add_filter('comment_text', 'to_replace_text'); //评论替换