2

Okay, so I have a plugin on my site that triggers a do_action().

In a second plugin, my custom plugin, I hook into this action and end up calling a do_action() for another custom action of my own. This second action is never firing, but...I can't see why the code doesn't "see" it?

Here is a simple example:

OTHER PLUGIN, the trigger

static function doSomething()
{
  do_action( 'my_action_1' );
  do_action( 'my_action_2' );
}

MY PLUGIN

construtor
{
  add_action( 'my_action_2', myAction2 );
  add_action( 'my_action_1', myAction1 );
}

myAction1()
{
  print( 'print from action1' );
  do_action( 'my_action_2' ); // this is the call that never executes
}

myAction2()
{
  print( 'print from action2' );
}

Output as it is now:

print from action1
print from action2

What it SHOULD be…because myAction1() calls do_action for myAction2():

print from action1
print from action2
print from action2

For some reason, action1 cannot “see” action2 as if it doesn’t exist. But, I don’t know how to make it exist any more than it does?

Bonus points, adding has_action( ‘my_action2‘ ); under the line commented up above produces 1 when the add_action() exists. If I comment it out, it is 0. So it KNOWS the action/hook is there, it’s just not firing?

0

I've taken you example and reduced it to this functioning test case:

class ClassOne {
    public static function do_something() {
        do_action( 'my_action_1' );
        do_action( 'my_action_2' );
    }
}

class ClassTwo {
    function __construct() {
        add_action( 'my_action_2', [ $this, 'my_action_2' ] );
        add_action( 'my_action_1', [ $this, 'my_action_1' ] );
    }

    public function my_action_2() {
        print "My action 2";
    }

    public function my_action_1() {
        print "My action 1";
        do_action( 'my_action_2' );
    }
}

$class_two = new ClassTwo();

ClassOne::do_something();

When this code runs, the output is exactly what you would expect:

My action 1My action 2My action 2

So whatever issue there is with your plugin is specific to the actual implementation in your plugin and the other plugin. Firing actions the way you've described works fine, as I've demonstrated.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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