Wordpress - how detect if current page is the logi

2020-05-30 06:49发布

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)

标签: php wordpress
4条回答
Anthone
2楼-- · 2020-05-30 07:06

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楼-- · 2020-05-30 07:06

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'));
}
查看更多
劫难
4楼-- · 2020-05-30 07:09

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'));
}
查看更多
男人必须洒脱
5楼-- · 2020-05-30 07:12

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;

?>

查看更多
登录 后发表回答