Skip to content

Instantly share code, notes, and snippets.

@igorbenic
Last active September 16, 2016 20:54
Show Gist options
  • Select an option

  • Save igorbenic/58e8096df9ea50d8f2112d8e5aac418e to your computer and use it in GitHub Desktop.

Select an option

Save igorbenic/58e8096df9ea50d8f2112d8e5aac418e to your computer and use it in GitHub Desktop.
How to use WordPress Cron to Schedule Events | http://www.ibenic.com/how-to-use-wordpress-cron-to-schedule-events
<?php
add_filter( 'cron_schedules', 'six_hours_custom_schedule' );
function six_hours_custom_schedule( $schedules ) {
$schedules['every_six_hours'] = array(
'interval' => 6 * HOUR_IN_SECONDS,
'display' => esc_html__( 'Every Six Hours' ),
);
return $schedules;
}
<?php
add_action( 'publish_post', 'get_share_counts_for_post', 20, 2 );
/**
* Schedule the share counts for this post
*/
function get_share_counts_for_post( $post_id, $post ) {
// Getting a UNIX Timestamp
$timestamp = get_post_time('U', false, $post);
$args = array( $post_id );
$hook = 'get_share_counts_every_six_hours';
$scheduled_timestamp = wp_next_scheduled( $hook, $args );
if( $scheduled_timestmap == false ) {
wp_schedule_event( $timestamp, 'every_six_hours' $hook, $args );
}
}
add_action( 'every_six_hours', 'update_share_counts_for_post' );
/**
* Update the share counts for the post
*/
function update_share_counts_for_post( $post_id ) {
// Process the share counts
}
<?php
// You can use any custom post type in the hook such as: 'publish_book' for a custom post type of 'book'
add_action( 'publish_post', 'share_my_post_after_hour', 10, 2 );
/**
* Getting the timestamp and applying 1 hour in seconds to it for scheduling
* Scheduling a single event with the post ID
*/
function share_my_post_after_hour( $post_id, $post ){
// Getting a UNIX Timestamp
$timestamp = get_post_time('U', false, $post);
// Using WordPress Time Constants
// https://codex.wordpress.org/Easier_Expression_of_Time_Constants
$timestamp_after_hour = $timestamp + 1 * HOUR_IN_SECONDS;
// Define remaining parameters
$args = array( $post_id );
$hook = 'share_the_post_after_hour';
// Get the timestmap of an already scheduled event for the same post and the same event action ($hook)
// Returns false if it is not scheduled
$scheduled_timestamp = wp_next_scheduled( $hook, $args );
if( $scheduled_timestmap == false ) {
wp_schedule_single_event( $timestamp_after_hour, $hook, $args );
}
}
add_action( 'share_the_post_after_hour', 'process_sharing_for_post' );
/**
* Processing the sharing for a post
* @param numeric $post_id The ID passed through the hook in the $arg variable
*/
function process_sharing_for_post( $post_id ) {
// Create the logic to share the post
}
<?php
/**
* Action that is called when a post has been moved to trash
*/
add_action( 'trashed_post', 'unschedule_sharing_from_post' );
/**
* Unschedule the sharing event on a post
*/
function unschedule_sharing_from_post( $post_id ) {
$args = array( $post_id );
$hook = 'share_the_post_after_hour';
$timestamp = wp_next_scheduled( $hook, $args );
if( $timestamp ) {
wp_unschedule_event( $timestamp, $hook, $args );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment