Wordpress - Check if wp-login is current page
Use the global $pagenow
, which is a common global set by WordPress at runtime:
if ( $GLOBALS['pagenow'] === 'wp-login.php' ) {
// We're on the login page!
}
You can also check the type of login page, for example registration:
if ( $GLOBALS['pagenow'] === 'wp-login.php' && ! empty( $_REQUEST['action'] ) && $_REQUEST['action'] === 'register' ) {
// We're registering
}
Following code is considered legacy and should not be used (wp-register.php
was deprecated & subsequently removed quite a while back):
if ( in_array( $GLOBALS['pagenow'], array( 'wp-login.php', 'wp-register.php' ) ) )
run_my_funky_plugin();
My preferred way:
if( is_wplogin() ){
...
}
code:
function is_wplogin(){
$ABSPATH_MY = str_replace(array('\\','/'), DIRECTORY_SEPARATOR, ABSPATH);
return ((in_array($ABSPATH_MY.'wp-login.php', get_included_files()) || in_array($ABSPATH_MY.'wp-register.php', get_included_files()) ) || (isset($_GLOBALS['pagenow']) && $GLOBALS['pagenow'] === 'wp-login.php') || $_SERVER['PHP_SELF']== '/wp-login.php');
}
Why it's safest?
- Sometimes, if you try to check login page using
REQUEST_URI
(orSCRIPT_PATH
), you will get INCORRECT VALUES, because many plugins change LOGIN & ADMIN urls.
2)$pagenow
will give you incorrect value too in that case!
Notes:
- In some cases, it might not work if you output login-form (i.e. with shortcode or etc) manually on other template files/pages.
More modern way to do that, it should work even when the wp-login URL is changed by plugins and when WP is in a subfolder, etc:
if(stripos($_SERVER["SCRIPT_NAME"], strrchr(wp_login_url(), '/')) !== false){
/* ... */
}