• I am trying to schedule a function to run immediately in the background. I can schedule the hook, but there’s no function attached to it!?

    The function works. I’ve successfully called it but it’s slow so I want it to run in the background.

    Here’s the code I’m using to schedule it.

    function rmi_delete_all_providers(){ 
    //stuff
    }
    add_action('rmi_bg','rmi_delete_all_providers',10,0);
    wp_schedule_single_event( time(), 'rmi_bg' );
    spawn_cron();

    I’ve run this with a delay so I can check it in WP Crontrol. The hook is there, but there is no associated action. I’ve also tried the add_action without the optional values.

    Any ideas why I can’t get this function to properly attach to the hook?

    • This topic was modified 3 years, 1 month ago by t-p. Reason: Moved to Fixing Wordpress from Developing with WordPress
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Event timing isn’t very precise. I think building in some sort of delay is needed for reliable execution. By trying to execute the event immediately, I think you’ve introduced a race condition where the cron is spawned before WP can finish scheduling the event. By scheduling several minutes into the future, any subsequent request will execute the action.

    If there’s any chance of scheduling multiple single events in a short time frame, heed the note in the scheduling function’s doc page description about unique args.

Viewing 1 replies (of 1 total)
  • The topic ‘add_action not adding action to hook’ is closed to new replies.