Skip to main content

Automatically Approve Comments in a Certain Post Category

Coding tutorial: How to automatically approve all comments made on posts in a specific category.
Mohmoh submitted a question about how to approve comments that are made on a post in a specific category:
I had used this code to automatically approve comments of a specific category but after the last update wordpress 4.4 this code not work:
add_filter( 'pre_option_comment_moderation', 'auto_aprove_posts_b' );
add_filter( 'pre_option_comment_whitelist', 'auto_aprove_posts_b' );

function auto_aprove_posts_b( $option ) {
    if( in_category( '20' ) )
        return 0;

    return $option;
}
do you know how to automatically approve comments in the posts of a specific category?
mohmoh
To do this in WordPress, you’d want to hook into the pre_comment_approvedfilter. This filter allows you to adjust the comment’s approval status before adding it to the database.
The filter accepts two parameters:
  1. $approved – The current approval status before you modify it. This is what we’ll be changing (under certain circumstances).
  2. $commentdata – An array of data about the comment, including the ID of the post it corresponds to. We’ll need to use this when checking the category the post is in.
The code for this is quite simple:
function auto_approve_comments_in_category( $approved, $commentdata ) {
 $cat_id = 10; // This needs to be the ID of the category you want to approve.
 
 // If the post being commented on is in our category, always approve the comment.
 if( in_category( $cat_id, $commentdata['comment_post_ID'] ) ) {
  return 1;
 }
 
 // Otherwise, return the original approval status.
 return $approved;
}

add_filter( 'pre_comment_approved' , 'auto_approve_comments_in_category' , '99', 2 );
If you want, you could use this same code for other applications. For example: automatically approving comments on one specific post. Here’s how that would look:
function auto_approve_comments_on_post( $approved, $commentdata ) {
 $post_id = 503; // This needs to be the ID of the post you want to approve comments on.
 
 // If the post being commented on is post ID 503, always approve the comments.
 if( $commentdata['comment_post_ID'] == $post_id ) {
  return 1;
 }
 
 // Otherwise, return the original approval status.
 return $approved;
}

add_filter( 'pre_comment_approved' , 'auto_approve_comments_on_post' , '99', 2 );

Comments

Popular posts from this blog

Resources for Writers Some helpful books, blogs and other tools we recommend. These are tools we use and have found helpful for new writers. Alas, we don’t have time to vet new companies to add to the list, so please don’t ask us to do that. This list is simply a helpful, free offering to our readers–as is all the content of this blog. Unfortunately, we don’t have time to offer any more free services than we already offer with this blog. We need to spend some of our time writing books so we don’t starve–and we also need to sleep and see our families and go outside and breathe like other earth creatures. Thanks.    Recommended Books Social Media Smart Social Media  by Chris Syme Blog It!  By Molly Greene Rise of the Machines  by Kristen Lamb Blogging for Writers  by Robin Houghton (Only in paper. Extensively illustrated.) The Author’s Platform  by Barb Drozdowich. Barb also provides one-on-one social media help at  Bakerview Consulting . She’s great!) For Self-Pub
Welcome Services Blog Charities Contact Skip to primary navigation Skip to content Skip to primary sidebar Skip to footer HORKEY HANDBOOK Find or become a kickass virtual assistant today! MENU 9 Essential Elements of a Virtual Assistant Website that Attracts Clients February 22, 2018  By  Daryn Collier   1 Comment Heads up  — this post may contain affiliate links. We might receive a small commission if you make a purchase as a result of reading our awesome content. Thanks for supporting Horkey HandBook! In our private Facebook group for virtual assistants, it’s rare for a week to go by when someone doesn’t ask for feedback on their soon to be launched website. No matter how you slice it, launching version 1.0 of your website can be nerve-wracking and time-consuming — especially if you’re new to WordPress. The temptation to find an “easier” platform can be great but as a general rule, your hard work will be rewarded.
How to Create Your Own Social Media Icons Posted by  Ashley 26th June, 2014 Design Bitchin' Book Blog ,  Graphics ,  Graphics Tutorials & Freebies ,  Icons ,  Social Media I’m going to walk you through how to create your own social media icons for your blog. You can create icons that match your design, even if just by using the colours in your palette.  Requirements Photoshop : I suppose this isn’t really  required  but my tutorial will be using Photoshop. However, similar results can always be achieved in different programs. But you may not be able to follow my instructions exactly . Internet access : Good news! If you’re reading this, you have it! Step #1: Collect your icons The first step is to collect the basic, raw, social media icons. By that I mean only the site logo without any background or design elements. Decide which sites you want to make icons for, then download the icons. I personally use  IconFinder  for all my icon searches. You ca