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';
}
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 thatglobal
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:
After that, somewhere in plugin (maybe in main plugin file) I’d write two function like this:
Doing so, everywhere in your plugin you can call
getCurrentUser
and retrieve the current user object, and if you want the ID, you can callgetID()
on the user returned bygetCurrentUser
.Usage example:
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:
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
norget_currentuserinfo()
will be defined.For example,
… will give you a notice about an undefined function–
get_currentuserinfo()
.You are definitely loading your class instance early.
First solution could be loading the class this way –
Another way it can be accomplished by modifying
__contruct
and adding another method –This would probably show you some output.