Skip to:
Content
Pages
Categories
Search
Top
Bottom

Redirect from page if not logged in


  • ajuuls
    Participant

    @ajuuls

    Hello there! I’ve been searching for a code which actually works for me, and sadly, without any luck..

    I want to redirect the users from Member page to Login-page if they are not logged in. I can’t add it to my bp-custom.php because it does something weird to my theme so that php file is gone. So a function i can place in the functions.php or in the plugin “Custom CSS & JS” is the right solution. (Im using REIGN Theme)

    Thanks guys! Hopefully someone can help me.

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

  • Prashant Singh
    Participant

    @prashantvatsh

    Hi,

    Please paste the code in your child theme’s functions.php. If you do not have child theme then install this plugin https://wordpress.org/plugins/code-snippets/ and add a new snippet there and paste this code.

    add_action( 'template_redirect', 'ps_redirect_to_login' );
    
    function ps_redirect_to_login() {
    
    if ( is_page('members') && ! is_user_logged_in() ) {
    
      auth_redirect();
        }
    }

    Thanks 🙂


    ajuuls
    Participant

    @ajuuls

    Hmm.. it doesn’t seem to work. I want followed:
    If not logged in redirect from http://www.legit-check.dk/medlemmer/ to http://www.legit-check.dk/rm_login/

    My try:

    View post on imgur.com


    Prashant Singh
    Participant

    @prashantvatsh

    Ok, got that.

    Please replace auth_redirect(); with wp_redirect( 'http://www.legit-check.dk/rm_login/', 301 );

    and is_page('members') with is_page('medlemmer').

    I was not knowing the exact URL and page slug.

    Thanks


    ajuuls
    Participant

    @ajuuls

    Hello. I’ve tried the following code, without any solution:

    add_action( 'template_redirect', 'ps_redirect_to_login' );
    
    function ps_redirect_to_login() {
    
    if ( is_page('medlemmer') && ! is_user_logged_in() ) {
    
      wp_redirect( 'http://www.legit-check.dk/rm_login/', 301 );
        }
    }

    shanebp
    Moderator

    @shanebp

    Note: wp_redirect() does not exit automatically, and should almost always be followed by a call to exit;

    wp_redirect( 'http://www.legit-check.dk/rm_login/' );
    exit;

    Prashant Singh
    Participant

    @prashantvatsh

    Not an issue, just write exit(); after this wp_redirect( 'http://www.legit-check.dk/rm_login/', 301 ); and also replace template_redirect with wp.
    I forgot to mention that.
    If it still of no help, then please go for this plugin https://wordpress.org/plugins/simple-301-redirects/. You have to just enter the source and destination URL there.

    Thanks


    ajuuls
    Participant

    @ajuuls

    Now i’ve done what you’ve said and it still doesn’t work..

    add_action( 'wp', 'ps_redirect_to_login' );
    
    function ps_redirect_to_login() {
    
    if ( is_page('medlemmer') && ! is_user_logged_in() ) {
    
      wp_redirect( 'http://www.legit-check.dk/rm_login/', 301 );
      exit();
        }
    }

    ajuuls
    Participant

    @ajuuls

    Im trying to make that bp-custom.php and place it in /wp-content/plugins/ folder but when i write any code it makes my site come with the “site not working” error.. it feels like that file only makes it worse?

    Got it from this page:

    bp-custom.php


    ajuuls
    Participant

    @ajuuls

    View post on imgur.com

    This makes my site doesn’t work.


    Prashant Singh
    Participant

    @prashantvatsh

    There must be some other issue. Remove these codes nd try to give the plugin once. See if that helps you or not.


    ajuuls
    Participant

    @ajuuls

    It doesn’t help at all. Feel like the plugin doesnt does anything at all.


    Prashant Singh
    Participant

    @prashantvatsh

    Fixed 🙂

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