How to setup cron job in wordpress without plugin ?

If you are looking for a way to execute commands and tasks on your WordPress site several times a day, something like a specific day, week, month, etc., you’re struggling to accomplish this functionality, don’t worry, I will tell you how to overcome this with a WordPress cron jobs, a cron job is a task that is set to run at a specified interval.

WordPress cron is one of the most useful features. You can schedule events to run at specific intervals, but rather than act like a classic cron job, it is scheduled with events and sets and written to the database. The next time the user opens the site, the WordPress cron system checks if the event is scheduled, and if so, it raises the event.

The WordPress cron schedule events can be categorized into two flavors there are
1. Single events – It will be executed only once, and it will never fire until reschedule it
2. Recurring events -that occurs more than once using a time interval.

Here I am going to show you how to set cron job on WordPress with a simple examples, You can test yourself by using the following code in WordPress’s active theme functions.php file or by creating a custom plugin.

Note : To schedule periodic events, you need to create custom ‘actions’ that need also be registered in cron. When cron is executed, the function associated with the custom ‘action’ created previously is executed.

1.Let’s see the following scheduling single event WordPress Cron Job example where we are going to be send an email after 5 minutes post was published, here we used wp_schedule_single_event() WordPress Api to set schedule event.

2. Let’s see the another recurring scheduling events WordPress Cron Job example where we are going to be send an email on a daily basis to users to visit our site, here we used wp_next_scheduled() and wp_schedule_event() WordPress Api to set schedule events.

3. Let’s see the another unschedule scheduling event WordPress Cron Job example with  using above example, here we used wp_unschedule_event() and wp_clear_scheduled_hook() WordPress Api to unschedule events.

OR

By default, WordPress defines three intervals only “hourly“,”twicedaily“and “daily“. To add custom intervals with WordPress’s “cron_schedules” filter, we use some WordPress time constants. A place to ease our lives. For more information about these constants, see “Using Time Constants“. Here is a brief overview

4. Let’s see the antoher customizing cron Intervals example, here we used “cron_schedules” filter to add our own intervals.

Hopefully, in this article, you have learned WordPress Cron Job is an excellent feature that can be used to schedule multiple events on WordPress sites. With some easy examples, we have seen how to use Cron WordPress for some functions to share. It can also be used for other situations. Please Subscribe to ScriptHere.Com by Email. You can also find us on Facebook and Twitter

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.