• PG

    (@parulgarg2)


    Hi

    I have a custom login page made in wordpress. I do not want to show wp-admin page, instead I would like users to access the custom page I made.

    I tried the below code that I found everywhere on the internet but it doesn’t work. $page_viewed never satisfies the condition. I am getting this is $page_viewed – wp-login.php?redirect_to=http%3A%2F%2Flocalhost%2F{siename}%2Fwp-admin%2F&reauth=1

    function my_redirect_login_page() {
        $login_page  = home_url( '/login/' );
        $page_viewed = basename($_SERVER['REQUEST_URI']);
        if( $page_viewed == "wp-login.php" && $_SERVER['REQUEST_METHOD'] == 'GET') {
            wp_redirect($login_page);
            exit;
        }
    }
    add_action('init','my_redirect_login_page');

    Can any one please help me out here.

    Thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello parulgarg2,

    Try below Code is that works

    Note : Replace $pageid with the page you want users to be redirected to

    /* Redirect log in page */
    function redirect_login_page(){
      // Store for checking if this page equals wp-login.php
       $page_viewed = basename( $_SERVER['REQUEST_URI'] );
    
      // permalink to the custom login page
      $login_page  = get_permalink($pageid);
    
      if( $page_viewed == "wp-login.php" ) {
        wp_redirect( $login_page );
        exit();
      }
    }
    
    add_action( 'init','redirect_login_page' );

    Let us know if this works.

    Thanks.

    Thread Starter PG

    (@parulgarg2)

    Hi Kartik

    Thanks for your reply.

    Main problem is the if condition here. page_viewed does not match wp-login.php. When we enter URL – sitename/wp-admin it redirects to {sitename}/wp-login.php?redirect_to=http%3A%2F%2Flocalhost%2F{siename}%2Fwp-admin%2F&reauth=1
    which is not equal to “wp-login.php”

    I hope what I am saying makes sense.

    Hello parulgarg2,

    Yes, I understand your concern, but I did try this code in my Local Env and it worked fine for me.

    I think you should check what value you get in $page_viewed by printing it, that will give you more clarity on what your if condition will be.

    Hope this helps.

    Thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Custom login page redirect’ is closed to new replies.