• Resolved JesperT

    (@jespert)


    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?

Viewing 5 replies - 1 through 5 (of 5 total)
  • s0what

    (@s0what)

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

    Warren Harrison

    (@hungrymedia)

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

    Txanny

    (@txanny)

    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'));
      }
    }
    Warren Harrison

    (@hungrymedia)

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

    Thread Starter JesperT

    (@jespert)

    Tnx!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘current_user_can() in the __construct?’ is closed to new replies.