BuddyDev

Search

[Resolved] Limiting activity posting in user groups to admins onnly

  • Participant
    Level: Initiated
    Posts: 4
    gavyn on #53501

    I want to limit activity posting in my user group to admin only. Then I want to make sure other users that aren’t an admin can’t see what whats new form that is used for posting.

    I am using buddyx theme.

    Can someone help me out?

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #53506

    Hi gavyn,
    Welcome to BuddyDev support forums.
    I am not familiar with the code of BuddyX theme.

    Here is some code that doe sit (but in a reverse way, disables at other places except group).
    https://buddydev.com/support/forums/topic/hiding-whats-new-form-in-member-activity-but-not-in-groups/page/2/#post-44896

    If you are comfortable with code, Please customize it from your use case. If you are not comfortable with code customization, Please let me know. I will update it for default BuddyPress which you can check and see if that works for BuddyX.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 4
    gavyn on #53511
     function restrict_activity_updates_to_admins( $activity_content ) {
        // Check if the current user is logged in
        if ( is_user_logged_in() ) {
            // Get the current user's roles
            $user = wp_get_current_user();
            $roles = $user->roles;
    
            // Allow administrators to post updates
            if ( in_array( 'administrator', $roles ) ) {
                return $activity_content; // Admins can post
            } else {
                // Block non-admins from posting
                bp_core_add_message( __( 'Only administrators can post activity updates.', 'buddypress' ), 'error' );
                bp_core_redirect( wp_get_referer() ? wp_get_referer() : home_url() );
                return false;
            }
        }
    
        return $activity_content;
    }
    add_filter( 'bp_activity_content_before_save', 'restrict_activity_updates_to_admins' ); 

    This is my code to limit access to posting for admins only. I just want to make it so that the post forum doesn’t even show up for non admns.

  • Participant
    Level: Initiated
    Posts: 4
    gavyn on #53512

    for groups which if you could please help with code

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #53529

    Hi,
    Please try the following code.

    
    // Disables activity post form on group pages for non-admins
    /**
     * Checks if user can post activity
     *
     * @return bool
     */
    function buddydev_user_can_post_activity() {
    
    	if ( ! bp_is_group_activity() ) {
    		return true;
    	}
    
    	// Replace ids by your allowed user ids.
    	$allowed_user_ids = bp_group_admin_ids( groups_get_current_group(), 'array' );
    
    	$can = false;
    	if ( is_super_admin() || in_array( get_current_user_id(), $allowed_user_ids ) ) {
    		$can = true;
    	}
    
    	return $can;
    }
    
    add_action( 'bp_before_activity_post_form', function() {
    
    	if ( ! buddydev_user_can_post_activity() ) {
    		ob_start();
    	}
    
    } );
    
    add_action( 'bp_after_activity_post_form', function() {
    
    	if ( ! buddydev_user_can_post_activity() ) {
    		ob_get_clean();
    
    		$css = '#buddypress form#whats-new-form {display: none};';
    
    		wp_add_inline_style( 'bp-legacy-css', $css );
    	}
    } );
    
    add_action( 'bp_enqueue_scripts', function () {
    	if ( ! buddydev_user_can_post_activity() ) {
    		$css = '#buddypress form#whats-new-form {display: none};';
    
    		wp_add_inline_style( 'bp-parent-css', $css );
    	}
    }, 15 );
    
    /**
     * Filter ajax post request
     */
    function buddydev_restrict_post_update() {
    
    	// Always allow site admin.
    	if ( buddydev_user_can_post_activity() || function_exists( 'bp_nouveau' ) ) {
    		return;
    	}
    
    	$object = empty( $_POST['object'] ) ? '' : sanitize_key( $_POST['object'] );
    
    	if ( ! $object || 'user' == $object || bp_is_user_activity() ) {
    		exit( '-1<div id="message" class="error bp-ajax-message"><p>' . __( 'You are not allowed to post activity.' ) . '</p></div>' );
    	}
    }
    
    add_action( 'wp_ajax_post_update', 'buddydev_restrict_post_update', 9 );
    
    

    It should only show the post form to the group admin.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 4
    gavyn on #53540

    This code is not working. when i try going into a group now, it just shows “loading the group updates. PLease wait.”

    I want everyone to be able to still see updates but only group admins can post them.

  • Participant
    Level: Initiated
    Posts: 4
    gavyn on #53541

    I ended up figuring it out. Thank you for the help.

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #53547

    Hi,
    I am glad you were able to resolve it.

    Regards
    Brajesh

The topic ‘ [Resolved] Limiting activity posting in user groups to admins onnly’ is closed to new replies.

This topic is: resolved