WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] current_user_can() in the __construct? (6 posts)

  1. JesperT
    Member
    Posted 4 years ago #

    When I use current_user_can() in the __construct function in my plugin I'll get this error: Fatal error: Call to undefined function wp_get_current_user() in C:\Inetpub\WP\wp-includes\capabilities.php on line 969

    class myPlugin {
        public function __construct() {
            if(current_user_can('manage_options'))
            add_action("publish_post",array(&$this,'save_post_form'));
        }
    }

    Is it not possible to run the current_user_can() in the __construct-funktion?

  2. s0what
    Member
    Posted 4 years ago #

    same problem for me. Maybe becouse of new WP version?

  3. Warren Harrison
    Member
    Posted 4 years ago #

    I just ran into the same problem today. Seems this function is not working in the latest WP version (2.9.2)

  4. Txanny
    Member
    Posted 4 years ago #

    You cannot run this on the plugin load. I think you are instantiating the class as soon as the plugin loads.

    wp_get_current_user() is a pluggable function. This means that can be replaced by plugins. Because of that, this functions library is only available after plugins are loaded.

    Try calling it on the 'plugins_loaded' action hook:

    class myPlugin {
      public function __construct () {
        add_action('plugins_loaded', array($this, 'pluginInit');
      }
    
      public function pluginInit () {
        if(current_user_can('manage_options'))
          add_action("publish_post",array(&$this,'save_post_form'));
      }
    }
  5. Warren Harrison
    Member
    Posted 4 years ago #

    Aha! Good to know. That solved my issue. Thank you.

  6. JesperT
    Member
    Posted 4 years ago #

    Tnx!

Topic Closed

This topic has been closed to new replies.

About this Topic