Wordpress - how detect if current page is the logi

2020-05-30 07:14发布

问题:

There is a better way than using global variable $pagenow to detect current page if is login page, like a is_admin() ?

if ($pagenow != 'wp-login.php' && !is_admin())
{
    // Do something
}

There is a global variable $current_screen with a getter get_current_screen() (which declared in /wp-admin/includes/template.php), but it's always equal to NULL

On #15686 (Detect the current page template tag) – WordPress Trac it's sayed it's usually used $pagenow, but I think is not the good way to compare non-dynamic pages against there file name instead of there function (like admin page)

回答1:

While I tend to agree with others on the need for a function is_login_page() or something similar, I found what seems to be the best answer at https://wordpress.stackexchange.com/questions/12863/check-if-were-on-the-wp-login-page, which I used to make the following:

<?php
function is_login_page() {
    return in_array($GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php'));
}


回答2:

If you are like me, and you actually tried to de-register / mess with the jQuery that WordPress automatically loads, then the correct answer is:

Don't use wp_print_styles to register your scripts – use wp_enqueue_scripts instead!

This hook will run only on the frontend, not on the login page, so there's no need for workarounds.

Nacin is explaining it here: http://make.wordpress.org/core/2011/12/12/use-wp_enqueue_scripts-not-wp_print_styles-to-enqueue-scripts-and-styles-for-the-frontend/



回答3:

Can't you explain what are you going to do with it? So I can tell if you should code using wordpress hooks.

or you can use the absolute uri, just match it with wp-login.php

<?php
$uri = $_SERVER['REQUEST_URI'];

echo $uri;

?>



回答4:

Incase you want to be as non WP independant as possible; for instance in a plugin keeping future changes out of scope. You can use something like this:

function is_login_page() {
    return !strncmp($_SERVER['REQUEST_URI'], '/wp-login.php', strlen('/wp-login.php'));
}


标签: php wordpress