Support » Fixing WordPress » wp-login canonical_redirect issue

  • hi.
    i am building a website, with wordpress (latest version).
    in this website, i have custom login form, which has its own address.
    i have blocked the back-end login form (default wp-login form)

    i do not want any redirection.
    i want to stop the redirection when special URL is called.

    here is the situation :
    you are not logged in.
    when you try to reach
    site.com/wp-admin
    you automatically will get redirected to :
    site.com/wp-login.php?redirect_to=http%3A%2F%2Fsite.com%2Fwp-admin%2F&reauth=1

    i want to stop this automatic redirection. and i know this is the default action of wordpress.
    but i simply want to stop it.

    when you try to reach /wp-admin , you have to see site.com/wp-admin (whether you are logged in or not)
    – if you are not logged in, you will see 404 (i have done that with custom code)
    – if you are logged in, and you do not have permission to see the dashboard , you will get to back-end and see the dashboard. (i have done it with custom code)
    – – if you are logged in, and you have permission to see the dashboard , you will get to back-end and see the dashboard.

    the url should not change, and you should not get redirected to wp-admin…etc address.

    i found some codes and modified them , but none of them is working…
    code number 1 –
    remove_action('template_redirect', 'redirect_canonical');
    code number 2 –
    remove_filter('template_redirect', 'redirect_canonical');
    code number 3 –

    add_action(
              'init',
              function() {
                  remove_action('template_redirect', 'wp_redirect_admin_locations', 1000);
              }
          );

    code number 4 –

    function custom_wp_redirect_admin_locations() {
        global $wp_rewrite;
        if ( ! ( is_404() && $wp_rewrite->using_permalinks() ) )
            return;
        $admins = array(
            home_url( 'wp-admin', 'relative' ),
        );
        if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $admins ) ) {
                    get_template_part( 404 ); 
                    exit();
        }
        $logins = array(
            home_url( 'wp-login.php', 'relative' )
        );
        if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $logins ) ) {
                    get_template_part( 404 ); 
                    exit();
        }
    }
    
    function remove_default_login_redirect() {
        remove_action('template_redirect', 'wp_redirect_admin_locations', 1000);
        add_action( 'template_redirect', 'custom_wp_redirect_admin_locations', 1000 );
    }
    add_action('init','remove_default_login_redirect');

    code number 5 –

    add_action(
      'template_redirect', 
      function() {
        $requ = untrailingslashit($_SERVER['REQUEST_URI']);
        if (site_url('wp-admin','relative') === untrailingslashit( $_SERVER['REQUEST_URI'] )){
          remove_action( 'template_redirect', 'wp_redirect_admin_locations', 1000 );
        }
      }
    );

    code number 6 –

    function custom_wp_redirect_admin_locations() {
        global $wp_rewrite;
        if ( ! ( is_404() && $wp_rewrite->using_permalinks() ) )
            return;
            
    $requested_url = untrailingslashit( $_SERVER['REQUEST_URI'] );
    
        $admins = array(
            home_url( 'wp-admin', 'relative' ),
            home_url( 'dashboard', 'relative' ),
            home_url( 'admin', 'relative' ),
            site_url( 'dashboard', 'relative' ),
            site_url( 'admin', 'relative' ),
        );
        if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $admins ) ) {
    redirect_canonical( $requested_url , false );
            exit;
        }
    
        $logins = array(
            home_url( 'wp-login.php', 'relative' )
        );
        if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $logins ) ) {
    redirect_canonical( $requested_url , false );
            exit;
        }
    }
    
    function remove_default_login_redirect() {
        remove_action('template_redirect', 'wp_redirect_admin_locations', 1000);
        add_action( 'template_redirect', 'custom_wp_redirect_admin_locations', 1000 );
    }
    
    add_action('init','remove_default_login_redirect');
    

    many thank to anyone who is trying to help me,
    Masoud.

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘wp-login canonical_redirect issue’ is closed to new replies.