I want to redirect the default WordPress login page to a custom login page. I am adding this code in a plugin:

if (!is_user_logged_in()) {
    wp_redirect( wp_login_url( $redirect ) );

But it gives this error:

Fatal error: Call to undefined function is_user_logged_in()

Why is that happening and how can I fix that? Thanks.

  • What hook are you using to call the function that holds the redirect? Try using 'wp'. – shanebp Feb 7 '15 at 20:11
  • I did not understand, I placed the above code in a plugin file. Can you give a example of what you meant? Thanks. – alex Feb 7 '15 at 20:13

Generally, you need to tell WordPress when to do things. If you just dropped your code in a plugin, then you're not giving WordPress information on when to execute your commands, and you risk the functions you need not being defined yet.

Right now, your code is saying, "If someone isn't logged in, always redirect them to the regular login URL," which is the opposite of what it sounds like you're trying to do.

Try a function in your plugin file, and attaching it to a hook. Here's an example:

function wpse_177404_login_screen_redirect()
    global $pagenow;

    if ($pagenow == 'wp-login.php' && !is_user_logged_in()) {
add_action('init', 'wpse_177404_login_screen_redirect');

With that, you're telling WordPress to check if the page is wp-login.php on every load, and if someone isn't logged in, redirect them to the new page of your choosing.

Be careful when you over-ride these types of Core functionality, as it also impacts password resetting and registration, and can be very easy to frustrate your users by doing these sorts of things.

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