BuddyDev

Search

[Resolved] Hiding “whats-new-form” in member activity but NOT in Groups

  • Keymaster
    (BuddyDev Team)
    Posts: 22218
    Brajesh Singh on #44584

    The script will work fine for BuddyPress legacy Template pack. You are most probably not using it.

    Please visit Dashboard->Settings->BuddyPress->Options and look for the Template pack option.

    Let us know which template pack it is.

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 39
    yuriix on #44591

    Can you post a screenshot of where it should be? I can’t find it.

    Thanks.

    Regards

  • Keymaster
    (BuddyDev Team)
    Posts: 22218
    Brajesh Singh on #44622

    Hi Yuriix,
    Sure.
    Please have a look.
    https://i.ibb.co/8xS4Ncm/Selection-484.png

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 39
    yuriix on #44624

    Hello,

    The option to select “Template pack” is not there.

    Regards

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2505
    Ravi on #44636

    Hello Yuiix,

    Are you using BuddyPress or BuddyBoss Platform for your community?. Also, please post me the code you are using.

    Regards
    Ravi

  • Participant
    Level: Enlightened
    Posts: 39
    yuriix on #44639
    This reply has been marked as private.
  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2505
    Ravi on #44644

    Hello Yuriix,

    Thank you for sharing the details. It seems your theme hiding that setting. Please try the following code it will work with legacy template pack.

    
    /**
     * Filter ajax post request
     */
    function buddydev_restrict_post_update() {
    
    	// Always allow site admin.
    	if ( is_super_admin() || function_exists( 'bp_nouveau' ) ) {
    		return;
    	}
    
    	$object = empty( $_POST['object'] ) ? '' : sanitize_key( $_POST['object'] );
    
    	if ( ! $object || 'user' == $object || bp_is_user_activity() ) {
    		// Allowed user ids.
    		$allowed_user_ids = array( 10 );
    
    		if ( ! in_array( get_current_user_id(), $allowed_user_ids ) ) {
    			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 );
    
    

    You can assign comma(,) separated user_ids for allowed users.

    Please give it a try and let me know if it helps or not.

    Regards
    Ravi

  • Participant
    Level: Enlightened
    Posts: 39
    yuriix on #44645

    Hello,

    it works 🙂

    Would it be possible to hide the form for other users except (besides) admin? If not, still great and thanks.

    Regards

  • Participant
    Level: Enlightened
    Posts: 39
    yuriix on #44889

    Hello, I suppose it’s probably not possible for only Admin to see the Add Activity field. It’s good that the user can’t post activities, but it’s confusing that it’s by see.

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2505
    Ravi on #44896

    Hello Yuriix,

    Please try the following code:

    
    /**
     * Check 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 = array( 24, 2 );
    
    	$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 );
    
    

    Please replace the old code with this and give it a try.

    Note: It will hide the posting form on the activity directory as well. But users can post group activity within the group.

    Regards
    Ravi

The topic ‘ [Resolved] Hiding “whats-new-form” in member activity but NOT in Groups’ is closed to new replies.

This topic is: resolved