It is better to use ‘comment_post’ action for this purpose, it is fired when the comment is saved in database:
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;
}
}
Related Posts:
- Why do I get comment spam even with Akismet and Captcha?
- Removing the “Website” Field from Comments and Replies?
- How to deal with small scale comment spam on small commercial sites? [closed]
- What’s the easiest way to close comments on media/attachments?
- How to remove comment spam in WordPress
- How is comment spam received without a comments form?
- Hook to edit an column on comments screen?
- Allow anonymous comments, but prevent spam [closed]
- Is there any advantage to emptying comment spam?
- Why do I get email notifications about comments that WordPress has already determined are spam?
- Number of External Links in Comments – Moderation Option
- Altering the pingback functionality
- Comments screen in backend, how to disable Quick Edit | Edit | History | Spam | for non admins
- An action hook where a comment meta is updated
- Strategies for coping with hyperagressive spambots?
- On WordPress 4.4, how to get the post id using the comment_post hook
- How can I automatically delete comments that contain chinese / russian signs?
- Comments view limited to 20 results – any way to increase to 50 or 100?
- Report spam button
- Bots posting comments on pages
- How to make a secure blog that is completely private?
- Assign author to comment from post edit page
- All users/comments suspected as bot? [closed]
- Spammers attacking my WordPress Site – Removing URL field from core? [closed]
- How to use the ‘comments_number’ filter?
- custom hooks before a comment is updated/edited in the database
- How to block comments and pings?
- How to direct user after comment save
- Batch approve comments
- reCaptcha doesnt appear in comment (manual or plugin)
- Make every comment go to the spam folder
- Anonymous spam comments when only registered users can comment
- ‘comment_post’ hook doesn’t seem to work? [closed]
- WordPress comment processing . Default unapproved comments detection before posting
- Comment moderation
- Comments closed only for contributors
- Auto-deleting comments that trigger the blacklist
- approve,spam,trash etc. options are not coming on comments in admin panel
- What filter or action to replace comments or add new comment system
- comments are going to spam
- How to hide and disable URL and email fields from comments?
- Add option to disable comments on a per posts basis?
- Check If comment author is registered
- How to allow the reply link to remain on the comment form after I have reached my 10 nested comment limit?
- Is there a hook for comment author link?
- How can I edit the email sent when a new comment is received?
- How to Block Access to Standard Login Flow and Comment Flow
- Prevent Contributor to show comment list
- where to modify get_comment_author_link()?
- Does Akismet plugin expose any hooks, functions, class that can work with custom code?
- Displaying comments with a walker: how to distinguish between parent and child comments
- Count parent comments & replies separately?
- Comment form problem with comment_author_url and HTML5 input placeholders
- How would I count the number of times a comment meta field’s value is in a post’s entire comments?
- stackexchange-like submit comment window
- Cannot get local avatars to show [closed]
- Limit user to one comment
- WordPress comments on users profile
- wordpress threaded comments – customize nested ul / how child ul is displayd
- identify and delete spam comments from 3000 pending comments
- Hide comments column in WordPress backend
- How unrequire comment text in wordpress post
- WordPress Comments are automatically publishing
- save_post action hook for comments
- How to handle upvotes and downvotes of disqus comments after importing disqus comments to wordpress?
- Show comments of a user post only when they are login
- How to get and use the the number of days since the last comment?
- How do I add class to an admin comment?
- Best way to tell if a comment is from a user?
- How to replace default comments with custom HTML?
- How to make comments private for commentor and post author
- Name of comment field differs on different sites
- WordPress Spam Comment Filter
- Which hook do I use to edit pending comment count on wordpress dashboard?
- comment button shows only logged in users wordpress
- Applying same style to all the comments on the page
- Customise Comment form
- stumped on add_action hook to delete_comment – any ideas?
- Avoiding calls to theme-compat
- Allow tags between shortcode in comments
- Delete/Spam Comment Button
- Who approved a comment, to show up in dashboard
- comment just attachment .. reply just text … can I do that?
- I have an odd field with a purple background, mentioning HTML-codes to use while leaving comments
- No comment Section but still got a Comment
- How to prevent users/authors from seing IP/email of new commentators?
- Why are my threaded comments not quite working? [closed]
- $post->comment_status always returns ‘closed’
- Order comments in admin by custom date
- How to replace anonymous comment form with a registration form on wordpress?
- Redirect first time comments
- Posting XML in comment section
- Comments – Ensure the correct field is highlighted for nested replies
- How to hold all comments for moderation, including author’s comments on own post
- WordPress error when sending comment
- WordPress Page Template: Comment Filtering with Querystring
- Why default comment fields don’t show up?
- How to enable truly anonymous posting in bbPress forums? [closed]
- Get approved comments or comments that the author is me
- Show/Hide comments