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.

I can't get the user variable from the main class loading a "child class" example:

//PLUGIN FILE
class father{

    var $user;

    function __construct() {
        add_action('plugins_loaded', array(&$this, 'loaded'));
    }

    function plugins_loaded(){
        global $wp_get_current_user;
        $this->user = wp_get_current_user();
    }
}

$plugin = new parent();

That was the plugin file.

//EXTEND CLASS
class child extends father{

    function __construct(){
        parent::__construct();
    }

    function user_id(){
        echo $this->user->ID;
    }
}

That was the extend class.

//CONFIG FILE (DISPLAYED IN ADMIN PANEL)
$child = new child();
$user_id = $child->user->id;
$child->user_id();

And that was the config page.

I can't get the user id in the extended class, but yes in the father class.

Why and how i can solve it?

share|improve this question
    
I assume that 'class child extends parent' should have read 'class child extends father'? –  Dougal Campbell Jun 23 '11 at 13:00

1 Answer 1

up vote 1 down vote accepted

This works for me:

class father {
    var $user;
    function __construct() {
        add_action( 'init', array( &$this, 'set_user' ) );
    }
    function set_user() {
        $this->user = wp_get_current_user();
    }
}

class child extends father {
    function __construct() {
        parent::__construct();
    }
    function user_id(){
        return $this->user->ID;
    }
}

$father = new father();
$child = new child();

add_action( 'admin_notices', 'test_stuff' );
function test_stuff() {
    global $child;
    print '<pre>Child: ' . print_r( $child->user_id(), true ) . '</pre>';
}
share|improve this answer
    
Yeah but if child had a function that uses $this->user->ID it return an empty value. Code edited with example. –  Sein Oxygen Jun 23 '11 at 14:01
    
I just updated my answer. I'm not having a problem with the code. Perhaps 'plugins_loaded' is too early to be getting the user data? 'init' works fine for me. –  mfields Jun 23 '11 at 14:19
    
Are you sure? because wp_get_current_user() is in pluggable.php and it is loaded once all plugins are loaded so it should throw an error on init. –  Sein Oxygen Jun 23 '11 at 14:33
    
Loading plugins is one of the first things that WordPress does ... init fires after the set_current_user hook. plugins_loaded fires before set_current_user. See: codex.wordpress.org/Plugin_API/Action_Reference –  mfields Jun 23 '11 at 14:37
    
Ahhh i found why. I was creating the instance of child on the settings page. For some reason that doesn't work there. But i created the instance in the plugin file and the used global in the settings page, that worked. Still that give me a bad taste in my mouth...i'm not too happy with the solution. –  Sein Oxygen Jun 23 '11 at 14:40

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.