Support » Plugin: User Role Editor » Allow commenting only users with specific role

  • Hello everybody,
    is there an option to allow only a specific user role to comment on posts?
    We have a paid membership and only these members should be allowed to comment.
    Unfortunately, I only found the moderate_comments permission, but this does not match the option I am looking for.
    Is there a feature like write-comments or similar that can be implemented?

    thanks a lot for your help!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Vladimir Garagulia

    (@shinephp)

    Hello,

    It may depend from a theme you use. Example below works with 2021 theme:

    add_filter( 'comments_open', 'comments_on_condition', 10, 2);
            
    function comments_on_condition($open, $post_id ) {
        
        if ( !is_user_logged_in() ) {
            // no role - no comments
            return false;
        }
        
        // replace roles below with your own, which can comment
        $roles_with_comments = array(
          'administrator',
          'role2'
        );
        
        $user = wp_get_current_user();
        if ( empty( $user->roles ) ) {
            // no role - no comments
            return false;
        }
        foreach ($roles_with_comments as $role)      {
            if ( in_array( $role, $user->roles) ) {
                // role can comment, return the global $open
                return $open;                    
            }
        }
        
        // no comments for all others
        return false;    
    }

    You can add it to the active theme functions.php file or setup as a Must Use plugin.

    Thread Starter behandlung

    (@behandlung)

    Thank’s a lot for your help!!
    I will try 🙂

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Allow commenting only users with specific role’ is closed to new replies.