Using WordPress ‘is_login()’ PHP function

The is_login() WordPress PHP function determines whether the current request is for the login screen.

Usage

if (is_login()) {
    // Code to execute if the current request is for the login screen
}

Parameters

  • None

More information

See WordPress Developer Resources: is_login()

Examples

Redirect to homepage if the login page is accessed

Check if the current request is for the login page, and redirect the user to the homepage if it is.

function redirect_if_login() {
    if (is_login()) {
        wp_redirect(home_url()); // Redirect to the homepage
        exit;
    }
}
add_action('init', 'redirect_if_login');

Add custom CSS to the login page

Add a custom CSS file to the login page using the login_enqueue_scripts action.

function enqueue_custom_login_stylesheet() {
    if (is_login()) {
        wp_enqueue_style('custom-login', get_stylesheet_directory_uri() . '/login.css');
    }
}
add_action('wp_enqueue_scripts', 'enqueue_custom_login_stylesheet');

Disable the admin bar on the login page

Disable the admin bar for all users when they are on the login page.

function disable_admin_bar_on_login() {
    if (is_login()) {
        add_filter('show_admin_bar', '__return_false');
    }
}
add_action('init', 'disable_admin_bar_on_login');

Display a message on the login page

Display a custom message on the login page above the login form.

function display_custom_message_on_login() {
    if (is_login()) {
        add_action('login_message', function() {
            return "<p class='message'>Welcome to our website! Please log in to continue.</p>";
        });
    }
}
add_action('init', 'display_custom_message_on_login');

Log the number of login attempts

Increase a counter of login attempts in the database each time the login page is accessed.

function log_login_attempts() {
    if (is_login()) {
        $attempts = (int) get_option('login_attempts', 0);
        $attempts++;
        update_option('login_attempts', $attempts);
    }
}
add_action('init', 'log_login_attempts');