How can I check if the current page is wp-login.php
or wp-signup.php
?
Are there more elegant solutions than using $_SERVER['REQUEST_URI']
?
How can I check if the current page is wp-login.php
or wp-signup.php
?
Are there more elegant solutions than using $_SERVER['REQUEST_URI']
?
You must be logged in to post a comment.
Use the global
$pagenow
, which is a common global set by WordPress at runtime:You can also check the type of login page, for example registration:
Following code is considered legacy and should not be used (
wp-register.php
was deprecated & subsequently removed quite a while back):My preferred way:
code:
Why it’s safest?
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:
Core Update:
Since WordPress Version 6.1 (Nov 2022) is an additional function inside the core –
is_login
that check thewp_login_url
.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:
There is a new function introduced in WordPress 6.1.0 which is
is_login()
which does just that.https://developer.wordpress.org/reference/functions/is_login/
$GLOBALS['pagenow']
doesn’t work, use$_SERVER['PHP_SELF']
.and if your wordpress is not installed in the web root folder, you should use some params like
YOUR_WP_PATH/wp-login.php
to replace the elements in array.I have implemented it using WordPress own wp_login_url() method as follows:
Just comparing both paths (because it’s difficult to be absolutely sure about the use of SSL as it may be terminated) should be enough … It does mean, however, that a plugin or theme developer who changes the default login form must have done so the proper way …
None of the current answers worked for me.
What I’ve done was check if
$_GET
array has a ‘page’ key and if its value is ‘sign-in’.If you need to add some code for login page, you can use a hook for it
This file has so many action hooks. Thanks WordPress devs!
Here’s a listing of the most common ones to get you started in the order they execute.
An alternative method:
Function
login_header
is defined in file wp-login.php. We are assuming, that login page is not included as this is separate WP page, which shall be called directly. Probably one of the most bulletproof solutions.Here’s a more readable version of @T.Todua answer. I just formatted it nicely, made the cheapest checks first and returned early:
If you want the least performance impact, leave only the first two checks.
Also, if you are running this check after the template has loaded, you can simply use:
I am only interested in register page, not in login page. So this might not be wanted by everybody.
$GLOBALS[‘pagenow’] returns index.php for me. Maybe because of buddypress or my theme.
So I used
If you inspect the body of the registration page, it also has the ID as well, so if it says page-id-4906, you can use it this way if it works better:
This is my preferred method, it will handle any slug you decide to use for the login page. It also handles custom login forms if you decide not to use the default WordPress login form since is_login() won’t work if you don’t include the login script. Uses global $wp instead of the $_SERVER array.
Always a good idea to use WP native function
is_login()
. This is introduced since 6.1.Here is the details
OK, I’m changing my answer to accommodate an alternate solution on detecting wp-login part of a url, that comes really handy if your theme uses a custom login page template or you have the login page modified by a plugin… A few solutions proposed here worked, so I’ve came with something that works for me on an ordinary WordPress site (not tested on Multisite).
As simple as: