Setup cron job on wordpress

Do you want to set a cron job in your WordPress Website? if yes then this is the right blog, I will show you the complete process of how you can create the cron job without any plugin or server details in just 10 minutes.  Schedules a hook which will be triggered by WordPress at the specified time interval. The action will trigger when someone visits your WordPress site if the scheduled time has passed. For more details, you can visit the WordPress function page wp_schedule_event and wp_schedule_single_event

For checking your wordpress cron job you have following way
1) Send Email
2) Create a .txt file
3) Enter some value in the database or update any post meta.

let’s start the coding part. You need to add the code in the function.php file under the active theme
1) wp_schedule_single_event()
2) wp_next_scheduled()

wp_schedule_single_event()

The use of this function when you want to hit your cron job at manually click on any button or you want to perform this action when any specific task run like below code will be excute after 2 minutes of publish the post


/**
 * schedule_post_publish_event runs when a Post is Published
 */
add_action( 'publish_post', 'schedule_post_publish_event' );
/**
 * when user posts this function will call
 */
function schedule_post_publish_event( $post_id ) {
    // Schedule the actual event
    wp_schedule_single_event( 2 * MINUTE_IN_SECONDS, 'post_publish_send_email');
}
/**
 * this method will call when cron executes
 */
function post_publish_send_email() {
    //init time
    $time = date("Y-m-d h:i:sa", time());
    //send email to admin
    wp_mail( 'admin@domain.com', 'New post published on your website', 'New post has been published on' . $time );
}

wp_next_scheduled()

Another cron job for recurring scheduling events WordPress Cron Job example where we are going to be send an email on to users, here we used wp_next_scheduled() and wp_schedule_event() WordPress Api to set schedule events.


/**
 * add method to register event to WordPress init
 */
add_action( 'init', 'register_user_send_email_event');

/**
 * this method will register the cron event
 */
function register_user_send_email_event() {
    // make sure this event is not scheduled
    if( !wp_next_scheduled( 'send_email_to_user' ) ) {
        // schedule an event
        wp_schedule_event( time(), 'daily', 'send_email_to_user' );
    }
}

/**
 * send_email_to_user method will be call when the cron is executed
 */
add_action( 'send_email_to_user', 'notify_all_user_send_email' );

/**
 * this method will call when cron executes
 */
function notify_all_user_send_email() {
    //here you can build logic and email to all users	
    //send email to admin
    wp_mail( 'user@domain.com', 'Today report from website', 'Visit my blog for wordpress tutorial ...!');
}

By default, WordPress defines three intervals only “hourly“,”twicedaily“and “daily“. To add custom intervals we use some WordPress time constants. A place to ease our lives. For more information about these constants, see “Using Time Constants“.


MINUTE_IN_SECONDS  = 60 (seconds)
HOUR_IN_SECONDS    = 60 * MINUTE_IN_SECONDS
DAY_IN_SECONDS     = 24 * HOUR_IN_SECONDS
WEEK_IN_SECONDS    = 7 * DAY_IN_SECONDS
MONTH_IN_SECONDS   = 30 * DAY_IN_SECONDS
YEAR_IN_SECONDS    = 365 * DAY_IN_SECONDS

Hope this blog will help you to create the cron job for your wordpress project

Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*

*