If I'm going to use this code in my website

function redirect_logged_in_user() 
    if( is_user_logged_in () {

global $pagenow;
if( $pagenow == 'wp-login.php')     

Should I be replacing the $pagenow url for every page I need this to happen? An example is that I have at least three pages which needs to redirect logged in users. Not only wp-login.php, but my membership login page and also register pages.

And, once I inserted it on my functions.php theme file, how can I make the is_user_logged_in work?

1 Answer 1


You wouldn't replace the $pagenow, but would check it's value: 'wp-login.php' === $pagenow.

I recommend using one of the $_SERVER values, or WordPress helper functions (ex: is_page(), is_login()) on the template_redirect action hook.

add_action( 'template_redirect', static function () {
    if ( ! is_user_logged_in() ) {

    if ( ! is_login() ) {

    wp_redirect( 'https://customurl' );
} );
  • Oh by the way the $pagenow means that this code will check if it's on that wp-login.php page and then, if yes, it will put the functions to work? If I'm right, how can I replace the wp-login.php for like a normal URL?
    – Maria
    Jun 1, 2023 at 13:25
  • You shouldn’t need to use $pagenow. The WordPress helper functions should be sufficient.
    – Caleb
    Jun 1, 2023 at 13:34

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.