is_user_logged_in() not working in wordpress plugin

元气小坏坏 提交于 2020-01-06 13:11:02

问题


is_user_logged_in() function not working in wordpress plugin Show warning like below:

Fatal error: Call to undefined function is_user_logged_in() in 

How can I use the logic in wordpress plugin?


回答1:


is_user_logged_in() is located in wp-includes/pluggable.php. So please include this file in your plugin file and check.




回答2:


Plugins are loaded prior to pluggable.php, which is where is_user_logged_in() is located. Which means the function doesn't exist yet when you're trying to call it. Instead, do this:

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



回答3:


Try with

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



回答4:


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

EDIT:

is_user_logged_in() is a pluggable function and you could get a fatal error if you call it too early. You can use this function inside your theme files without any additional code. Like:

<?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 } ?>

But inside plugin you should wait for wordpress to be loaded.

P.S. Sorry for my english.



来源:https://stackoverflow.com/questions/30135667/is-user-logged-in-not-working-in-wordpress-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!