首页 > php教程 > php手册 > 正文

Preprocess Comment Content in?WordPress

php中文网
发布: 2016-06-06 20:13:07
原创
1263人浏览过

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;">&lt;/code&gt;?elements:&lt;/p&gt; &lt;pre class=&quot;brush:php;toolbar:false;&quot;&gt;// Manage comment submissions function preprocess_new_comment($commentdata) { // Replace `code` with code $commentdata['comment_content'] = preg_replace(&quot;/`(.*)`/Um&quot;, &quot;$1&quot;, $commentdata['comment_content']); // Ensure that code inside pre's is allowed preg_match_all(&quot;/(.*)&lt;\/pre&gt;/&quot;, $commentdata['comment_content'], $pre_matches); // $2 foreach($pre_matches as $match) { $immediate_match = str_replace(array('&lt;', '&gt;'), array('&lt;', '&gt;'), $match[2]); $commentdata['comment_content'] = str_replace($match[2], $immediate_match, $commentdata['comment_content']); } // Return return $commentdata; } add_action('preprocess_comment', 'preprocess_new_comment'); </pre>

登录后复制
</div> <p>This snippet?should be added to functions.php, as you would expect of a WordPress theme enhancement.</p> <p>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!</p> <p></p> <div class="aritcle_card"> <a class="aritcle_card_img" href="/ai/2373"> <img src="https://img.php.cn/upload/ai_manual/001/246/273/176239923777053.png" alt="论小文"> </a> <div class="aritcle_card_info"> <a href="/ai/2373">论小文</a> <p>可靠的论文写作助手,包含11种学术写作类型,万字论文一键生成,可降重降AIGC,参考文献真实可标注,图表代码均可自定义添加。</p> <div class=""> <img src="/static/images/card_xiazai.png" alt="论小文"> <span>431</span> </div> </div> <a href="/ai/2373" class="aritcle_card_btn"> <span>查看详情</span> <img src="/static/images/cardxiayige-3.png" alt="论小文"> </a> </div> <p>Read the full article at: Preprocess Comment Content in WordPress</p> <p><img alt="Treehouse" src="http://www.68idc.cn/help/uploads/allimg/150123/0SUKb1-0.jpg"><br><img alt="Wufoo" style="max-width:90%" src="http://www.68idc.cn/help/uploads/allimg/150123/0SUIX6-1.jpg" style="max-width:90%"></p>

WPS零基础入门到精通全套教程!
WPS零基础入门到精通全套教程!

全网最新最细最实用WPS零基础入门到精通全套教程!带你真正掌握WPS办公! 内含Excel基础操作、函数设计、数据透视表等

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号