Programmatically block commenting by restricting view of comment form

I wonder if you mean this kind of approach:

add_filter( 'init', function()
{
    $u = wp_get_current_user();

    if( $u->exists() && in_array( 'banned', $u->roles, true ) )
        add_filter( 'comments_open', '__return_false' );
} );

where we check if the current user has the custom banned role.

If that’s the case then we force all comments to be closed through the comments_open filter.

That means this user should not be able to see the comment form or post a comment directly to wp-comments-post.php, because of the comments_open() checks.

Leave a Comment