Skip to:
Content
Pages
Categories
Search
Top
Bottom

How to redirect to a custom page url if non logged in user clicks on a profile


  • kpkreative
    Participant

    @kpkreative

    Hi there,

    I am looking to achieve the following:

    If a non-logged in user clicks on a single user profile from the public view directory, I would like them to be redirected to a custom url on the site to show them that the page is restricted unless you login (url/content-restricted).

    At the moment, if a non-logged in user clicks on a profile from the directory, they are redirected to the homepage. I would like to override this redirects destination if possible?

    Thanks in advance

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

  • Anonymous User 18187419
    Inactive

    @anonymized-18187419

    This free plugin should work for you:

    BuddyPress Members Only


    kpkreative
    Participant

    @kpkreative

    Hi Evie,

    Do you have an example of working code for this with regards to if non logged in user tries to view a members profile, redirect them to a custom url?

    Thanks @Michael w/regards to plugin reference. I was hoping to achieve this without the use of another plugin. The one referenced hasnt been updated in sometime and i am a bit sceptical about that.

    Thanks in advance.


    kpkreative
    Participant

    @kpkreative

    I tried something like the following without success:

    //Hide for non-logged-in users (public visitors)
    function bp_logged_out_page_template_redirect() {
    if( ! is_user_logged_in() && bp_is_user() ) {
    wp_redirect( home_url( ‘/content-restricted/’ ) );
    exit();
    }
    } add_action( ‘template_redirect’, ‘bp_logged_out_page_template_redirect’ )


    Anonymous User 18187419
    Inactive

    @anonymized-18187419

    Hi kp,

    yes I’m with ya on keeping extra plugins to a minimum.

    Code published here…

    https://easywebdesigntutorials.com/hide-buddypress-pages-and-bbpress-forums-from-not-logged-in-users/

    …with minor modification (to remove bbpress check) seems to achieve what you want, as per below:

    function ps_guest_redirect() {
         global $bp;
      if ( bp_is_user() ) {
          if(!is_user_logged_in() and !bp_is_register_page() and !bp_is_activation_page()) {
               wp_redirect('https://google.com');
      exit;
          }
      }
    }
    add_filter( 'get_header','ps_guest_redirect',1);

    kpkreative
    Participant

    @kpkreative

    Hi Michael,

    Thank you so much for sharing the code for the redirect and the great source.

    There must be something on my website that is conflicting as I’ve added the code and the redirect still goes to the homepage rather than the custom link.

    I want the non logged in user to still see the directory, though not the single profiles, so I referenced the page below in the code

    My code is as follows:
    function ps_guest_redirect() {
    global $bp;
    if ( bp_is_user() ) {
    if(!is_user_logged_in() and !bp_is_register_page() and !bp_is_activation_page() and !is_page(‘member-directory’)) {
    wp_redirect(‘https://websiteurl/content-restricted’);
    exit;
    }
    }
    }
    add_filter( ‘get_header’,’ps_guest_redirect’,1);


    Anonymous User 18187419
    Inactive

    @anonymized-18187419

    kp,

    yes probably a conflict.
    I’ve tested both versions (include/exclude members directory) on my site and both redirect to specified page as required.

    WP 6.0.3
    BP 10.6.0

    (note: check your code, redirect url has no closing single quote, might be an issue)


    kpkreative
    Participant

    @kpkreative

    Just incase it helps anyone else, the following code resolved the issue for me:

    //Hide for non-logged-in users (public visitors)
    add_action( ‘init’, function() {
    if ( ! is_user_logged_in() && !bp_is_members_directory() && bp_is_user() ) {
    bp_core_redirect( site_url(‘/content-restricted/’) );
    } else {
    // Do nothing.
    }
    } );


    locker17
    Participant

    @locker17

    This code does not work if the admin switches the approval status during the user is logged in.
    So code needs to check the approval status as well and if its pending redirect.


    locker17
    Participant

    @locker17

    Someone able to point me to the code to check the approval status of a logged in user.


    thinlizzie
    Participant

    @thinlizzie

    Where’s your approval functionality coming from ? … WordPress, Buddypress, or a plugin?


    locker17
    Participant

    @locker17

    I already solved this. I checked user meta “ur_user_status” when user logged in.

Viewing 11 replies - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.
Skip to toolbar