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

》¡》》》¡》 NAVIGATE Want To Spend More Time On Your Writing And Tired Of Doing It All? A Virtual Assistant Can Help Alexandra Amor Sometimes you need specific help for your situation Indie authors often have an edge of control freakery … well, I do! I like being in control and I enjoy pretty much all aspects of being an author entrepreneur. But I hit a wall about 18 months ago, and I definitely needed some help, so I started looking for a virtual assistant to help me. I had a few varied experiences and learned some lessons, and then  Alexandra Amor reached out to me with some brilliant suggestions for how she could help. Alexandra is a children's author, but she is also a fantastic virtual assistant for me and a number of other authors. I trust her to help me with key tasks in my author business, and she even suggests things that I may not have thought of. Alexandra Amor Today, Alexandra explains how a VA can help authors. Joanna has previously...
When we have excerpts or chapter reveals on site I do not like to copy and paste that long doc into a post. I have using a Google Embedder but it can get annoying and glitchy. Is there anyway to add a scrolling text box to posts? Juliana Hi Juliana! This is absolutely possible.   And luckily, it’s super easy to do! Here’s a preview of what we’ll be creating: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus non finibus tortor, sed ullamcorper massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Integer nulla lacus, maximus at tristique et, accumsan eget metus. Suspendisse arcu nisl, malesuada sit amet ante et, auctor porttitor nisl. Ut dictum leo metus, ac accumsan sem volutpat ac. Nulla leo eros, ultrices quis gravida id, rutrum at enim. Nam et volutpat nunc. Phasellus eu mollis nisl. Aliquam eget scelerisque ex, in dapibus nibh. Interdum et malesuada fames ac ante ipsum primis in faucibus. Suspendisse mollis s...
How to add an Image Slider to Your Blogger Blog Posted by  Ashley 8th March, 2013 Blogging Bitchin' Book Blog ,  Blogger ,  Coding ,  Image Slider ,  jQuery ,  Nivo Slider ,  Tutorial Last updated: 4 December 2015. We have a great question this week from Kaina! All you Blogger folks listen up, because this one is just for you! I was just wondering since I have a blogger blog do you know of any good picture sliders I could find or the process in which to make one? I would appreciate it tons!!  Kaina To make the image slider work, you need to add some extra files to your site. We need to add one or two JavaScript files and one CSS file. Step 1: Add the jQuery library (if you don’t already have it). First, you need to figure out if you need to include jQuery or not. The easiest way to do that is to visit your block, right click, select  “View Page Source”  and do a CTRL+F search for  jquery . If you get a ...