is_user_logged_in()在wordpress插件中不起作用

问题描述 投票:0回答:4

is_user_logged_in()函数在wordpress插件中不起作用显示如下警告:

Fatal error: Call to undefined function is_user_logged_in() in 

如何在wordpress插件中使用逻辑?

php wordpress wordpress-plugin
4个回答
0
投票

插件在pluggable.php之前加载,这是is_user_logged_in()所在的位置。这意味着当您尝试调用它时,该功能尚不存在。相反,这样做:

add_action('init', 'ajax_auth_init');
function ajax_auth_init()
{
    if(!is_user_logged_in()) return;
    // rest of your code
}

0
投票

试试吧

$user = wp_get_current_user();
if($user->ID != 0 or $user->ID != null) ...

0
投票
function example_function() {
    if ( ! is_user_logged_in() ) {
        ajax_auth_init();
    }
}
add_action('init', 'example_function');

编辑:

is_user_logged_in()是一个可插拔功能,如果你过早地调用它,你可能会遇到致命的错误。您可以在主题文件中使用此功能,而无需任何其他代码。喜欢:

<?php if ( is_user_logged_in() ) { ?>
    <a href="<?php echo wp_logout_url(); ?>">Logout</a>
<?php } else { ?>
    <a href="/wp-login.php" title="Members Area Login" rel="home">Members Area</a>
<?php } ?>

但在插件内部,你应该等待wordpress加载。

附:对不起我的英语不好。


0
投票

is_user_logged_in()位于wp-includes / pluggable.php。所以请在您的插件文件中包含此文件并检查。

© www.soinside.com 2019 - 2024. All rights reserved.