wordpress 预缓存_在WordPress中预处理评论内容

wordpress 预缓存

I've put a great amount of effort into making sure the comment system on this blog is fast and feature-filled.  The comment system is AJAX-based so you don't need to worry about page refreshes.  You can also post links to GitHub gists, CodePen pens, and JSFiddle fiddles and see them rendered within the comment.  Those tasks I accomplish after a comment has been registered in the system.  But what if you want to modify comment content before it is processed, and subsequently marked as SPAM or scrubbed?  That's super easy with WordPress hooks!

为了确保此博客上的评论系统快速且功能丰富,我付出了很多努力。 评论系统是基于AJAX的,因此您无需担心页面刷新。 您还可以发布指向GitHub要点,CodePen笔和JSFiddle小提琴的链接,并在评论中看到它们的呈现。 我在评论中注册了系统后完成的那些任务。 但是,如果要在处理注释内容之前先修改注释内容,然后将其标记为垃圾邮件或清理,该怎么办? 使用WordPress挂钩非常简单!

PHP (The PHP)

The preprocess_comment hook allows us to get at the comment data before it is processed.  Here is how I use this hook, wrapping `text` strings in <code> elements and encoding angle characters in <pre> elements:

preprocess_comment挂钩允许我们在处理注释数据之前获取注释数据。 这是我使用此钩子的方式,将`text`字符串包装在<code>元素中,并在<pre>元素中编码角度字符:

// Manage comment submissions
function preprocess_new_comment($commentdata) {
	// Replace `code` with <code>code</code>
	$commentdata['comment_content'] = preg_replace("/`(.*)`/Um", "<code>$1</code>", $commentdata['comment_content']);

	// Ensure that code inside pre's is allowed
	preg_match_all("/<pre(.*?)>(.*)<\/pre>/", $commentdata['comment_content'], $pre_matches); // $2
	foreach($pre_matches as $match) {
		$immediate_match = str_replace(array('<', '>'), array('<', '>'), $match[2]);
		$commentdata['comment_content'] = str_replace($match[2], $immediate_match, $commentdata['comment_content']);
	}

	// Return
	return $commentdata;
}
add_action('preprocess_comment', 'preprocess_new_comment');


This snippet should be added to functions.php, as you would expect of a WordPress theme enhancement.

正如您期望的WordPress主题增强功能一样,此代码段应添加到functions.php中。

I love the WordPress hook system -- it makes the CMS incredibly powerful and customizable.  I also use this hook to prevent WordPress comment SPAM.  And since many users place HTML code in my comments, it's important I encode those angle characters properly.  In the end, you never know what your user will submit and what each site will accept -- use this WordPress hook to take control!

我喜欢WordPress挂钩系统-它使CMS变得异常强大且可自定义。 我还使用此钩子来防止WordPress评论SPAM 。 并且由于许多用户在我的注释中放置了HTML代码,因此正确编码这些角度字符非常重要。 最后,您永远不知道您的用户将提交什么以及每个站点将接受什么-使用此WordPress挂钩进行控制!

翻译自: https://davidwalsh.name/modify-comment-content-wordpress

wordpress 预缓存

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值