How Do You Set Up Cron Jobs With WordPress? What is WordPress cron and how does it work?WordPress cron jobs are typical queries among WordPress developers, especially newbies. There is an easy solution.
A common UNIX tool called Cron is used to schedule the execution of tasks (scripts or commands) at particular times, dates, or intervals. A cron job is the name of the operation it will carry out. As you might have guessed, the goal of a cron job is to automate repetitive processes so that you can be more productive with your time.
Before getting into the action and strategy, you should learn what a WordPress cron is and how it performs task automation because it differs somewhat from the standard cron.
What Exactly Is the Meaning of WordPress Cron?
For planning operations like checking for updates, planning a post for publication, and removing comments from the trash, WordPress includes its own cron system. WP-Cron manages all cron tasks.
While WordPress cron borrows its name (and basic concept) from UNIX cron, it doesn't operate in the same way and instead schedules tasks using intervals. WordPress offers hourly, twice-daily, every day, and weekly default time durations. The time-based jobs in this case are dependent on users accessing your website; therefore, WordPress cron will only run when a user is on your site.
So when the page loads, WordPress cron is started. Due to the fact that this isn't always reliable, many users choose a server-level cron job over the WP-Cron job.
Benefits of using WordPress Cron
Cron jobs are useful if you wish to manage a website process or if you frequently need to check, validate, and regulate it. For instance, we might wish to check the status of a system every hour and log it into the database, ping a server once per day, and so forth. The WordPress core and additional plugins may easily complete time-based activities thanks to WP Cron.
Instead of utilising a true cron job that requires familiarity with Linux commands, it is rather simple to set up a WordPress cron job. Additionally, unlike a genuine cron that specifies the specific moment a task should run, the WP-Cron uses gaps to schedule tasks, so you can be assured that your task will ultimately run even if the scheduled time has passed. The job won't run again until you manually reschedule it if the time expires and it is unable to be completed.
Controlling Cron Jobs in WordPress
There are various ways to set up and handle cron jobs in WordPress, but I'll only explain two of the more widely used ones.
The first makes use of a plugin for managing cron jobs, such as WP Control, which enables you to add, edit, and manage WordPress cron jobs right from the WP dashboard. Additionally, you may design triggers and functions, establish custom time intervals for tasks, and create cron jobs.
The second strategy makes use of the WordPress CLI.
How to Use Plugins to Control WordPress Cron Jobs
I'll make use of the WP Control plugin, which enables viewing and managing WordPress cron for your WordPress application. It is a well-liked plugin with more than 200,000 active installations that manage WP cron jobs.
WP-Cron doesn't operate constantly. By default, wp-cron.php runs on every page load, which might be an issue for busy websites. Otherwise, a request will come in if a website doesn't have enough PHP workers, and WordPress will create the cron. But the cron must continue to function for the worker, so it stays there.
This is the simplest way to manage WordPress cron jobs. With WordPress cron events, you can easily browse, modify, add, remove, and do much more. If You are going For Ecommerce WordPress Theme For Woocommerce Website this will come handy to you.
WP Control Plugin Installation:
WP Control must be installed from the plugin repository and activated, just like any other plugin.
After installation, you'll see a new selection called "Cron Events" under the Tools menu.
View and comprehend Cron Events:
After that, click Cron Events to get a list of the cron events that are now taking place in your WordPress application.
From this section, you may execute, amend, and remove your cron events. When you place your mouse over every cron event, this option will show up.
You can quickly determine which cron job is used for what purpose, obtain important information about various cron processes, and much more.
- The name of the hook utilised by the WordPress website or any plugin is displayed in the first column under "Hook." As an example, the prefix "wp" simply suggests that it is a cron of the WordPress core.
- However, you can use the edit and run now options.You'll also notice that there is no option for eliminating these cron events. Similar to how you'll see various prefixes for your plugins (like "wc" or "woo-commerce" for WooCommerce),
- The arguments given to the hook functions are only contained in the second column, called Arguments.
- The next time this cron job will execute is indicated in the third column.
- The "function" of the cron job is displayed in the fourth column, "Action," which lists the action this cron will take.
- The cron tasks' recurrence intervals are specified in the final column. This time frame relates to the scheduled task.
Cron event editor:
In order to test this plugin, let's now edit a WordPress cron job.
- To update a hook, click on its name.
- You'll find different fields in a new section called "Edit Cron Event." I changed the "wp privacy delete old export files" hook in my situation.
- The cron job's repetition will then be changed to "Once Hourly (Hourly)".
Adding a Custom Time Interval:
I'll then explain how to set a customised time frame for your cron events:
- Go to Cron Schedules in WordPress Settings.
- Now select the "Cron Scheduling" tab to view the specifics of the scheduled time intervals on a new screen.
- After filling out the fields Internal Name, Interval Seconds, and Show Name, click Add Cron Schedule.
- Please take note that the interval input is in seconds. The interval will be set to 10 minutes with a timer of 600 seconds.
- Now that your custom interval has been added, you can modify your cron event.
How to Create a New Cron Event:
Now that you know how to handle simple cron event duties, what should you do if you want to implement a custom cron job for your WordPress application?You just need to follow the directions below, so don't worry.
- To access the Add Cron section, first click Add Cron Event.
- After that, simply name the hook whatever you want (normal PHP naming conventions, no spaces).
- Next, add parameters (this step is optional); in my case, they are empty.
- Choose any option after that, then enter a time in the Next Run area.
- Then, click Add Event after selecting a recurrence time (schedule time) for your new cron.
- If you have any questions, you can look at any cron job settings from the cron events that are currently running.
- Go to the cron events page after adding a new cron job to verify your freshly generated cron. As you can see, the action column will display "Noob to verify your freshly generated cron. As you can see, the action column will display "None." This is due to the lack of actions specified for this cron job.
- As a result, you must add an action and a few lines of code to the theme's functions.php file.
- By going to WordPress Dashboard Appearance, select Theme File Editor.
- From the Theme Files menu on the right side of the page, select the function.php file.
How to Set Up a Genuine Cron Job
You now know what a WordPress cron job is and how it works. What about if you wish to change it out for a real cron job because of low traffic, critical tasks that must be completed at a specific time, frequent DDoS attacks, or slow page loads?
Open and modify the file wp-config.php
To use an FTP client like FileZilla or an SSH client like PuTTY, you must first open the wp-config file. Modify the file then, and add the following code line just before the line that reads:
With this code, the cron events that were already running on your WordPress website are disabled, allowing you to create a legitimate cron job.
I hope this article has explained how the WordPress cron job works. I've included two ways to manage cron jobs in WordPress, one using a plugin and another using WP-CLI. I've already talked about how to set up a genuine cron job on a live WordPress server.