• Resolved carlla

    (@carlla)


    I’m getting a empty $current_user->ID value inside my plugin class.

    This plugin is suppose to update a user custom field (once) before show template content, and prints its content inside a page template.

    I check out the database and found a user_id=0 on wp_usermeta with my custom meta key

    So I tried to store the ID to debbug and always I get a empty value

    My code looks like it:

    <?php
    class My_class{
    
     var $out;
    
     function My_class() {
        add_action ( 'init', array( $this, 'my_function' ) );
     }
    
     function my_function() {
    
      global $current_user;
      $current_user = get_currentuserinfo();	
    
      // do stuffs
      // I need the current user at this point
      $this->out = $current_user->ID; 
    
      update_usermeta( $this->out, 'my_meta_key', $value );
    
      // after I'll set a cookie here
      // for while, I only want the user id
    
     } 
    
     // this will be called on template/theme
     public function show(){
       // check if the current user was found before
       echo $this->out; // it prints ""
    
       global $current_user;
       echo "*".$current_user->ID; // it works fine
    
     }
    } 
    
    $my_obj = new My_class();
    
    ?>

    Is there any rule to get $current_user inside a class?

Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘empty $current_user value in plugin class’ is closed to new replies.