• I am building my first plugin in OOP, for a couple of things I need to get the users ID(roles etc) so I need the users data inside the whole class, I dont want to include the puggable.php file so I need some help on this.

    the code(basic overview)

    <?php
    
    	class WP_PM{
    
    		public $mainnuser;
    
    		public function __construct(){
    
    			//the idea
    			global $current_user;
    			$this->mainnuser = $current_user->ID;
    
    			$this->addActions();
    			$this->someUserStuff();
    		}
    
    		private function addActions(){
    			add_action('wp_loaded', array($this, 'getCid'));
    			add_action('admin_menu', array($this, 'setPages'));
    
    		}
    
    		public function getCid(){
    			global $current_user;
    
    			echo $current_user->ID;//working
    
    			$this->mainuser = $current_user->ID;// settings but no value outside this function
    			//$this->mainuser = 100;// this will return an value of 100
    
    		}
    
    		public function setPages() {
    
    			echo $current_user->ID;//NOT working
    			echo $this->mainuser;//NOT working
    
    			// the idea on how to use
    			if($mainnuser == 'administrator'){
    				add_menu_page();//dummy
    			}else{
    				// do nothing
    			}
    		}
    
    		public function someUserStuff() {
    
    			echo $current_user->ID;//NOT working
    			echo $this->mainuser;//NOT working
    
    			// the idea on how to use
    			if($mainnuser == 'administrator'){
    				echo 'adminnininistratoooor';
    			}else{
    				// do nothing
    			}
    		}
    	}
    
    	$wppm = new WP_PM();
    
    ?>
  • The topic ‘Plugin: How to get logged in userdata inside a class’ is closed to new replies.