0

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().

Example:

    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 );
        $this->init();  

    }

    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();

$learnObj->printAge();

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?

5
  • You are sure that when you call $learnObj->printAge(); 'init' action was already done, aren't you?
    – gmazzap
    Jul 31, 2013 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, 2013 at 4:33
  • 2
    where do you create your class instance: $learnObj = new learnable_test();?
    – Milo
    Jul 31, 2013 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, 2013 at 4:44
  • 2
    Also don't use &$this unless you're stuck in a time machine.
    – Wyck
    Jul 31, 2013 at 4:57

1 Answer 1

5

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();

$learnObj->printAge();

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.

1
  • 2
    In short: call $learnObj = new learnable_test(); before 'init' is done and $learnObj->printAge(); after 'init' is done. :)
    – gmazzap
    Jul 31, 2013 at 5:26

Your Answer

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

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