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 am trying to get the current logged-in user data and use this in the parent class(I need it in several functions).

In the example below I can get the userID in the first function but it will give no output inside the next function.

This is my first wordpress plugin and the first time that I work with OOP, so who can help me out with this thing.

<?php

class WP_PM{

    public $currentId;

    public function __construct(){

        $this->addActions();
        $this->showId();
    }

    private function addActions(){
        add_action('wp_loaded', array(&$this, 'getId'));
    }

    public function getId(){
        global $current_user;

        $this->currentId = $current_user->ID;

        //$this->currentId = 'hello';//we have an output

        echo $this->currentId;//we have an output

        return $this->currentId;
    }

    public function showId(){
        echo $this->currentId;//no output
            echo $this->getId();//no output
    }

}

register_activation_hook(__FILE__, array('WP_PM', 'activate'));
register_deactivation_hook(__FILE__, array('WP_PM', 'deactivate'));

$wp_pm = new WP_PM();
?>

// new example based on G.M solution(still not working -> triggers wp error and no logged in text)

class WP_PM_Extend extends WP_User {

    function getID() {
        return $this->ID;
    }

}

class WP_PM{

    protected $user;

    public function __construct(WP_PM_Extend $user = NULL){

        if (!is_null( $user ) && $user->exists()){
            $this->user = $user;
        }

        $this->getUser();

    }

    public function getUser() {
        return $this->user;
    }

}


function getWPPM() {

    if(!did_action('wp_loaded')){
     $msg = 'Please call getCurrentUser after wp_loaded is fired.';
    return new WP_Error('to_early_for_user', $msg);
    }

    static $wp_powertour_core = NULL;

    if(is_null( $wp_powertour_core)){
        $wp_pM = new WP_PM( new WP_PM_Extend( get_current_user_id() ) );
    }

    return $wp_powertour_core;
}

function getCurrentUser(){

  $wppm = getWPPM();

    if(is_wp_error($wppm)){
        return $wppm;
     }

    $user = $wppm->getUser();

    if($user instanceof WP_PM_Extend){ 
        return $user;
    }
}

add_action( 'wp_loaded', 'getCurrentUser');

$current_user = getCurrentUser();
if($current_user instanceof WP_PM_Extend){
    echo 'Current user ID: ' . $current_user->getID();
}else{
    echo 'No one logged in';
}
share|improve this question
    
I see lots of problems. I am going to tackle the constructor. Plugins are loaded very early, before a user is authenticated. Calling showId() (in constructor) which tries to print out current_user info wont work. getId() works because it is called at 'wp_loaded' which is after the user authentication process. Note that printing out information in the plugin could disrupt WP by interferring its normal output... for example if WP or plugin tries to update http headers. –  user42826 Feb 16 at 18:21

3 Answers 3

up vote 2 down vote accepted

Once you said is you first time using OOP, I want to say: stop using & before $this: PHP4 died long time ago.

Second tip, don't use global variables, if you can. I know that function like wp_get_current_user use global variable internally, but I hope that in future it will not be so anymore, however not seeing that global word in my code, make me fill a little better.

Third tip, try to make your plguin more solid using type hinting.

Here I post how I had wrote that code.

Firts of all I'd wrote 2 classes, probably in 2 different files named in same way of class:

class WP_PM_User extends WP_User {

  function getID() {
    return $this->ID;
  }

}

class WP_PM {

  protected $user;

  function __construct ( WP_PM_User $user = NULL) {
    if ( ! is_null( $user ) && $user->exists() ) $this->user = $user;
  }

  function getUser() {
    return $this->user;
  }

}

After that, somewhere in plugin (maybe in main plugin file) I'd write two function like this:

function getWPPM() {

  if ( ! did_action('wp_loaded') ) {
    $msg = 'Please call getCurrentUser after wp_loaded is fired.';
    return new WP_Error( 'to_early_for_user', $msg );
  }

  static $wp_pm = NULL;

  if ( is_null( $wp_pm ) ) {
    $wp_pm = new WP_PM( new WP_PM_User( get_current_user_id() ) );
  }

  return $wp_pm;
}


function getCurrentUser() {

  $wppm = getWPPM();

  if ( is_wp_error( $wppm ) ) return $wppm;

  $user = $wppm->getUser();

  if ( $user instanceof WP_PM_User ) return $user;
}

add_action( 'wp_loaded', 'getCurrentUser' );

Doing so, everywhere in your plugin you can call getCurrentUser and retrieve the current user object, and if you want the ID, you can call getID() on the user returned by getCurrentUser.

Usage example:

add_action( 'wp_loaded', function() {

  $current_user = getCurrentUser();
  if ( $current_user instanceof WP_PM_User ) {
    echo 'Current user ID: ' . $current_user->getID();
  } else {
    echo 'No one logged in';
  }

}, 30 );
share|improve this answer
    
Thanks. I have to take some time to read this ;-) –  user759235 Feb 16 at 18:31
    
not sure if it's me but I copyed your code but i get an error <pre><code>Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'getCurrentUser' not found or invalid function name in xxxxxxxx\wp-includes\plugin.php on line 429</code></pre> –  user759235 Feb 16 at 18:44
    
the error was my fault, I forgot the add the add_option, but some reason the class is called twice, showing all of the code twice. I have added your example to top post. –  user759235 Feb 16 at 22:09
    
Okay so I couldn't find the issue last night but I have found the error you forgot to use of 'this' getCurrentUser() should be $this->getCurrentUser()....but no the example echo's out the 'o one logged in'.. –  user759235 Feb 17 at 12:33
    
Yes I now see why I need the this, as I am using the code inside the Class, I dont want to use any thing outside my classes. –  user759235 Feb 17 at 20:42

You are firing both $this->getId(); and $this->showId(); in the constructor. I am pretty sure that that is the problem.

If this is a plugin, which it looks like it is since you are using activation hooks, you should be able to do this:

public function getId(){
    global $current_user;
    get_currentuserinfo();

    $this->currentId = $current_user->ID;

    //$this->currentId = 'hello';//we have an output

    echo $this->currentId;//we have an output

    return $this->currentId;
}

And have things work after that.

If you instantiate in other contexts, you need to be careful what hook it instantiates on. Too early and neither $current_user nor get_currentuserinfo() will be defined.

For example,

add_action(
  'muplugins_loaded',
  function() {
    global $wp_pm;
    $wp_pm = new WP_PM();
  }
);

... will give you a notice about an undefined function-- get_currentuserinfo().

share|improve this answer
    
the getId should not be in the constuctor thats right(not present in the real example). But I still dont get an output in the other function... –  user759235 Feb 16 at 16:57

You are definitely loading your class instance early.

First solution could be loading the class this way -

add_action( 'init', 'load_wp_pm' );
function load_wp_pm(){
    global $wp_pm;
    $wp_pm = new WP_PM();
}

Another way it can be accomplished by modifying __contruct and adding another method -

// modified the construct method
public function __construct(){
    add_action( 'init', array( get_class(), '_init' ) );
}

// getting user id & setting the $currentId variable and 
public function _init(){
    $this->addActions();
    $this->showId();
}

This would probably show you some output.

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.