0

I have a function in functions.php

function test(){
   do_action('before_test_action');
   echo 'in test';
}

Then within my Widget, in the widget( $args, $instance ) function i use:

$boolean = (isset($instance['boolean'])) ? $instance['boolean'] : '';
if ($boolean == 'on'){
   add_action('before_test_action', array($this, 'widget_test_func'));
}

After widget() function i have the widget_test_func function:

public function widget_test_func(){
   echo 'within widget test func';
}

The action does not run and i don't see anything on the browser. If i add the add_action within the widget constructor it will work but i need to make sure it runs if the $boolean == 'on'

1 Answer 1

1

Not really following how the functions relate to each other and where (so what variables are available, how), but you might try something like:

add_action('before_test_action', array($this, 'widget_test_func'));

public function widget_test_func( $instance) {

    $boolean = (isset($instance['boolean'])) ? $instance['boolean'] : '';

    if ($boolean == 'on') {

        echo 'within widget test func';

    }

}

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.