Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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; 
}
share|improve this question
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 '12 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 '12 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 '12 at 20:20

3 Answers 3

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.

share|improve this answer

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

global $current_user; echo $current_user->ID;

share|improve this answer
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 '12 at 23:00

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().

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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