BuddyDev

Search

[Resolved] Restrict Activity Commenting by Member-type

  • Participant
    Level: Enlightened
    Posts: 38
    Keith on #27686

    I was wondering if there is a code snippet I could use to prevent certain member types from being able to comment in the activity feed. I want them to be able to see it button not comment on other people postings unless they are of a certain member type. Thank you.

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2934
    Ravi on #27687

    Hello Keith,

    Thank you for posting. Try the following code it prevents student type user from comment and replying on other user’s activity.

    
    /**
     * 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 bp_has_member_type( get_current_user_id(), 'student' );
    }
    
    /**
     * 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 do let me know if it works or not.

    Regards
    Ravi

  • Participant
    Level: Enlightened
    Posts: 38
    Keith on #27693

    It worked. Thanks Ravi

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2934
    Ravi on #27698

    Hello Keith,

    Thank you for the acknowledgement.

    Regards
    Ravi

The topic ‘ [Resolved] Restrict Activity Commenting by Member-type’ is closed to new replies.

This topic is: resolved