最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

WP action is not triggering a do_action() call within?

programmeradmin3浏览0评论

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?

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?

Share Improve this question asked Mar 26, 2020 at 20:56 yesbutmaybenoyesbutmaybeno 1655 bronze badges 1
  • Mirrored WordPress Support Thread – Howdy_McGee Commented Mar 26, 2020 at 21:01
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论