我们知道为了防止用户在提交评论时因评论延迟而重复点击提交造成重复评论的现象,还有为了防止垃圾评论spam,WordPress本身在同一篇文章内是不允许有相同评论出现的。
而JV的主题添加了快捷评论功能,方便用户点击快捷评论按钮就可以提交一些常用语,当一个用户在这篇文章中使用了一个常用语后,下一个用户再想提交相同的评论就会被WordPress所限制。
为了解决这个问题,有两种方法:
一、修改WordPress核心文件
WordPress中检测重复评论的函数为wp_allow_comment()
,它在wp目录\wp-includes\comment.php
文件里面。
if ( $wpdb->get_var( $dupe ) ) { /** * Fires immediately after a duplicate comment is detected. * * @since 3.0.0 * * @param array $commentdata Comment data. */ do_action( 'comment_duplicate_trigger', $commentdata ); if ( defined( 'DOING_AJAX' ) ) { die( __('Duplicate comment detected; it looks as though you’ve already said that!') ); } wp_die( __( 'Duplicate comment detected; it looks as though you’ve already said that!' ), 409 ); }
将它们注释掉即可。 没有相关的钩子可供返回值,所以只能改源文件达到目的。
二、自定义函数
方法思路:先给评论末尾添加一串随机数,绕过wp_allow_comment()
函数,在刷新页面后再把这串随机数去掉。
function enable_duplicate_comments_preprocess_comment($comment_data) { //add some random content to comment to keep dupe checker from finding it $random = md5(time()); $comment_data['comment_content'] .= "重复评论{" . $random . "}重复评论"; return $comment_data; } add_filter('preprocess_comment', 'enable_duplicate_comments_preprocess_comment'); function enable_duplicate_comments_comment_post($comment_id) { global $wpdb; //remove the random content $comment_content = $wpdb->get_var("SELECT comment_content FROM $wpdb->comments WHERE comment_ID = '$comment_id' LIMIT 1"); $comment_content = preg_replace("/重复评论\{.*\}重复评论/", "", $comment_content); $wpdb->query("UPDATE $wpdb->comments SET comment_content = '" . $wpdb->escape($comment_content) . "' WHERE comment_ID = '$comment_id' LIMIT 1"); /* add your own dupe checker here if you want */ } add_action('comment_post', 'enable_duplicate_comments_comment_post');
这样就能让所有人都重复评论。若只希望管理员能够重复评论,则加一个判断进行过滤:
if( current_user_can('administrator') ) { add_filter('preprocess_comment', 'enable_duplicate_comments_preprocess_comment'); add_action('comment_post', 'enable_duplicate_comments_comment_post'); } function enable_duplicate_comments_preprocess_comment($comment_data) { //add some random content to comment to keep dupe checker from finding it $random = md5(time()); $comment_data['comment_content'] .= "重复评论{" . $random . "}重复评论"; return $comment_data; } function enable_duplicate_comments_comment_post($comment_id) { global $wpdb; //remove the random content $comment_content = $wpdb->get_var("SELECT comment_content FROM $wpdb->comments WHERE comment_ID = '$comment_id' LIMIT 1"); $comment_content = preg_replace("/重复评论\{.*\}重复评论/", "", $comment_content); $wpdb->query("UPDATE $wpdb->comments SET comment_content = '" . $wpdb->escape($comment_content) . "' WHERE comment_ID = '$comment_id' LIMIT 1"); /* add your own dupe checker here if you want */ }
以上代码添加到主题的functions.php
当中即可,That's all.
本站文章除注明转载/出处外,均为本站原创或翻译。若要转载但请务必注明出处,尊重他人劳动成果。
转载请注明出处链接 : https://www.inlojv.com/5062.html