BuddyDev

Search

[Resolved] Restrict activity commenting by role?

  • Participant
    Level: Initiated
    Posts: 9
    Amanda on #45061

    I am following up on this post I read:

    https://buddydev.com/support/forums/topic/restrict-activity-commenting-by-member-type/

    The code given restricts commenting by member type. Is there a way to restrict commenting by wordpress role? I would like the default role to be unable to comment but be able to see all posts and other comments. If possible, is there a way to also have a message saying “Please sign up as a member to comment” To let them know as well?

    I’m currently restricting the entire activity feed by WordPress role but if the option above is available, any help would be appreciated!
    Thanks!

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2935
    Ravi on #45064

    Hello Amanda,

    Welcome to the BuddyDev forums. You can replace the following function

    
    
    bp_has_member_type( get_current_user_id(), 'student' );
    
    

    with

    
    
    // Replace the capability with role's capability you want to restrict
    current_user_can( 'edit_posts' );
    
    

    For role-based capability check the following URL.
    Reference URL: https://wordpress.org/support/article/roles-and-capabilities/

    Regards
    Ravi

    • This reply was modified 1 year, 11 months ago by Ravi.
  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2935
    Ravi on #45066

    Hello Amanda,

    Please replace the function with the followingone :

    
    
    /**
     * Check weather user is restricted from commenting.
     *
     * @param int $user_id User id.
     *
     * @return bool
     */
    function buddydev_is_restricted_commenter( $user_id ) {
    
    	// Never restrict super admin from commenting or no user found.
    	if ( is_super_admin( $user_id ) ) {
    		return false;
    	}
    
    	$user = get_user_by( 'id', $user_id );
    
    	// Roles those will be restricted.
    	$restricted_roles = array( 'contributor', 'subscriber' );
    
    	// Restrict only if the user has restricted roles.
    	if ( $user && array_intersect( $user->roles, $restricted_roles ) ) {
    		return true;
    	}
    
    	return false;
    }
    
    

    Regards
    Ravi

    • This reply was modified 1 year, 11 months ago by Ravi.
  • Participant
    Level: Initiated
    Posts: 9
    Amanda on #45078

    Hi, thanks for the response!
    I was a bit confused on the 2 responses you gave. I put the following code in functions and it’s not working. Is it incorrect? Am I supposed to place it elsewhere?

    /**
    * Check weather user is restricted from commenting.
    *
    * @param int $user_id User id.
    *
    * @return bool
    */
    function buddydev_is_restricted_commenter( $user_id ) {
    // Never restrict super admin from commenting.
    if ( is_super_admin( $user_id ) ) {
    return false;
    }

    return // Replace the capability with role’s capability you want to restrict
    current_user_can( ‘publish_posts’ );
    }

    /**
    * Check user can comment or not.
    *
    * @param bool $can Bool.
    *
    * @return bool
    */
    function buddydev_user_can_comment( $can ) {
    $activity = new BP_Activity_Activity( bp_get_activity_id() );

    $user_id = get_current_user_id();

    // Do not restrict for its own activity.
    if ( $activity->user_id == $user_id ) {
    return $can;
    }

    if ( buddydev_is_restricted_commenter( $user_id ) ) {
    $can = false;
    }

    return $can;
    }
    add_filter( ‘bp_activity_can_comment’, ‘buddydev_user_can_comment’ );
    add_filter( ‘bp_activity_can_comment_reply’, ‘buddydev_user_can_comment’ );

    Thanks again!

    • This reply was modified 1 year, 11 months ago by Amanda.
  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2935
    Ravi on #45083

    Hello Amanda,

    Try the following code:

    
    
    /**
     * Check weather user is restricted from commenting.
     *
     * @param int $user_id User id.
     *
     * @return bool
     */
    function buddydev_is_restricted_commenter( $user_id ) {
    
    	// Never restrict super admin from commenting or no user found.
    	if ( is_super_admin( $user_id ) ) {
    		return false;
    	}
    
    	$user = get_user_by( 'id', $user_id );
    
    	// Roles those will be restricted.
    	$restricted_roles = array( 'contributor', 'subscriber' );
    
    	// Restrict only if the user has restricted roles.
    	if ( $user && array_intersect( $user->roles, $restricted_roles ) ) {
    		return true;
    	}
    
    	return false;
    }
    
    /**
     * Check user can comment or not.
     *
     * @param bool $can Bool.
     *
     * @return bool
     */
    function buddydev_user_can_comment( $can ) {
    	$activity = new BP_Activity_Activity( bp_get_activity_id() );
    
    	$user_id = get_current_user_id();
    
    	// Do not restrict for its own activity.
    	if ( $activity->user_id == $user_id ) {
    		return $can;
    	}
    
    	if ( buddydev_is_restricted_commenter( $user_id ) ) {
    		$can = false;
    	}
    
    	return $can;
    }
    add_filter( 'bp_activity_can_comment', 'buddydev_user_can_comment' );
    add_filter( 'bp_activity_can_comment_reply', 'buddydev_user_can_comment' );
    
    

    Please give it a try.

    Regards
    Ravo

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2935
    Ravi on #45084

    You can place this code in the ‘bp-custom.php’ file.

    Please have a look at the resource URL: https://buddydev.com/docs/buddypress-guides/what-is-bp-custom-php/

    Regards
    Ravi

  • Participant
    Level: Initiated
    Posts: 9
    Amanda on #45094

    Hi, I inserted this code in my bp-custom.php folder and it, unfortunately, caused a critical error on my entire site. I removed the code, and my site is restored. I am not sure what is causing the error. I input the code you posted above and did not change it in any way. Any ideas? I can try the code that allows commenting based on capabilities as well if this code does not work. Members have more capabilities than subscribers.
    Thanks again!

  • Participant
    Level: Initiated
    Posts: 9
    Amanda on #45095

    Disregard the above post! I forgot to delete the previous code from my functions.php so that is what caused the critical error. I removed from functions.php and reinserted the new code to bp-custom.php and tested again. It’s now working! subscribers cannot comment unless it is their own post!
    Thank you so much Ravi!

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2935
    Ravi on #45098

    Hello Amanda,

    Thank you for the acknowledgement. I am glad that I could help you.

    Regards
    Ravi

The topic ‘ [Resolved] Restrict activity commenting by role?’ is closed to new replies.

This topic is: resolved