is_login: check if current page is the login page in WordPress
The is_login
function in WordPress is used to determine if the current request is for the login page. This function checks whether the current page being accessed is the login page, which is typically located at /wp-login.php
.
The is_login
function can be useful in scenarios where specific actions or behaviors need to be triggered based on whether the user is on the login page. For example, it can be used to conditionally enqueue scripts or styles, redirect users, or modify the layout or content displayed on the login page.
The function operates by inspecting the global $pagenow
variable, which contains the filename of the currently requested script. If the filename matches wp-login.php
, the function returns true
; otherwise, it returns false
.
Parameters
The is_login
function does not take any parameters.
Return Value
The is_login
function returns a boolean value: true
if the current screen is the WordPress login screen, and false
otherwise.
Examples
How to Enqueue Scripts on Login Page
function enqueue_custom_login_scripts() {
if ( is_login() ) {
wp_enqueue_script('custom-login-script', get_template_directory_uri() . '/js/custom-login.js', array(), '1.0', true);
}
}
add_action('login_enqueue_scripts', 'enqueue_custom_login_scripts');
This code snippet demonstrates how to enqueue a custom JavaScript file on the WordPress login page. The enqueue_custom_login_scripts
function checks if the current page is the login page using the is_login
function. If true, it enqueues the custom-login-script
using the wp_enqueue_script
function. The script is located in the theme’s /js
directory.
How to Add a Custom Header Message on Login Page
function custom_login_message() {
if ( is_login() ) {
echo '<h1>Welcome to My Custom Login Page!</h1>';
}
}
add_action('login_header', 'custom_login_message');
This code snippet shows how to add a custom header message to the WordPress login page. The custom_login_message
function checks if the current page is the login page using the is_login
function. If true, it outputs a custom header message. The function is hooked to the login_header
action to ensure it appears at the top of the login page.
Conclusion
The is_login
function in WordPress is designed to determine whether the current request is for the login page. This function can be particularly useful for developers when conditionally enqueuing scripts or styles, customizing login page behavior, or implementing security measures. By leveraging the is_login
function, developers can create more dynamic and context-aware WordPress themes and plugins that respond appropriately when users are interacting with the login page.
Related WordPress Functions
- How to set authentication cookie using wp_set_auth_cookie in WordPress
- How to log out a user in WordPress with wp_logout
- Authenticating WordPress users using wp_authenticate
- Checking if user is logged in on WordPress using is_user_logged_in function
- Logging in users programmatically in WordPress using wp_signon