Andy Tran

Setup Cron Job in WordPress without Plugin

by | 11 Feb, 2022 | Wordpress | 3 comments

Setup Cron Job in Wordpress without Plugin

How to set up a Cron Job in WordPress without using any plugin

One of the most useful features of WordPress is the cron. You can schedule events to run at specific intervals. They are scheduled with events and sets and written to the database, rather than acting as a traditional cron job. The WordPress cron system checks if the event is scheduled the next time the user visits the site, and if it is, it raises the event.

There are two types of WordPress cron schedule events.

    • Single Events – They will execute once, and never fire again unless they are rescheduled.
    • Recurring Events – These happen repeatedly over a period of time.

To manage all the cron scheduled events, Download and do Install Advanced Cron Manager – debug & control Plugin.

After Installation and Activation the plugin, Go to Tools -> Cron Manager.
Here, You can display all the Scheduled Events.
Cron job

Write the following code in your functions.php file to upload the custom logo on WP Login Page.

    • Login to your WordPress Admin Dashboard.
    • Now from the left sidebar go to Appearance -> Theme Editor.
    • Go to the functions.php file and write the below code.

Scheduling single event WordPress Cron Job

 


//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( 5 * MINUTE_IN_SECONDS, 'post_publish_send_email');
}

//When cron runs, this method will be called.
function post_publish_send_email() {
    //init time
    $time = date("Y-m-d h:i:sa", time());
    //send email to admin
    wp_mail( 'admin@yoursite.com', 
             'New post has been published', 
             'New post has been published on' . $time );
}

Recurring scheduling events WordPress Cron Job
Where we are going to send an email on a daily basis to users to visit our site using the WordPress API wp_next_scheduled() and wp_schedule_event().

 


//Add method to register event to WordPress init
add_action( 'init', 'register_daily_notify_user_send_email_event');

//This method will register the cron event
function register_daily_notify_user_send_email_event() {
    // make sure this event is not scheduled
    if( !wp_next_scheduled( 'notify_user_send_email' ) ) {
        // schedule an event
        wp_schedule_event( time(), 'daily', 'notify_user_send_email' );
    }
}
 
//notify_user_send_email method will be call when the cron is executed

add_action( 'notify_user_send_email', '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 adminhttps://assets.grammarly.com/emoji/v1/1f610.svg
    wp_mail( 'admin@yoursite.com', 
             'TEST DAILY NEWS', 'Visit my site !!');
}

For Unschedule and clear the Events, You can use the hooks – wp_unschedule_event() and wp_clear_scheduled_hook().

Example of the Send Email to Admin Every 2 Hours (Cron Job)

 


// If a cron job interval does not already exist, create one.
 
add_filter( 'cron_schedules', 'check_every_2_hours' );
 function check_every_2_hours( $schedules ) {
    $schedules['every_two_hours'] = array(
        'interval' => 7200,
        'display'  => __( 'Every 2 hours' ),
    );
    return $schedules;
}

// Unless an event is already scheduled, create one.
 
add_action( 'wp', 'bbloomer_custom_cron_job' );
 
function bbloomer_custom_cron_job() {
   if ( ! wp_next_scheduled( 'send_email_two_hours' ) ) {
      wp_schedule_event( time(), 'every_two_hours', 'send_email_two_hours' );
   }
}

// Trigger email when hook runs
 
add_action( 'send_email_two_hours', 'custom_send_email' );

// Send email
 
function custom_send_email() {   
      $email_subject = "Testing a cron event";
      $email_content = "This is an automatic WordPress email for testing a cron event.";
      wp_mail( 'your@email.com', $email_subject, $email_content );
}

After that, you can get the email every 2 hours.
Testing a cron event

Also Read: Connecting WordPress Media Library to Google Drive made easy!!

Thank you for exploring our blog. We’re confident that you’ve found value in our ideas and processes. Erudite Works Pvt Ltd stands out as a top IT consulting firm, dedicated to delivering exceptional results. Our expert team is fully committed to understanding our clients’ unique needs, enabling us to offer customized solutions that fuel significant business growth. With a proven track record of successful projects, we are committed to helping clients achieve their goals and stay competitive in today’s fast-paced digital landscape. For more information on our services, please don’t hesitate to reach out. Contact Us.

Share this article...

3 Comments

  1. קמגרה

    Everything is very open with a precise clarification of the challenges. It was really informative. Your website is useful. Thanks for sharing!

    Reply
    • Palak Mistry

      קמגרה – Thank you so much for your positive response
      We really appreciate it.

      Reply
  2. Palak Mistry

    קמגרה – Thank you so much for your positive response
    We really appreciate it.

    Reply

Submit a Comment

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


The reCAPTCHA verification period has expired. Please reload the page.