BuddyDev

Search

[Resolved] Hide member list/page from specific users

  • Participant
    Level: Master
    Posts: 285
    evillizard on #25302

    Hello boss

    About this

    https://buddydev.com/hide-buddypress-members-directory/

    I want to make it visible for just about 4 specific users (including admin)

    These users already have a user-role serving another important function and i dont want to mess with those roles

    How do i make visible per user??

    ….

    ….
    *****

    Also for other users who can’t see it…i want them to be redirected to another page/post of my choice when they click the members page link. (the page would have a message telling them that they are not allowed to view this page… Instead of them just seeing error 404)

  • Participant
    Level: Master
    Posts: 285
    evillizard on #25303

    Sorry i think this topic is meant to be

    : “SHOW member list/page TO
    specific users”



    Sorry my mistake

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

    Hi,
    Thank you for the question.

    Do you want to keep the list of user ids or name for allowing?

    Regards
    Brajesh

  • Participant
    Level: Master
    Posts: 285
    evillizard on #25311

    I dont get boss

    ..

    Both…teach me both.. How to keep the user id and name..teach me the two

  • Participant
    Level: Master
    Posts: 285
    evillizard on #25320

    Hello

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

    Hi,
    Here is the code.

    1. Let us define a function to check if the current user has one of the ids in the lsit

    
    
    /**
     * Check if the directory should be visible to a user.
     *
     * @return bool
     */
    function bp_custom_is_directory_visible() {
    
    	$visible_to_user_ids = array( 1, 2, 3, 20 );
    
    	return is_user_logged_in() && in_array( get_current_user_id(), $visible_to_user_ids );
    }
    
    

    You can add your own user ids.

    In case you wish to use username instead of the id, we can redefine the same function as

    
    
    /**
     * Check if the directory should be visible to a user.
     *
     * @return bool
     */
    function bp_custom_is_directory_visible() {
    
    	$visible_to_user_names = array( 'admin', 'xyz' );
    
    	return is_user_logged_in() && in_array( wp_get_current_user()->user_login, $visible_to_user_names );
    }
    
    

    Please note, you can use only one of the above as I have used same function name. Putting both in your file will generate error.

    Now that we have written the function for checking user, we can update our code from blog to use it as

    
    
    /**
     * Hide Members Directory from everyone.
     */
    function buddydev_hide_members_directory_for_all() {
    	if ( bp_is_members_directory() && ! bp_custom_is_directory_visible() ) {
    		bp_do_404();
    		load_template( get_404_template() );
    		exit( 0 );
    	}
    }
    
    add_action( 'bp_template_redirect', 'buddydev_hide_members_directory_for_all' );
    
    

    That’s all.

    Hope it works for you.

    Regards
    Brajesh

  • Participant
    Level: Master
    Posts: 285
    evillizard on #25405

    Thank you boss…i appreciate you so much ( i used by user ID cos thats the one that worked)

    I.wonder is it possible to redirect those who are not allowed to see the page.. To redirect them to another page of my choice?

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

    No Issues.

    Please remove these line

    
    bp_do_404();
    load_template( get_404_template() );
    exit( 0 );
    

    and replace them with

    
    bp_core_redirect( "http://yoursite_url");
    
    

    That will do it.

    Regards
    Brajesh

  • Participant
    Level: Master
    Posts: 285
    evillizard on #25811

    Thank you sir..ild try it out as soon as i can…no electricity for days

You must be logged in to reply to this topic.

This topic is: resolved