is_user_logged_in() not available to plugins?

Hello,

I know, this might not be the correct place to ask, but I know there are people here that might know the answer… :slight_smile:

I am writing a plugin that loads some custom stuff to WordPress bar. I need to decide inside my plugin if the user is logged in or if its an anonymous user.

According to WordPress Codex I should call is_user_logged_in() but when I do I get “Fatal error: Call to undefined function is_user_logged_in()”.

It seems like this function is not available to plugins? What could I use instead to determine logged in/anonymous?

Code;

<?php

/*

Plugin Name: Skolbloggen.se

Description: Does stuff

Version: 1.0

Author: Måns Larsson

*/

if (is_user_logged_in())

{

/* do some stuff */

}

else

{

/* do some other stuff */

}

?>

  • Shawn
    • The Crimson Coder

    You can steal it directly out of the core from pluggable.php:

    if ( !function_exists('is_user_logged_in') ) :
    /**
    * Checks if the current visitor is a logged in user.
    *
    * @since 2.0.0
    *
    * @return bool True if user is logged in, false if not logged in.
    */
    function is_user_logged_in() {
    $user = wp_get_current_user();

    if ( empty( $user->ID ) )
    return false;

    return true;
    }
    endif;

  • Shawn
    • The Crimson Coder

    I guess I should also specify – add that code to your plugin. :wink:

    Don’t worry – it won’t break WP since the pluggable functions each operate with the same function_exists check for “code sanity”.

    Note that, however, if the specific code from your plugin is being called directly (ie; outside of the WP handlers), then the native WP functions (including the wp_get_current_user() function this depends on) will not work. You’ll need to rewrite your plugin to depend upon WP.

  • skolbloggen
    • Site Builder, Child of Zeus

    Well, maybe I was to quick to say this solved my problem… it did not;

    I now got;

    Fatal error: Call to undefined function wp_get_current_user()

    Ok, so I pasted that one to from pluggable, I then get;

    Fatal error: Call to undefined function get_currentuserinfo()

    This can’t be the right way of doing it?

  • Shawn
    • The Crimson Coder

    That means your plugin isn’t actually operating within the WP environment.

    What does your plugin do? I assume it must be using a custom URL to run this PHP – not via a parameterized URL such as “/wp-admin/network/admin.php?page=ust”?

  • skolbloggen
    • Site Builder, Child of Zeus

    Its a plugin that loads a CSS, adds/removes some menus, etc to WordPress Admin bar. The reason I have to do this in a plugin is because my theme is not loaded when a blog other then the main on is loaded, I have a MU environment.

    But in order to accomplish the error, the code I submitted is enough.

    The plugin is activated site-wide.

  • skolbloggen
    • Site Builder, Child of Zeus

    After some more research, I found a solution. I post it here if somebody is interested;

    add_action( 'template_redirect', 'admin_bar_skolbloggen' );
    function admin_bar_skolbloggen() {
    if ( is_user_logged_in() )
    {
    add_action( 'admin_bar_menu', 'admin_bar_skolbloggen_menu' );
    }
    }

    function admin_bar_skolbloggen_menu( $wp_admin_bar )
    {
    /* here is the stuff that needs to be done if logged in... */
    }

  • Shawn
    • The Crimson Coder

    Okay, how about this…

    It doesn’t look like you’re using a function for your code. Try wrapping it withing a function, then triggering the function based on an appropriate event. For example, init:

    function whatever(){
    // do stuff
    }
    add_action( 'init', 'whatever' );

  • Shawn
    • The Crimson Coder

    The code you posted within your question is a simple “if” statement, NOT a function. That means it processes inline. If the procedural code (functions) it depends upon have not yet been declared, it will not work.

    This is why there are so many ways to perform triggers, actions and filters with WP. To get the effect of a true application, most code depends on the WP core which may not be done loading by the time the plugin code is run.

    If you instead wrap it in a function and execute that function on init (or on whatever the most appropriate trigger is) then it will be more reliable and the libraries/functions that it depends on will have been given the time to load.

  • skolbloggen
    • Site Builder, Child of Zeus

    Shawn, you are quite right, I think. I have a pretty good understanding what is happening, but again, your argumentation makes me think that maybe not…

    Lets talk about my code (I hope I am not boring you :slight_smile:;

    add_action( 'template_redirect', 'admin_bar_skolbloggen' );

    That first line of my code is telling WP; call a function called “admin_bar_skolbloggen” when you reach the point “template_redirect”.

    What happens then, is that my function admin_bar_skolbloggen() are executed, and this is in my case the solution; “is_user_logged_in()” are now available because pluggable.php has now been loaded. Before, I tried to check “is_user_logged_in()” outside any function in my plugin, that is, when the plugin are loaded, before pluggable.php had been loaded, leaving me with the error “Fatal error: Call to undefined function is_user_logged_in()”.

    Well, thats my analysis, and lesson of this case. Don’t you agree on my conclusion? Do you think I am wrong?

  • Shawn
    • The Crimson Coder

    That’s exactly the situation.

    At this point, the only thing you might want to investigate is whether that’s (‘template_redirect’:wink: the most appropriate trigger. If your code is “heavy” (performs anything memory intensive you don’t want running on *every* request) then you’ll want to move it later. If it’s a determinination of whether users should have access rights to a specific URL, for example, then you might want to move it earlier. If it’s admin-only behavior, you’ll want to move it to admin_init so the code doesn’t run unnecessarily. If it’s a front-end thing then that’s probably a pretty good place for it.

  • skolbloggen
    • Site Builder, Child of Zeus

    Thanks for the input!

    And, If I consider my code so important that I want it to run always, no matter what, what event would you register to?

    What my plugin does is, always load and customize the wordpress admin bar so it contains, in my mind, the appropriate menu options.

  • Shawn
    • The Crimson Coder

    If you want it to run *always* then use “init” – this is the first trigger/action after WP core is fully loaded. But if you’re customizing the admin bar – it only really should run if the admin bar is being displayed, right? It shouldn’t run for an RSS Feed request, a trackback, a rejected request or for a javascript or CSS URL, should it?

    If you only want to effect the admin bar then I’d use ‘admin_bar_init‘.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.