Wednesday, December 7, 2022
HomeWordPress DevelopmentWorking perform() does not execute when triggered by WP CRON

Working perform() does not execute when triggered by WP CRON


I’ve constructed the next earlier than and have referenced my very own code to see if I missed something and from what I can inform I’ve not.

I’ve a WP CRON scheduled and it executes because the scheduled intervals. Once I run CRONS I at all times file their ‘final run’ time as an possibility within the DB so I can at all times bounce in and test if it is operating. So I do know it is operating. I’ve additionally put in WP Crontrol in the meanwhile so I can manually set off the CRON each time I would like.

So, I’m 100% assured the WP CRON is working.

I additionally then have a perform that queries a customized post-type on the lookout for entries flagged that they want a ‘reminder’ despatched. I then run an SQL question in opposition to a customized desk that retrieves ‘assignments’ that match the CPT and tomorrow’s date. For every of those, an e mail is shipped.

The perform works. If I name it wherever else on the location, it executes. If I simply drop it in an add_action() like add_action( 'wp_footer', 'my_reminder_emails', 100 ); it would correctly execute and it’ll ship me the entire acceptable e mail reminders. (In the intervening time I’ve all of the emails coming to me moderately than the assignees.)

The place all of it breaks down is throughout the WP CRON. The WP CRON runs as scheduled, it updates it is final run time stamp after which… …nothing.

HTTP Authentication is enabled. As an alternative of ready for visitors on the event server I am utilizing WP Crontrol to fireside the WP CRON. If I am affected person and simply refresh the location each 5 minutes myself, I can see the WP CRON is operating. However no emails are despatched.

The positioning can ship emails, that is not the problem. If I set off the emailer perform in ANY different means, it really works. It solely does not work when the WP CRON triggers it.

I’ve even taken the code from inside my perform and included it within the WP CRON (moderately than having a separate perform to name) and that additionally does not work.

This is the WP CRON set-up:

perform custom_cron_deactivate() { 
    $timestamp = wp_next_scheduled( 'custom_cron_hook' );
    wp_unschedule_event( $timestamp, 'custom_cron_hook' );
} 
register_deactivation_hook( __FILE__, 'custom_cron_deactivate' );
perform custom_cron_activation() {
    if( !wp_next_scheduled( 'custom_cron_hook' ) ) :  
        wp_schedule_event( time(), 'everyfiveminutes', 'custom_cron_hook' );  
    endif;
}
add_action( 'wp', 'custom_cron_activation' );

perform custom_cron_exec() {
    date_default_timezone_set( 'America/Los_Angeles' );
    $current_time       = date( 'F j, Y, g:i a' );
    update_option( 'customCRON_last', strtotime( $current_time ) );
    //run duties
    custom_reminders_cron_task();
}
add_action( 'custom_cron_hook', 'custom_cron_exec' );

That is the executable perform (I’ve eliminated among the fundamental ‘Question’ stuff which I do know works.

perform custom_reminders_cron_task() {
    $right now      = date( 'Y-m-d' );
    $tomorrow   = strtotime( date( 'Y-m-d', strtotime( $right now . ' +1 day' ) ) );
    $rems_args  = array(
        'post_type'         => 'custom_position',
        'posts_per_page'    => -1,
        'post_status'       => 'publish',
        'fields'            => 'ids',
        'meta_query'        => array(
            array(
                'key'               => 'custom_send_reminder',
                'worth'             => 1,
                'examine'           => '=',
                'sort'              => 'NUMERIC',
            ),
        ),
    );
    $rems   = get_posts( $rems_args );
    $remslist = implode( ',', $rems );
    international $wpdb;
    $custom_table       = $wpdb->prefix . 'custom_assignments';
    $rems_prepare   = $wpdb->put together(
        "SELECT * FROM {$custom_table} WHERE `sdate ` = %d AND `posid` IN (%d)",
        $tomorrow, $remindlist
    );
    $rems_results   = $wpdb->get_results( $rems_prepare );
    foreach( $rems_results as $send_rem ) :
        $topic="Automated Project Reminder";
        $udata      = get_userdata( $send_rem->user_id );
        $uemail="myemail@tackle.com";
        $uname      = $udata->first_name . ' ' . $udata->last_name;
        $physique       = '<h3>Customized Project Reminder</h3><p>' . $uname . ', <br/><br/>you may have been assigned <robust>' . get_the_title( $send_rem->posid ) . '</robust> for ' . date( 'F j, Y', $send_rem->sdate ) . '.<br/><br/><small>That is an routinely generated reminder - please don't reply.</small></p>';
        $headers    = array(
            'Content material-Sort: textual content/html; charset=UTF-8',
            'From: Customized <no-reply@area.com>'
        );
        wp_mail( $uemail, $topic, $physique, $headers );
    endforeach;
}

As I mentioned, each particular person piece works. Simply not together.

Any assist can be enormously appreciated.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments