php Wordpress - 如何检测当前页面是否为登录页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5266945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:49:08  来源:igfitidea点击:

Wordpress - how detect if current page is the login page

phpwordpress

提问by mems

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

$pagenow如果是登录页面,则有比使用全局变量检测当前页面更好的方法,例如is_admin()?

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

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

有一个$current_screen带有 getter的全局变量get_current_screen()(在 中声明/wp-admin/includes/template.php),但它始终等于NULL

On #15686 (Detect the current page template tag) – WordPress Tracit'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)

#15686(检测当前页面模板标签) – WordPress Trac据说它通常被使用$pagenow,但我认为这不是将非动态页面与文件名而不是功能(如管理页面)进行比较的好方法

回答by Amereservant

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:

虽然我倾向于同意其他人需要函数 is_login_page() 或类似的东西,但我在https://wordpress.stackexchange.com/questions/12863/check-if-were-找到了似乎是最好的答案on-the-wp-login-page,我用来做以下事情:

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

回答by Manu

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:

如果您像我一样,并且实际上尝试取消注册/弄乱 WordPress 自动加载的 jQuery,那么正确答案是:

Don'tuse wp_print_stylesto register your scripts – use wp_enqueue_scriptsinstead!

不要使用wp_print_styles注册脚本-使用wp_enqueue_scripts,而不是!

This hook will run onlyon the frontend, noton 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/

Nacin 在这里解释:http://make.wordpress.org/core/2011/12/12/use-wp_enqueue_scripts-not-wp_print_styles-to-enqueue-scripts-and-styles-for-the-frontend/

回答by fedmich

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

你不能解释一下你打算用它做什么吗?所以我可以告诉你是否应该使用 wordpress hooks 进行编码。

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

或者您可以使用绝对uri,只需将其与 wp-login.php 匹配即可

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

echo $uri;

?>

?>

回答by Drone Brain

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:

如果您想尽可能不独立于 WP;例如在一个插件中,使未来的更改超出范围。你可以使用这样的东西:

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