-
-
Save KoolPal/15989bc5ecc168f4501cc2ae6c132bf8 to your computer and use it in GitHub Desktop.
WP Cron - how to use and how to extend
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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. | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/usr/bin/php -q /usr/local/webspace/httpdocs/damiencarbery.com/wp-cron.php |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Disable WP Cron as it will be invoked manually. | |
define('DISABLE_WP_CRON', 'true'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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