Preprocess Comment Content in?WordPress

WBOY
リリース: 2016-06-06 20:13:07
オリジナル
1058 人が閲覧しました

Ive 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 dont need to worry about page refreshes. ?You can also post links to GitHub gists, CodeP

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!

The PHP

The <code>preprocess_comment hook allows us to get at the comment data before it is processed. ?Here is how I use this hook, wrapping?<code>`text` strings in <code><code> elements and encoding angle characters in <code><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"></pre><div class="contentsignin">ログイン後にコピー</div></div>?elements:

// Manage comment submissions
function preprocess_new_comment($commentdata) {
	// Replace `code` with code
	$commentdata['comment_content'] = preg_replace("/`(.*)`/Um", "$1", $commentdata['comment_content']);
	// Ensure that code inside pre's is allowed
	preg_match_all("/(.*)/", $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.

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!

Read the full article at: Preprocess Comment Content in WordPress

Treehouse
Wufoo

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のおすすめ
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!