WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm learning to create a WP plugin as a class. However, it seems the add_action doesn't work when calling my function; it works when using $this->init().


    class test

    public $age = NULL;

    public function __construct()
        //register an activation hook
        register_activation_hook( __FILE__, array( &$this, 'installplugin' ) );

        add_action('init', array( &$this, 'init' ) ); //this doesn't work
        //$this->init(); //This WORKS!


    function installplugin()

        add_option( 'age', 45 );


    function init()

        $age = get_option( 'age' );
        $this->age = $age;


    function printAge(  )
        echo "The age is " . $this->age . "<br />";


So, after running:

$learnObj =  new learnable_test();


It only prints: "The age is "

But if I were t comment out the add_action('init',...) and use the $this->init(), that seems to work, printing "The age is 45"

What am I missing?

share|improve this question
You are sure that when you call $learnObj->printAge(); 'init' action was already done, aren't you? – gmazzap Jul 31 '13 at 4:30
If it was already done, then shouldn't $this->age be initialize with the value 45? Then when I called printAge(), the value of $this->age should already be set, right? – tester2001 Jul 31 '13 at 4:33
where do you create your class instance: $learnObj = new learnable_test();? – Milo Jul 31 '13 at 4:43
'init' action does not mean "class inizialization", it is an action performed by wordpress after the theme is included and after all the stuff under after_setup_theme hook are done. If this is a plugin (and so it seems) you can replace add_action('init' with add_action('plugins_loaded' that is performed before. Better you can: $learnObj = new learnable_test( get_option('age') ); and in the class public function __construct($age) { $this->age = $age;. Doing so init method is not needed... – gmazzap Jul 31 '13 at 4:44
Also don't use &$this unless you're stuck in a time machine. – Wyck Jul 31 '13 at 4:57
up vote 4 down vote accepted

Well it can't and won't work like you want it to.

Let's look at what exactly are you doing.

Inside your constructor you have:

add_action('init', array( &$this, 'init' ) ); //this doesn't work

So you add init method of your object to WPs init hook. When WP runs it's init action, then your init method will be run to, but not sooner and not later.

Then you do something like this:

$learnObj =  new learnable_test();


So you create object of your class. It will add $learnObj->init to WPs init hook.

If you call these 2 lines after WPs init hook was already done, nothing will happen.

If you call them before (I guess you do) - your $learnObj->init will be executed during WPs init action (and age variable will be set).

But in the second line you want to access this age variable. It won't be set here yet, because WP haven't yet executed it's init action, thus your $learnObj->init hasn't been executed too.

share|improve this answer
In short: call $learnObj = new learnable_test(); before 'init' is done and $learnObj->printAge(); after 'init' is done. :) – gmazzap Jul 31 '13 at 5:26

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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