11

The current result is "PHP Fatal error: Call to undefined function wp_get_current_user()" which makes sense, but doesn't help.

I need to use $current_user.

Here is the code I'm currently using:

$wp->init(); 
do_action( 'init' ); // Check site status 
$file='http://xxxxxxxx.com/wp-admin/wp_includes/pluggable.php'; 
if ( is_multisite() ) { 
    if ( true !== ( $file = ms_site_check() ) ) { 
        require( $file );
        die(); 
    } 
    unset($file); 
}

// Get the current user's info 
$current_user = wp_get_current_user(); 

if ( !($current_user instanceof WP_User) ) 
    return; 

echo $current_user->user_login; 

function paf_uname(){ 
    return $current_user->user_login; 
}
3
  • 1
    Plugins are loaded before pluggable, but there are hooks available well after that. Post the code that you're working with and we might be able to help you find a better hook.
    – EAMann
    Jul 13, 2012 at 23:31
  • $wp->init(); do_action( 'init' ); // Check site status $file='http://taddy.co.uk/wp-admin/wp_includes/pluggable.php'; if ( is_multisite() ) { if ( true !== ( $file = ms_site_check() ) ) { require( $file ); die(); } unset($file); } // Get the current user's info $current_user = wp_get_current_user(); if ( !($current_user instanceof WP_User) ) return; echo $current_user->user_login; function paf_uname(){ return $current_user->user_login; }
    – PAFoster
    Jul 14, 2012 at 20:15
  • I know none of that is right, the 'init' bit i don't understand but was a suggestion from someone else. I was under the impression it did something like load a file - in my case pluggable.php - really all I need is just $current_user->user_login
    – PAFoster
    Jul 14, 2012 at 20:20

4 Answers 4

9

To add to @EAMann's answer, you need to wrap your wp_get_current_user() call (or any call that tries to access a function defined within pluggable.php) within the 'plugins_loaded' action.

So, if you're putting this inside your functions.php file, do it like this:

add_action( 'plugins_loaded', 'get_user_info' );

function get_user_info(){
  $current_user = wp_get_current_user(); 

  if ( !($current_user instanceof WP_User) ) 
    return; 

  echo $current_user->user_login;

  // Do the remaining stuff that has to happen once you've gotten your user info
}

Do note that we're not interested in what this function returns. We're interested in when this function executes, namely, after the pluggable.php file has loaded and defined your wp_get_current_user() function.

So, don't expect to do anything with the return value for this function. Instead, consider this function as the starting point for everything that you want to do once you've got the current user's info.

Doing it in a plugin

For the sake of completeness, here's how you would access a similar pluggable function from within the context of your own plugin:

(put this inside a .php file inside your plugins folder)

class WPSE_58429 {
    public function __construct(){
        add_action( 'plugins_loaded', array( $this, 'check_if_user_logged_in' ) );
    }

    public function check_if_user_logged_in(){
        if ( is_user_logged_in() ){
           // ... do stuff for your logged-in user
        }
    }
}

$wpse_58429_plugin = new WPSE_58429();

I've used this technique successfully for a very simple "Coming Soon" type of plugin that redirects the user to a specific page if they're not logged in using wp_safe_redirect().

2
  • how would you use a plugins_loaded action to run wp_safe_redirect() if an plugin's admin page outputs HTML before the header('Location: …') has a chance to run? I'm having a problem where a page defined using add_menu_page(…) outputs the WP header (and content and footer) when the plugin loads, well before plugins_loaded is run. I'd like to have access to functions inside pluggable.php (for accessing user data and doing redirects) but this seems impossible. Have you used add_menu_page with plugins_loaded? May 23, 2015 at 0:45
  • 1
    Never mind, it seemed impossible, but it was not—I had an error in my code. In fact the load order is exactly as expected: plugins are includedpluggable.php is included'plugins_loaded' is triggered'load-{page_hook}' is triggered'page_hook' is triggered May 23, 2015 at 1:33
4

The problem is that you're trying to load the code directly rather than with a WordPress hook. WordPress loads a bunch of code in a specific order (you can see the list of actions fired in a typical request in the Codex).

By trying to fire your code directly, you're executing just before pluggable.php is loaded. And you should not try to include() this file directly. Let WordPress do that for you.

Instead, define a function that gets the user information:

function wpse_58429() {
    // Get the current user's info 
    $current_user = wp_get_current_user(); 

    if ( !($current_user instanceof WP_User) ) 
        return; 

    return $current_user->user_login; 
}

You can then use this function anywhere in your theme without issue. For example:

echo wpse_58429();

If you need to use $current_user in other code, make sure you fire that code with a WordPress action ... don't call it directly or it will be executed before the function is available.

0

It looks like you're loading your code before certain functions are available. Have you tried:

 global $current_user; 
 //print_r($current_user); //all user related information
echo $current_user->ID; //get current user id 
1
  • 1
    Yes, but you can't get $current_user until wp_get_current_user() is available and since that function is in pluggable.php, it's not available until after the plugins are loaded. Hence the undefined function error.
    – PAFoster
    Jul 13, 2012 at 23:00
0

Simply add this function to your plugin .php file

function is_logged_in(){
    if(function_exists( 'is_user_logged_in' )) {
        return is_user_logged_in();
    }
}

Then call it anywhere you want to get user login status. For example:

echo is_logged_in();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.