Support » Fixing WordPress » Restrict comments by user role AND post category

  • Resolved weric

    (@weric)


    Good morning,

    I am building a blog with various post categories. I would need all users to see all comments in all categories but I would need to allow comments only for a specific user role for each category.
    Example : for CATEGORY_1 posts only users with an EXPERT_1 role could comment, for CATEGORY_2 only users with an EXPERT_2 role could comment, for CATEGORY_3 only EXPERT_3 users could comment.

    I undestand this requires several lines of PHP and could be done with hiding the comment form for all roles expect the EXPERT in his CATEGORY. Any help appreciated in achieving this.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Something like this should work

    <?php if( current_user_can(‘EXPERT_1’) && in_category(‘CATEGORY_1’) ) { ?>
    // Comment form here
    <?php } ?>

    <?php if( current_user_can(‘EXPERT_2’) && in_category(‘CATEGORY_2’) ) { ?>
    // Comment form here
    <?php } ?>

    You should use something along lines of the following code:

    <?php
    	if( (current_user_can( 'expert_1') && has_category( 'category_1')) ||
    		(current_user_can( 'expert_2') && has_category( 'category_2')) ||
    		(current_user_can( 'expert_3') && has_category( 'category_3')) ) {
    			if ( comments_open() || get_comments_number() ) {
    				comments_template();//or comment_form() or whatever function you are using to display the comment form
    			}
    	}
    ?>

    You will need to place it in single.php or content-single.php of whatever relevant your theme is using.

    will try all this and get back, thanks

    Hi,

    Thanks vmuch, it works perfect when I insert this code in comments.php
    (did not work in single.php)

    Thanks again

    Glad it worked for you.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Restrict comments by user role AND post category’ is closed to new replies.