我试图阻止客人(而不是用户)在一个页面上发布评论,如果他们填写相同的电子邮件地址。我的进一步目标是收集他们的独特地址为比赛(最好的评论.)。我正在考虑编写一些代码,以检查当前海报的电子邮件地址是否已经插入注释表之前的实际插入注释。检查电子邮件的存在对我来说是相当清楚的(get_comments()
),但我想不出一个合适的钩子来解雇我的支票。wp_insert_comment
钩子将在注释插入后运行我的代码<#>。有什么好主意吗?谢谢!
稍后编辑:我找到了一个preprocess_comment
过滤器的解决方案.
发布于 2023-03-30 21:40:34
function preprocess_mycomment($commentdata) {
$existing_comments = get_comments( array('post_id' => 31691) ); // I run the code for one specific page only
foreach ($existing_comments as $comment) {
$previous_comments = $comment->comment_author_email; // email address send by the current poster
if ( $previous_comments == $commentdata['comment_author_email'] ) { // comparing the current email address with the previous ones in database.
wp_die('The email ' . $previous_comments . ' has already been used.');
}
}
return $commentdata;
}
add_filter('preprocess_comment', 'preprocess_mycomment');
https://wordpress.stackexchange.com/questions/415017
复制