BuddyDev

Search

Hide multiple user types from member/profile page

  • Participant
    Level: Enlightened
    Posts: 24
    xmginc on #10648

    First, thanks for the amazing resource you have here for BuddyPress users.

    Your solution to hide the profile for super admins is brilliant. (https://buddydev.com/support/forums/topic/hide-profile-administrator/)

    However, can I pls ask how to change this to work with multiple types of WordPress roles such as administrator, contributor, author and editor.

    Thank very much

  • Participant
    Level: Enlightened
    Posts: 24
    xmginc on #10662

    For example, could this be combined somehow with this below so that not only will the member role type not be displayed in the directory, the URL of the profile page will also not be visible:

    add_filter( ‘bp_after_has_members_parse_args’, ‘buddydev_exclude_users_by_role’ );

    function buddydev_exclude_users_by_role( $args ) {
    //do not exclude in admin
    if( is_admin() && ! defined( ‘DOING_AJAX’ ) ) {
    return $args;
    }

    $excluded = isset( $args[‘exclude’] )? $args[‘exclude’] : array();

    if( !is_array( $excluded ) ) {
    $excluded = explode(‘,’, $excluded );
    }

    //$role = ‘administrator’;//change to the role to be excluded
    $user_ids = get_users( array( ‘role__in’ => [‘administrator’ , ‘contributor’ , ‘author’, ‘editor’] ,’fields’=>’ID’) );

    $excluded = array_merge( $excluded, $user_ids );

    $args[‘exclude’] = $excluded;

    return $args;
    }

    Combined with the redirect of the profile page but including the various role types above:

    function buddydev_redirect_from_admin_profile() {
    //not a profile page or viewing own profile page, no redirect
    if ( ! bp_is_user() || bp_is_my_profile() ) {
    return;
    }

    //check if the user is admin?
    //there are multiple ways to do it, I will show for Super admin
    //You can use user_can(bp_displayed_user_id(), ‘cap_name’ ) too, Make sure to change cap name to appropriate cap

    $redirect_url = site_url(‘/why-did-i-get-redirected’);//or any url

    if ( is_super_admin( bp_displayed_user_id() ) ) {
    bp_core_redirect( $redirect_url );
    }

    }

    add_action( ‘bp_template_redirect’, ‘buddydev_redirect_from_admin_profile’ );

  • Keymaster
    (BuddyDev Team)
    Posts: 22202
    Brajesh Singh on #10771

    Hi,
    There are two parts

    1. Excluding users from listing
    2. Redirecting from the profile

    Can you please tell me which parts you need help with and I will assist.

    Please do note that on multisite, users may have different roles on different sites, so I need a little more information about your setup.

    Thank you
    Brajesh

You must be logged in to reply to this topic.

This topic is: not resolved