WordPress 快速替换文章/评论的某些文字内容

如果你想替换某些文章或评论中的文字,比如敏感词和过期内容,或者给某些关键字添加链接,比如推广链接等,那么你将下面的代码添加到主题的 functions.php 即可:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
 * WordPress 快速替换文章/评论的某些文字内容
 * https://www.wpdaxue.com/replace-text-of-content-or-comment.html
 */
function wpdaxue_replace_text($text){
	$replace = array(
		// '原始文字' => '替换为这些'
		'WP大学' => 'WordPress大学',
		'阿里云' => '<a href="https://www.wpdaxue.com/go/aliyun">阿里云</a>',
		'' => '<a href="http://www.cmhello.com/"></a>'
		);
	$text = str_replace(array_keys($replace), $replace, $text);
	return $text;
}
add_filter('the_content', 'wpdaxue_replace_text'); //正文
add_filter('the_excerpt', 'wpdaxue_replace_text'); //摘要
add_filter('comment_text', 'wpdaxue_replace_text'); //评论

/**
* WordPress 快速替换文章/评论的某些文字内容
* https://www.wpdaxue.com/replace-text-of-content-or-comment.html
*/
function wpdaxue_replace_text($text){
$replace = array(
// ‘原始文字’ => ‘替换为这些’
‘WP大学’ => ‘WordPress大学’,
‘阿里云’ => ‘<a href="https://www.wpdaxue.com/go/aliyun">阿里云</a>’,
” => ‘<a href="http://www.cmhello.com/"></a>’
);
$text = str_replace(array_keys($replace), $replace, $text);
return $text;
}
add_filter(‘the_content’, ‘wpdaxue_replace_text’); //正文
add_filter(‘the_excerpt’, ‘wpdaxue_replace_text’); //摘要
add_filter(‘comment_text’, ‘wpdaxue_replace_text’); //评论

请根据自己的实际需求,修改要替换的内容,以及要应用的钩子。

如果你要永久替换某些文字,建议你试试 WordPress批量搜索和替换插件:Search & Replace

本人擅长Ai、Fw、Fl、Br、Ae、Pr、Id、Ps等软件的安装与卸载,精通CSS、JavaScript、PHP、ASP、C、C++、C#、Java、Ruby、Perl、Lisp、Python、Objective-C、ActionScript、Pascal等单词的拼写,熟悉Windows、Linux、OS X、Android、iOS、WP8等系统的开关机。

通过下面的方式来联系我们:

电邮:138762189@qq.com

联系QQ:点击这里给我发消息

官方站:www.tadke.com

※ ※ 联系请加我的企鹅号 ※※

※ ※技术支持请微信联系站长 ※※

Copyright © 2023 Tadke.com. 琼ICP备20000547号