最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How do I call wp_get_current_user() in a plugin when plugins are loaded before pluggable.php?

programmeradmin1浏览0评论

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='.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; 
}

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/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 edited Jul 16, 2012 at 15:32 EAMann 32.2k9 gold badges88 silver badges147 bronze badges asked Jul 13, 2012 at 20:54 PAFosterPAFoster 1111 gold badge1 silver badge3 bronze badges 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 Commented 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 Commented 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 Commented Jul 14, 2012 at 20:20
Add a comment  | 

4 Answers 4

Reset to default 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().

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.

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 

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();
发布评论

评论列表(0)

  1. 暂无评论