BuddyDev

Search

Creating galleries only to role admin

  • Participant
    Level: Initiated
    Posts: 1
    AntonioForte on #16462

    I am using mediapress to create galleries, the problem is that I would like the option to be available only for the administrator role.

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2932
    Ravi on #16472

    Hello Antonio,

    Thank you for posting. Please use the following code disable gallery tab for non-admin users.

    
    /**
     * Modify gallery tab permission
     *
     * @param bool   $is_active Can create gallery or not.
     * @param string $component Component name.
     * @param int    $component_id Component id.
     *
     * @return bool
     */
    function buddydev_modify_gallery_tab_permission( $is_active, $component, $component_id ) {
    
    	if ( 'members' == $component && ! is_super_admin( $component_id ) ) {
    		return false;
    	}
    
    	return $is_active;
    }
    add_filter( 'mpp_is_enabled', 'buddydev_modify_gallery_tab_permission', 10, 3 );
    
    

    Thank You
    Ravi

  • Participant
    Level: Initiated
    Posts: 1
    AntonioForte on #16494

    thanks excellent contribution and if I wanted a specific role besides the admin was able to create galleries

  • Keymaster
    (BuddyDev Team)
    Posts: 24190
    Brajesh Singh on #16499

    Hi Antonio,
    In that case, we will need to first create a function to test for role like this

    
    
    /**
     * Check if a user has given WordPress role.
     *
     * @param int    $user_id user.
     * @param string $role role.
     *
     * @return bool
     */
    function buddydev_user_has_role( $user_id, $role ) {
    	$user   = get_user_by( 'id', $user_id );
    
    	if ( empty( $user ) ) {
    		return false;
    	}
    
    	$user_roles = $user->roles;
    
    	if ( empty( $user_roles ) ) {
    		return false;
    	}
    
    	return in_array( $role, $user_roles, true );
    }
    
    

    And then, we can use it like below(I am only enabling for administrator in this case)

    
    
    /**
     * Only allow galleries for a certain role.
     *
     * @param bool   $is_active Can create gallery or not.
     * @param string $component Component name.
     * @param int    $component_id Component id.
     *
     * @return bool
     */
    function buddydev_enable_gallery_for_role_only( $is_active, $component, $component_id ) {
    
    	if ( 'members' == $component ) {
    		$is_active = buddydev_user_has_role( $component_id, 'administrator' ); // 'editor', 'contributor', 'subscriber'
    	}
    
    	return $is_active;
    }
    add_filter( 'mpp_is_enabled', 'buddydev_enable_gallery_for_role_only', 10, 3 );
    
    

    That will do it for you.

    Regards
    Brajesh

  • Keymaster
    (BuddyDev Team)
    Posts: 24190
    Brajesh Singh on #16568

    Hi Antonio,
    Hope you are doing well.

    Can you please tell me if the above worked for you or not?

    Thank you
    Brajesh

You must be logged in to reply to this topic.

This topic is: not resolved