Skip to:
Content
Pages
Categories
Search
Top
Bottom

Restrict the capability to change profile picture based on user role


  • Riolec
    Participant

    @riolec

    Halo,

    Can i decide who can or cannot change the profile picture based on user role?
    Is there any plugins for this or code change?

    Thank you in advance.

    note: i use wordpress Version 5.1.1 and buddypress Version 4.2.0

Viewing 2 replies - 1 through 2 (of 2 total)

  • Venutius
    Moderator

    @venutius

    This one is not that straight-forward there is a filter you can use as follows:

    function bpex_role_disable_avatar_uploads( $default ) {
    	
    	$user = wp_get_current_user();
    	$user_roles_array = $user->roles ? $user->roles : array();
    	
    		foreach ( $user_roles_array as $key => $role ) {
    			if ( $role == 'subscriber' ) {
    				$default = true;
    			}
    		}
    		
    		return $default;
    	
    }
    
    add_filter( 'bp_disable_avatar_uploads', 'bpex_role_disable_avatar_uploads' );

    However this causes BuddyPress to simply show the user that they can upload their profile image via gravatar, and I’m afraid you can’t switch this off.

    So the alternative is to remove the Change Photo tab and admin-bar menu item:

    add_action( 'bp_setup_nav', 'bpex_remove_change_profile_photo_tab', 50 );
    add_action( 'bp_setup_admin_bar', 'bpex_admin_bar_remove_change_profile_photo', 50 );
    
    function bpex_remove_change_profile_photo_tab() {
    	
    	if ( ! bp_is_user_profile() && ! bp_is_user_activity() && ! bp_is_user() && bp_displayed_user_id() != wp_get_current_user() ) {
    		return;
    	}
    	
    	$user = wp_get_current_user();
    	$user_roles_array = $user->roles ? $user->roles : array();
    	
    	foreach ( $user_roles_array as $key => $role ) {
    		if ( $role == 'subscriber' ) {
    			bp_core_remove_subnav_item( 'profile', 'change-avatar' );
    		}
    	}
    
    }
    
    function bpex_admin_bar_remove_change_profile_photo() {
    	
    	global $wp_admin_bar, $bp;
    
    	if ( ! bp_use_wp_admin_bar() || defined( 'DOING_AJAX' ) ) {
    		return false;
    	}
    
    	$user_id = get_current_user_id();
    	
    	if ( ! $user_id || $user_id == 0 || ! is_numeric( $user_id ) ) {
    		return;
    	}
    	
    	$user = wp_get_current_user();
    	$user_roles_array = $user->roles ? $user->roles : array();
    	
    	foreach ( $user_roles_array as $key => $role ) {
    		if ( $role == 'subscriber' ) {
    			$wp_admin_bar->remove_menu( 'my-account-xprofile-change-avatar', 'my-account-xprrofile' );
    		}
    	}
    
    }

    You’d need to put this in your child themes functions.php or a bp-custom.php file in the plugins directory.


    Venutius
    Moderator

    @venutius

    I forgot one, you also need to remove the option from the Admin>>My Profile page, if you allow admin access for users:

    
    add_action( 'bp_members_admin_xprofile_metabox', 'bpex_remove_user_profile_metabox', 20 );
    
    function bpex_remove_user_profile_metabox() {
    	
    	if ( ! is_admin() ) {
    		return;
    	}
    	
    	$user = wp_get_current_user();
    	$user_roles_array = $user->roles ? $user->roles : array();
    	
    	foreach ( $user_roles_array as $key => $role ) {
    		if ( $role == 'subscriber' ) {
    			remove_meta_box(
    				'bp_xprofile_user_admin_avatar',
    				buddypress()->members->admin->user_page,
    				'side'
    			);
    		}
    	}
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.
Skip to toolbar