BuddyDev

Search

Only user_role/user_can are able to accept user testimonals

  • Participant
    Level: Initiated
    Posts: 3
    buddyuser on #44349

    Hello,

    I am wondering if it is possible for the php code to make it so only certain user_role or user_can are able to accept testimonials and have their own BP User Testimonials tab/functionality in profile.

    In other words, only certain user_role/user_can will be able to accept user testimonials and have the “Testimonials” tab displayed in their BP user profile.

    Otherwise, all other users won’t have “Testimonials” tab displayed in user profile. However, they can still leave testimonials for other user that do have it.

    Thanks.

  • Keymaster
    (BuddyDev Team)
    Posts: 21477
    Brajesh Singh on #44362

    Hi,
    Thank you for the question.
    yes, both of these are doable.

    here is some sample code for that.

    
    // disable testimonials component for some users.
    add_filter( 'bp_testimonial_enabled_for_user', function( $is_enabled, $user_id ) {
    
    	// process
    	return $is_enabled;
    }, 10, 2 );
    /// limit show can write
    add_filter( 'bp_testimonial_user_can_write', function($can) {
    
    	if( ! is_user_logged_in() ) {
    		return $can;
    	}
    	// in case you want to find the user for whom testimonial is being written
    	// it is displayed user.
    	// $displayed_user_id = bp_displayed_user_id();
    	// check for current user
    	if( current_user_can('read') ) {
    	// update $can to true/false/
    	}
    
    	return $can;
    });
    
    

    Hope you can adapt it for your purpose. Please let me know if you want me to put an example with roles?

    Regards
    Brajesh

    • This reply was modified 4 months ago by Brajesh Singh. Reason: updated code slightly
  • Participant
    Level: Initiated
    Posts: 3
    buddyuser on #44373

    Hi Brajesh,

    Thank you for the reply.

    I tried the first code but could not get it to work properly.

    
    // disable testimonials component for some users.
    add_filter( 'bp_testimonial_enabled_for_user', function( $is_enabled, $user_id ) {
    
    	if( ! current_user_can('publish_products') ) {
    		$is_enabled = false;
    }
    	return $is_enabled;
    }, 10, 2 );

    Well, I know why this doesn’t work. It disables testimonials but only for the user who is viewing a profile, not for the actual users themselves.

    What I am trying to do is only allow certain users to be able to receive testimonials/have testimonials tab in their profile.

  • Keymaster
    (BuddyDev Team)
    Posts: 21477
    Brajesh Singh on #44374

    Hi,
    instead of current_user_can, Please use the user_can like the following

    
    if( ! user_can($user_id, 'publish_products') ) {
    	$is_enabled = false;
    }
    
    

    This will work for displayed user.

    Regards
    Brajesh

You must be logged in to reply to this topic.

This topic is: not resolved