pre_comment_approved

Home » Hooks » pre_comment_approved

The pre_comment_approved hook in WordPress is a filter that is executed just before the comment is approved and saved to the database. This hook is used to modify the approval status of a comment before it is saved to the database.

For example, if you want to automatically approve comments from certain users or email addresses, you can use the pre_comment_approved hook to do so. Or if you want to set some comments as "pending" for manual approval, you can use this hook to change the approval status.

Here is an example usage code of how you can use the pre_comment_approved hook:

function auto_approve_comments( $approved, $commentdata ) {
    $email = $commentdata['comment_author_email'];
    $auto_approve_emails = array( 'example@email.com', 'another@email.com' );
    if ( in_array( $email, $auto_approve_emails ) ) {
        return 1; // 1 means "approved"
    }
    return $approved;
}
add_filter( 'pre_comment_approved', 'auto_approve_comments', 10, 2 );

In this example, we are using the pre_comment_approved hook to automatically approve comments from certain email addresses. The function checks if the email address of the comment author is in an array of email addresses that we want to auto-approve. If it is, it returns 1 (meaning "approved"), otherwise it returns the original value of $approved (which could be 0 for "not approved" or "spam"). By adding this function to our theme’s functions.php file or a custom plugin file, we can modify the behavior of the comment approval process in WordPress.

Learn More on WordPress.org

WordPress snippets using the pre_comment_approved hook

Register an account to save your snippets or go Pro to get more features.