• Hey all,

    Quick foundation, there is a plugin that upon the admin uploading a file to the database fires off an action. (If it matters, this do_action is in a static function in the plugin’s files.)

    I hook into this with an add_action which performs some logic. Within this logic, I fire off my own CUSTOM do_action (to send a user an email). It appears this do_action never gets triggered, I can’t figure out why?

    Here’s the basic structure of the code:

    Plugin class

    static function onInsertFile() {
        // logic...
        do_action('file-edited', $file, $data);
    }

    My custom plugin class

    public function register() {
        add_action( 'file-edited', array( $this, 'onFileEdited' ), 10, 2 );
        add_action( 'send-user-email', array( $this, 'sendUserEmail' ), 10, 1 );
    }
    
    public function onFileEdited( $file, $data ) {
        // logic...
        do_action( 'send-user-email', $email_stuff );
    }
    
    public function sendUserEmail( $args ) {
        // Code here is never executed???
    }

    There must be some type of gotcha here going on because this all seems solid to me? Anything to do with static scopes?

    (For the sake of this example, and testing, both add_actions are in the same class yet that send-user-email one is still never executed.)

Viewing 5 replies - 1 through 5 (of 5 total)
  • I’m not sure anything is wrong with the code necessarily but maybe the order in which you expect things to get executed. Let’s break this down into a simple format, removed from any class definition.

    The action are added in init ( similar to yours are added via register which means anything that happens before init is going to fall out of order and not run the actions.

    function register() {
    	
            // Action is added before the function call, this will fire eventually.
    	add_action( 'send-user-email', 'sendUserEmail' );
    	
           // Function called
    	onFileEdited();
    	
    }
    add_action( 'init', 'register' );
    
    function onFileEdited() {
            
           // Check to see if there's actions added to this custom hook, run them.
    	do_action( 'send-user-email' );
    }
    
    function sendUserEmail() {
    	die( 'here' );
    }

    The init both adds the hook before any of the function calls. If we call onFileEdited() in the above example before add_action(), the do_action() functions will not run. If we call onFileEdited() in the above example outside init action, the do_action() functions will not run.

    function register() {
    	
    	onFileEdited(); // This will run first
    
            // The action is added _after_ our function has run and will not fire.
    	add_action( 'send-user-email', 'sendUserEmail' );
    	
    }
    add_action( 'init', 'register' );

    Another example:

    onFileEdited(); // This will run first
    
    function register() {
    
    	// The action is added _after_ our function has run and will not fire.
    	// This action will not fire in either of the above or below cases.
    	add_action( 'send-user-email', 'sendUserEmail' );
    
    }
    add_action( 'init', 'register' );
    
    onFileEdited(); // This will run second

    WordPress will run the functions as they are called but special positions such as init or other “load” hooks will run later. In the above case, PHP see that this is a direct action call and calls the function, then INIT gets added to queue ( but does not fire yet ) then it runs across the 2nd function call, fires it immediately, then continues with WordPress load process until the init function gets called THEN the contents get added and fired in order or priority.

    In short, it comes down to when your classes register is being fired, when those actions are being added, and when you expect them to run during the WordPress processing.

    Thread Starter FTLRalph

    (@ftlralph)

    @howdy_mcgee oof sorry for the late reply, I really appreciate the time you took to try to help me understand this

    so I’m reading over this, and really trying to understand but, you decoupled it from the class structure I got going and I can’t really apply it logically for a fix because, things seem pretty correct?

    here is the scenario in a simple example, I hope:

    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?

    EDIT: 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?

    • This reply was modified 4 years, 8 months ago by FTLRalph.
    • This reply was modified 4 years, 8 months ago by FTLRalph.

    Hello,

    Try running the above examples yourself to see how they work. Maybe decoupling it from your plugin to try and understand the ideas of how hooks work and when they fire might be better. Once you have it working outside your class and understand the hook adding/firing process then you can add it back into your class. Having it in the class simply add another layer to debug/manage, nothing more.

    I think the issue is either in the order hooks are being added or the order in which hooks are firing.

    Thread Starter FTLRalph

    (@ftlralph)

    @howdy_mcgee (I don’t know if I have to @ for you to see reply lol)

    thanks again

    even if this is the case, I don’t see how it would help in the end, I need the class structure, my entire plugin is structured this way-

    the example above is pretty non-class-based and it’s exactly the situation it is now

    the ” has_action( ‘my_action2’ ) ” is the kicker for me because that shows it SEES the action exists during runtime, but is just not calling the function hooked to it? I can’t understand how both of those can be a possibility

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    @ftlralph To be blunt about it, what you’re saying doesn’t make a lot of sense, and it’s difficult to determine your problem when you’re only posting examples and not even in real code.

    So, here’s an example in real code:

    
    <?php
    include 'wp-load.php';
    
    function test() {
    	do_action('test1');
    	do_action('test2');
    }
    
    function setup_actions() {
    	add_action('test1', 'func1');
    	add_action('test2', 'func2');
    }
    
    function func1() {
    	echo "output from func1\n";
    	do_action('test2');
    }
    
    function func2() {
    	echo "output from func2\n";
    }
    
    setup_actions();
    test();
    

    Now, that should be clear enough to anybody. It’s fairly straightforward and mimics your code. If you run php on this file, this is the output:

    
    output from func1
    output from func2
    output from func2
    

    As expected. So, whatever problem you’re having is not conceptual, it is a specific problem in your code. Without seeing the specific code, we probably cannot help you any further.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Custom do_action seems to not be calling?’ is closed to new replies.