繁体   English   中英

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

[英]is_user_logged_in() not working in wordpress plugin

is_user_logged_in()函数在 WordPress 插件中不起作用。 显示以下错误:

致命错误:在如何使用 WordPress 插件中的逻辑中调用未定义的函数is_user_logged_in()

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

试试

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

插件在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
}
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。

PS对不起我的英语。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM