• comments_open is used for both post types, posts and products, the snippet below worked and allowed only (example) role users to comment

    <!-- begin snippet: js hide: false console: true babel: false -->
    
    <!-- language: lang-js -->
    
        add_action( 'init', function()
        {
            $u = wp_get_current_user();
        	if( $u->exists() && in_array( 'example', (array) $u->roles, true ) )
        return;
            add_filter( 'comments_open', '__return_false' );
        } );
    
    <!-- end snippet -->

    But it also made the product reviews appear only for (example) role users and other roles cannot review products. I have tried modifying the snippet to remove post_type =='product' from the rule

    <!-- begin snippet: js hide: false console: true babel: false -->
    
    <!-- language: lang-js -->
    
        add_action( 'init', function()
        {$u = wp_get_current_user();
        	if( $u->exists() && in_array( 'example', (array) $u->roles, true ) OR $post->post_type == 'product' ){
        return;} 
        	
        	    add_filter( 'comments_open', '__return_false' );
        } );
    
    <!-- end snippet -->

    But still, other roles cannot review products, only (example) role users can, how do I do this? Thank you in advance

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Altering open/closed status is too broad a measure for what you want to do. What I would do is manage the visibility of the comment form based on role/capability. Do so right on the template that makes the comment_form() call. This is best done through a child theme. If you are managing reviews for WooCommerce products, there may be a useful WC filter or action available to manage visibility without altering templates.

    A user familiar with WP and HTTP requests could still post a comment without the form, but the lack of a form would deter almost everyone. If you need to securely enforce comment submissions, you could do so through the ‘allow_empty_comment’ filter. If the passed user is not allowed to post comments, simply call wp_die(). Only users getting around the lack of a form should see the die message. They are a hacker and are not owed a nice user experience.

    The empty comment filter is unrelated to what we’re doing, we’re using it like an action hook. It’s the first hook that fires which passes comment data and user ID. If the comment is allowed, just return false to proceed normally.

    Thread Starter 3dwan

    (@3dwan)

    Thank you for replying, it is just that I am not a developer, I don’t know coding, could it be done through a snippet? If so I would really appreciate it if you can help me add the right one for this function.
    Thank in advance for your time

    Moderator bcworkz

    (@bcworkz)

    You’ll need to locate where comment_form() (or whatever code generates the form) is located. It’s usually on theme templates, but it might be a WooCommerce template. Then we use your code to conditionally output the form instead of closing comments. (OR logic changes to AND) Something like:

    global $post;
    $u = wp_get_current_user();
    if( $u->exists() && in_array( 'example', (array) $u->roles, true ) && $post->post_type == 'product' ){
      comment_form();
    }

    If that works for you, there is another step needed to ensure this works in the long run. It’s not good to directly alter template code. Your alteration will be reverted during the next update. And you mustn’t avoid updates to protect your alteration. To properly override template code and be safe from updates, you need a child theme to contain your altered templates.

    Thread Starter 3dwan

    (@3dwan)

    Thank you but it seems that this can’t be done due to my child theme so I have installed WpDiscuz to do this function for me since it has all the features I need. I appreciate your time ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘WordPress comments_open for specific user role or post type’ is closed to new replies.