I've built the following before and have referenced my own code to see if I missed anything and from what I can tell I have not.
I have a WP CRON scheduled and it executes as the scheduled intervals. When I run CRONS I always record their 'last run' time as an option in the DB so I can always jump in and check if it's running. So I know it's running. I've also installed WP Crontrol for the time being so I can manually trigger the CRON whenever I want.
So, I am 100% confident the WP CRON is working.
I also then have a function that queries a custom post-type looking for entries flagged that they need a 'reminder' sent. I then run an SQL query against a custom table that retrieves 'assignments' that match the CPT and tomorrow's date. For each of these, an email is sent.
The function works. If I call it anywhere else on the site, it executes. If I just drop it in an add_action()
like add_action( 'wp_footer', 'my_reminder_emails', 100 );
it will properly execute and it will send me all of the appropriate email reminders. (For the time being I have all the emails coming to me rather than the assignees.)
Where it all breaks down is within the WP CRON. The WP CRON runs as scheduled, it updates it's last run time stamp and then... ...nothing.
HTTP Authentication is enabled. Instead of waiting for traffic on the development server I'm using WP Crontrol to fire the WP CRON. If I'm patient and just refresh the site every 5 minutes myself, I can see the WP CRON is running. But no emails are sent.
The site can send emails, that's not the issue. If I trigger the emailer function in ANY other way, it works. It only doesn't work when the WP CRON triggers it.
I have even taken the code from within my function and included it in the WP CRON (rather than having a separate function to call) and that also doesn't work.
Here's the WP CRON set-up:
function custom_cron_deactivate() {
$timestamp = wp_next_scheduled( 'custom_cron_hook' );
wp_unschedule_event( $timestamp, 'custom_cron_hook' );
}
register_deactivation_hook( __FILE__, 'custom_cron_deactivate' );
function 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' );
function 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 tasks
custom_reminders_cron_task();
}
add_action( 'custom_cron_hook', 'custom_cron_exec' );
This is the executable function (I have removed some of the basic 'Query' stuff which I know works.
function custom_reminders_cron_task() {
$today = date( 'Y-m-d' );
$tomorrow = strtotime( date( 'Y-m-d', strtotime( $today . ' +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',
'value' => 1,
'compare' => '=',
'type' => 'NUMERIC',
),
),
);
$rems = get_posts( $rems_args );
$remslist = implode( ',', $rems );
global $wpdb;
$custom_table = $wpdb->prefix . 'custom_assignments';
$rems_prepare = $wpdb->prepare(
"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 ) :
$subject = 'Automated Assignment Reminder';
$udata = get_userdata( $send_rem->user_id );
$uemail = '[email protected]';
$uname = $udata->first_name . ' ' . $udata->last_name;
$body = '<h3>Custom Assignment Reminder</h3><p>' . $uname . ', <br/><br/>you have been assigned <strong>' . get_the_title( $send_rem->posid ) . '</strong> for ' . date( 'F j, Y', $send_rem->sdate ) . '.<br/><br/><small>This is an automatically generated reminder - please do not reply.</small></p>';
$headers = array(
'Content-Type: text/html; charset=UTF-8',
'From: Custom <[email protected]>'
);
wp_mail( $uemail, $subject, $body, $headers );
endforeach;
}
As I said, every individual piece works. Just not in combination.
Any help would be greatly appreciated.
custom_reminders_cron_task();
before theupdate_option( 'customCRON_last', strtotime( $current_time ) );
, do you still see the option updated after a cron run?update_option()
into the function... ...it works when I run the function on it's own, doesn't work when I run it via the CRON.