Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Last active January 3, 2023 19:33
Show Gist options
  • Save damiencarbery/d6e08bd803bd9945ef1bd5dec3383049 to your computer and use it in GitHub Desktop.
Save damiencarbery/d6e08bd803bd9945ef1bd5dec3383049 to your computer and use it in GitHub Desktop.
WP Cron - how to use and how to extend
<?php
// Set up task when plugin is activated
register_activation_hook(__FILE__, 'dcwd_cron_status_setup_schedule');
// On an early action hook, check if the hook is scheduled - if not, schedule it.
function dcwd_cron_status_setup_schedule() {
// Add the event only if it is not already scheduled.
if ( ! wp_next_scheduled( 'dcwd_cron_status_status_email' ) ) {
// Schedule weekly at 2:12am.
wp_schedule_event( mktime(2,12,0), 'weekly', 'dcwd_cron_status_status_email');
}
}
// Clear the scheduled task when the plugin is deactivated.
register_deactivation_hook(__FILE__, 'dcwd_cron_status_status_email_deactivation');
function dcwd_cron_status_status_email_deactivation() {
wp_clear_scheduled_hook('dcwd_cron_status_status_email');
}
function dcwd_cron_status_status_email() {
// Send the email.
}
/usr/bin/php -q /usr/local/webspace/httpdocs/damiencarbery.com/wp-cron.php
<?php
function dcwd_cron_project_send_status_email() {
// Do a query based on the date.
// Examples at: https://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters
$args = array(
'date_query' => array(
array(
'year' => date( 'Y' ),
'week' => date( 'W' ), // You may need to -1 to get the posts of the previous week
),
),
);
$query = new WP_Query( $args );
$message = '';
if ( $query->have_posts() ) {
$message .= '<ul>';
while ( $query->have_posts() ) {
$query->the_post();
$message .= sprintf( '<li><a href="%s">%s</a></li>', get_the_permalink(), get_the_title() );
}
$message .= '</ul>';
wp_reset_postdata();
} else {
$message .= '<p>No posts for this week.</p>';
}
$subject = 'Recent Posts';
// Send the email to the site admin.
add_filter( 'wp_mail_content_type', 'dcwd_cron_status_set_html_content_type' );
$headers[] = 'From: '.get_bloginfo( 'admin_email' );
wp_mail( get_bloginfo( 'admin_email' ), $subject, $message, $headers );
// Reset content-type to avoid conflicts -- http://core.trac.wordpress.org/ticket/23578
remove_filter( 'wp_mail_content_type', 'dcwd_cron_status_set_html_content_type' );
}
function dcwd_cron_status_set_html_content_type() {
return 'text/html';
}
<?php
// Disable WP Cron as it will be invoked manually.
define('DISABLE_WP_CRON', 'true');
<?php
/*
Plugin Name: WP Cron: Add Recurrance Options
Plugin URI: http://www.damiencarbery.com
Description: Add 'weekly' and 'biweekly' recurrance values. From <a href="http://www.smashingmagazine.com/2013/10/schedule-events-using-wordpress-cron/">Schedule Events Using WordPress Cron</a>.
Author: Damien Carbery
Version: 0.1
*/
// From:
add_filter( 'cron_schedules', 'wi_add_weekly_schedule' );
function wi_add_weekly_schedule( $schedules ) {
$schedules['weekly'] = array(
'interval' => 7 * 24 * 60 * 60, //7 days * 24 hours * 60 minutes * 60 seconds
'display' => __( 'Once Weekly', 'my-plugin-domain' )
);
$schedules['biweekly'] = array(
'interval' => 7 * 24 * 60 * 60 * 2,
'display' => __( 'Every Other Week', 'my-plugin-domain' )
);
return $schedules;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment