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

THINKING THROUGH OUR FINGERS Writing, to me, is simply thinking through my fingers. – Isaac Asimov Menu SKIP TO CONTENT Search Advanced Critiquing: Using Facilitative Feedback OCTOBER 24, 2014 / THINKINGTHROUGHOURFINGERS Erin’s post yesterday offered a great primer on  effective critiquing . Today, I want to build on her ideas to offer some specific strategies toward becoming an outstanding critique partner. One of the most rewarding parts of the writing community is the support writers give each other, often through feedback and critique. I have been humbled and awed by the generosity of my fellow writers, time and time again. But critiquing can be intimidating, particularly if you’re new to writing. It’s scary to give your precious words to someone else. It can be even more daunting to try to respond to someone else’s words–especially when you’re not sure what to say. Or how to say it. That’s precisely what this post is for. In my other profession...
How to Plan, Plot, Write, Edit, Publish, and Market a Story PLAN   PLOT   WRITE   EDIT   PUBLISH   MARKET ABOUT Contact Resources Books Store Courses Programs Disclosure   HOME Anne R. Allen's Blog ...WITH RUTH HARRIS Sunday, August 10, 2014 What is a Beta Reader? Everything You Always Wanted to Know About Getting and Giving Feedback on your WIP T his week we're proud to host author and editor Jami Gold, fresh from her role as a presenter at the  RWA conference in San Antonio . If you missed the conference, Jami's posts on the highlights of the annual Romance Writers Association event are fascinating. You'll find them on her blog at  JamiGold, Paranormal Author . Jami's blog is a must-rea...
》》》》 》 How to Plan, Plot, Write, Edit, Publish, and Market a Story PLAN   PLOT   WRITE   EDIT   PUBLISH   MARKET ABOUT Contact Resources Books Store Courses Programs Disclosure   HOME What to Ask Your Beta Reader October 24, 2013  by  Valerie Comer   12 Comments A beta reader is going through a completed, polished manuscript much like a random reader would do. While they may be fellow writers, there’s no rule that says they must be. The main criteria for a beta reader is that they are widely read in the genre of your story, they know what they like and don’t like, can make a guess as to why they have either reaction—and aren’t afraid to tell you. A fellow writer is going to give you plenty of opinions about metaphors, punctuation, cliches, and grammar, which is why they’re more likely to fit the bill as a critique partner than beta reader. A beta reader isn’t an...