Hi, I’m following up on my previous post about how I wanted to restrict commenting on the activity page by restricted roles. I was wondering if it’s possible to instead have a “can comment” option where comments only show up for a paid role member (admin included).
I included the original code below:
/** * 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' );
To sum up, is it possible to reverse from restricted to allowing commenting for a paid role?
Is this possible?
Thanks!
Hi Amanda,
Thank you for the question.you can update the
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; }
with
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 allowed. $allowed_roles = array( 'contributor', 'subscriber' ); // Do not restrict if user has atleast one of the allowed roles. if ( $user && array_intersect( $user->roles, $allowed_roles ) ) { return false; } return true; }
Please feel free to change the $allowed_roles.
Regards
BrajeshThanks, that worked perfectly!
Is there also a way to restrict posting at the top of the activity page in the same way?screenshot here: https://ibb.co/zrsvwK3
Thanks again!
Hi Amanda,
There is no clean way to do it currently.
There are hooks that allow to buffer and remove the form but they are inconsistent and I will suggest doing a template override.Regards
Brajesh
The topic ‘ [Resolved] buddypress: can comment for user role option?’ is closed to new replies.