如果包含特定字符串,如何自动删除注释?
我尝试过这个,但没有成功:
add_action( \'transition_comment_status\', \'my_approve_comment_callback\', 10, 3 );
function my_approve_comment_callback( $new_status, $old_status, $comment ) {
if (strpos($comment->comment_content, \'dog\') !== false) {
wp_delete_comment( $comment->comment_ID, true );
}
}
我还尝试了:
wp_list_comments(\'callback=better_comment\');
function better_comment($comment, $args, $depth) {
if (strpos($comment->comment_content, \'dog\') !== false) {
wp_delete_comment( $comment->comment_ID, true );
}
}
没什么用=(
最合适的回答,由SO网友:Milan Petrovic 整理而成
为此,最好使用“comment\\u post”操作,当注释保存在数据库中时会触发该操作:
add_action(\'comment_post\', \'my_comment_post_callback\', 10, 3);
function my_comment_post_callback($comment_id, $comment_approved, $commentdata) {
if (strpos($commentdata[\'comment_content\'], \'dog\') !== false) {
$post_url = get_permalink($commentdata[\'comment_post_ID\']);
wp_delete_comment($comment_id, true);
wp_redirect($post_url);
exit;
}
}