Skip to content
Advertisement

wp_schedule_single_event hook not firing

I have a custom event scheduled for an admin HR system in WordPress. I need to schedule expiry warnings for uploaded documents but my hook isn’t firing.

    function do_this_in_a_minunte($args) {
        $to = "example@outlook.com";
        $subject = 'New expiry email';
        $body = "Hello " . $args;
        $headers = array('From: ' . get_bloginfo ( 'name' ) . ' <' . get_bloginfo ( 'admin_email' ) . '>', 'Content-Type: text/html; charset=UTF-8');
        wp_mail( $to, $subject, $body, $headers );
        print_r($args);
    }
    add_action( 'my_new_event', 'do_this_in_a_minunte', 1, 2 );      
    $args = array(array('email' => 'test', 'title' => 'hello'));
    wp_schedule_single_event( time() + (60 * 1), 'my_new_event', $args);
    wp_schedule_single_event( time() + (60 * 2), 'my_new_event', $args);
    wp_schedule_single_event( time() + (60 * 3), 'my_new_event', $args);

The jobs get added to the scheduler correctly but no emails are ever sent. The site sends a debug email before this block so I know it can send emails.

enter image description here

I cannot debug the site locally annoyingly and the logs are showing nothing of interest. Any help appreciated.

Advertisement

Answer

I worked it out and it was horrible but I’m going to answer my own question.

The code was all inside an Elementor custom form action.

public function run( $record, $ajax_handler ) {
function do_this_in_a_minunte($args) {
        $to = "example@outlook.com";
        $subject = 'New expiry email';
        $body = "Hello " . $args;
        $headers = array('From: ' . get_bloginfo ( 'name' ) . ' <' . get_bloginfo ( 'admin_email' ) . '>', 'Content-Type: text/html; charset=UTF-8');
        wp_mail( $to, $subject, $body, $headers );
        print_r($args);
    }
    add_action( 'my_new_event', 'do_this_in_a_minunte', 1, 2 );      
    $args = array(array('email' => 'test', 'title' => 'hello'));
    wp_schedule_single_event( time() + (60 * 1), 'my_new_event', $args);
    wp_schedule_single_event( time() + (60 * 2), 'my_new_event', $args);
    wp_schedule_single_event( time() + (60 * 3), 'my_new_event', $args);
}

WordPress “cron tasks” don’t run unless someone hits the site so I’ve moved the code outside of the class method and into the functions.php file so when the job runs when someone hits the site and the queue gets processed the hook to send the email actually exists and can be ran.

add_action( 'one_day_before_event', 'some_function', 10, 2 );

function some_function($post_id, $user_id) {
    error_log("some_function firing");
    $to = "test@outlook.com";
    $subject = 'TEST';
    $body = $post_id . " - " . $user_id;
    $headers = array('From: ' . get_bloginfo ( 'name' ) . ' <' . get_bloginfo ( 'admin_email' ) . '>', 'Content-Type: text/html; charset=UTF-8');

    wp_mail( $to, $subject, $body, $headers );
}

The answer is make sure your hook exists outside the class method you’re setting the task up otherwise it doesn’t exist when it needs to be ran.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement