PreprocessCommentContentin? WordPress

Source: Internet
Author: User
Iveputagreatamountofeffortintomakingsurethe? Commentsystemonthisblogisfast? Andfeature-filled .? ThecommentsystemisAJAX-basedsoyoudontneedtoworryaboutpagerefreshes .? YoucanalsopostlinkstoGitHubgists, CodeP

Ive put a great amount of effort into making sure? 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? 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? System .? But what if you want? Modify comment content before it is processed, and subsequently marked as SPAM or scrubbed? ? That's super easy with WordPress hooks!

The PHP

Thepreprocess_commentHook allows us to get at the comment data before it is processed .? Here is how I use this hook, wrapping?`text`Strings in elements and encoding angle characters in

?elements:

// Manage comment submissionsfunction 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 allowedpreg_match_all("/(.*)<\/pre>/", $commentdata['comment_content'], $pre_matches); // $2foreach($pre_matches as $match) {$immediate_match = str_replace(array('<', '>'), array('<', '>'), $match[2]);$commentdata['comment_content'] = str_replace($match[2], $immediate_match, $commentdata['comment_content']);}// Returnreturn $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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.