0

I'm using a custom url for the login of my WordPress site. I've added this code in my functions.php file

/** Hide default login */
function hide_default_login() {

    //Only proceed for guests
    if ( !is_user_logged_in() ) {

        //Getting current page
        $current_url   = str_replace( '/', '', $_SERVER['REQUEST_URI'] );
        $hiddenWpAdmin = 'access'; //Change this to your new secret wp-admin url
        $redirectTo  = '/';

        //Checking if accessing correct login url
        if ( $current_url == $hiddenWpAdmin ) {
            wp_redirect( '/'.$hiddenWpAdmin.'.php' );
            exit;
        }

        //Only allow requests to wp-login.php coming from correct login url
        $adminToCheck = [
            'wp-admin',
            'wp-login.php'
        ];
        
        if ( in_array( $current_url, $adminToCheck ) && $_GET['action'] !== "logout" ) {
            wp_redirect( $redirectTo );
            exit();
        }
    }
}

add_action('init', 'hide_default_login');

the login works fine, but I have some trouble with the logout, the user is redirected always to wp-login.php but I've modified and renamed the file.At the moment if I tr7y to logout, this will not work. How I can fix this issue? Do I need to use a filter or change something into the code?

2
  • 1
    WordPress believes the wp-login.php file exists unless you tell it otherwise. You can't just rename the file... it's a bit more complicated. There are a number of filters you need to modify, including site_url, network_site_url, wp_redirect (you need to prevent auto redirections to login). We have a feature in our Shield plugin that'll do all this for you and hide the login and wp-admin (wordpress.org/plugins/wp-simple-firewall)
    – Paul G.
    Aug 22, 2023 at 9:14
  • I've solved by adding some filters to the functions file of my theme. I will post the answer soon
    – ICTDEV
    Aug 22, 2023 at 10:18

1 Answer 1

1

After a bit of research I've fixed the problem I had with the redirect after logout. I've added this code to my functions file and it will use two filters to modify the default beahviour of wordpress after an user do the logout

/* Logout URL Fix */
function custom_logout_url( $logout_url, $redirect ) {
    $logout_url = home_url( '/access.php?action=logout' );
    $logout_url = wp_nonce_url( $logout_url, 'log-out' );
    return $logout_url;
}
add_filter( 'logout_url', 'custom_logout_url', 10, 2 );

function custom_redirect_after_logout() {
    if ( ! is_admin() ) {
        wp_safe_redirect( home_url() );
        exit();
    }
}
add_action( 'wp_logout','custom_redirect_after_logout' );

I will write a plugin so in future I will be able to get all things in place without touching the theme functions file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.