0

I am currently developing a plugin and I am trying to make use of the $current_user global.

class Something {
  public function __construct() {
    $this->get_user();
    var_dump( $this->user );
  }

 private function get_user() {
    require_once( ABSPATH . '/wp-includes/pluggable.php' );
    $this->user = wp_get_current_user();
  }
}

This actually works. However, I need to call pluggable.php file which shouldn't be necessary. I have also tried calling the global $current_user variable to no avail: it always returns NULL... unless of course I import pluggable.php again - like so:

private function get_user() {
  require_once( ABSPATH . '/wp-includes/pluggable.php' );
  global $current_user;
  get_currentuserinfo();
  $this->user = $current_user;
}

This might be a potential duplicate of: $current_user var returns NULL

And I've tried all the solutions, but still need to import pluggable.php no matter what.

Seems like the author of the other thread found a solution that did not share.

Anybody else ever have this issue? Thanks.

1 Answer 1

5

Wait for the action plugins_loaded before you create the class instance. The pluggable functions are loaded at this time. From wp-settings.php:

/**
 * Fires once activated plugins have loaded.
 *
 * Pluggable functions are also available at this point in the loading order.
 *
 * @since 1.5.0
 */
do_action( 'plugins_loaded' );

I would even wait for wp_loaded in most cases. Then the global WP_Roles object has been set up, you know the theme and the locale – you will very likely not run again into a problem because of missing information.

Never just create class instances when your plugin's main file is loaded. This always too early. Usually you want to check the request first to exclude your code from slowing down other plugins AJAX requests or WP's comment/XML RPC/feed processing.

So your plugin's main file could look like this:

add_action( 'wp_loaded', [ new Something, 'setup' ] );
4
  • Hi toscho, I'm not sure how I would go about integrating your solution. I'm somewhat confused since I have three classes, one of which is calling that 'Something' class. To be more specific, I have a init.php which calls the manager class (new Manager) then calls the Something class within that constructor. Jul 6, 2015 at 2:00
  • 2
    @mousesports See my update for an example. Never just create classes when your plugin file is loaded. This always too early. Usually you want to check the request first to exclude your code from slowing down other plugins AJAX requests or WP's comment/XML RPC/feed processing.
    – fuxia
    Jul 6, 2015 at 2:22
  • Thanks toscho for taking the time to answer. I figured based on my reading that that was the case where it was being called too early. However, I tried to implement what you said and still getting the same results. I wish I knew the answers to all OOP in WordPress, having a hard time refactoring and making my plugin clean as a baby's bum :) Would you mind taking a look? You can find the Git branch at github.com/chrisberthe/wordpress-database-reset/tree/… . Much appreciated if you can. Jul 6, 2015 at 14:06
  • Nevermind toscho. I understand what you mean now - it works great. Thanks so much for your help. Jul 6, 2015 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.