his may be the first time you’ve heard of a cron job, or you may have come across this technical term without knowing its meaning. If you are a website developer, website owner, or customer service employee for one of the servers or hosting, then you certainly know what this term is.
We can say that Cron is a special system for scheduling tasks and executing them on the server, and it is not only related to WordPress, as it is basically affiliated with Linux and Unix servers.
In today’s article, we will introduce you to Cron job in detail and show you how it is compatible and works on the WordPress system.
Cron job – Overview
As we indicated at the beginning of the article, Cron job is a system for automating and executing tasks on a schedule that is determined in advance. It was given this name (job) because it performs tasks that are assigned to it to be carried out according to the time that is specified.
A Cron tool is a software system or service originally created for the UNIX operating system that enables users to automatically execute certain tasks in the background at specific times. It is a scheduler that is executed based on time.
Referring to WordPress, which created its own Cron job system under the name WordPress Cron, or WP-Cron for short, it is activated automatically to perform a group of tasks on a scheduled basis. The most important thing this system does:
- Backup: So that your site is backed up at a specified date
- Newsletter and promotional messages: It schedules sending messages to users at specific times or when a specific event is implemented, such as publishing a new article.
- Organizing the publication of articles: Through WordPress Cron, you can schedule the publication of articles or move them from draft to published articles
- Checking for updates: One of the tasks assigned to WordPress Cron is to update the current WordPress theme or one of the site’s plugins
- Cleaning and updating the database: One of the tasks that WordPress Cron performs to improve the performance of your site is to automatically delete comments and neglected articles from the databases every specified period.
- Updating your site at a specific time: Through WordPress Cron, you can allocate the time when the number of visitors decreases to update the version of WordPress as well as the plugins automatically.
Many plugins use the WordPress Cron function to perform different tasks. The most famous plugins that use WordPress Cron are the Jetpack plugin , the Akismet plugin, and also the famous WooCommerce plugin that you use to delete unpaid orders and change the active session, to name a few.
Now you may be wondering how can I benefit from WordPress CRON?
In the following paragraphs, we will explain to you everything in detail about how to benefit from this service to schedule the tasks you want yourself.
How is CRON job compatible with WordPress?
The CRON job, as we mentioned previously, is essentially a system for servers and hosts that operate on the UNIX and LINUX operating systems. This system is used to request the execution of a specific task at a specific time. These tasks are performed automatically every day without user intervention, and without rewriting any software code.
For example:
- We ask the server to execute (run) the file send-mail.php at 15:00:00 every day.
- We ask the server to restart itself at a specific time every day (if we have permissions).
However, server administrators may disable the basic Cron job for security reasons, or running it may require different settings for each server. For this reason, WordPress developers created their own Cron job system under the name WordPress Cron to overcome this problem.
Returning to the question: How is Cron job compatible with WordPress?
We say that it is possible to set up your WordPress site to use the Cron job platform built into the Unix or Linux server operating system. However, this can be difficult for two main reasons as we noted:
- Hosting may not allow you to set up a Cron job integrated with the server or hosting for security reasons.
- Servers or hosting may require setting up a Cron job in different ways depending on the programming language, which requires you to be familiar with different programming languages.
Hence we conclude that:
Each of the Cron job and WordPress Cron has its own way in which it works, and the first is specific to servers on the Linux and Unix operating systems, and the second is specific to the WordPress system, which we will explain how to use and benefit from in detail in the following paragraphs of the article.
WordPress Cron Job
All WordPress Cron jobs are stored in the site’s database within the wp_options table , specifically within a line or row designated as cron.
These functions or tasks are called via the wp-cron.php program file located within the core WordPress files, as the image below shows:
Returning to the question, how does the WordPress Cron function work?
When a visitor visits your WordPress site, the file wp-cron.php will be executed, which calls the tasks stored in the database in the wp-options table, and those tasks begin to be executed according to their schedule (some of them run hourly, some of them run hourly). Once a day at a specific time..)
So it can be said that the WordPress Cron job works in the following sequence:
- A visitor enters the site
- Run the wp-cron.php file
- Calling tasks from the database from the wp-options table, specifically from the cron row
- Tasks are carried out according to the specified time
Did you notice anything from the previous sequence?
Note, my friend, that the WordPress Cron function is only executed if someone visits the site. This in turn means that if you have a task that will be completed at 12:00:00 and no one visits your site until 13:00:00, that task will be postponed until the first visit!
In other words, the wp-cron.php file will never work as long as your site does not receive traffic. This is in contrast to the Cron job integrated with the server or hosting, which will execute the tasks contained within it as long as the operating system is running, even if no visits come.
Although this can be useful and ensure that hosting resources are not exhausted as long as there is no traffic, it can be a problem in some cases. Especially if you have to perform a task regardless of visits, such as sending a message to all subscribers to your site at a specific time.
For example: You have an online store that runs on the WooCommerce system and you wanted to promote a specific product (by email) at 12:00:00 on Eid al-Fitr, but you did not receive any visits. In this case, this task will not be carried out until you receive a visit, which may be later than the requested date.
But do not worry, this problem can be overcome in more than one way. For example, you can create your own Cron job on the hosting or server to run the wp-cron.php file at this time or run it every specific period you choose.
The importance of creating a WordPress cron job
The importance of defining a WordPress cron function can be summarized in two basic points as follows:
- Executing tasks at specific intervals: so that a task runs every hour, every day, or even every month. Such as sending a message reminding subscribers that the date for paying the annual or monthly subscription for a service is approaching, or updating the version of WordPress, or taking a backup copy…etc.
- Performing a one-time task: such as deleting WordPress setup files after installing the site for the first time, publishing an article at a specific time, sending a message to remind a user to activate email, etc.
The WordPress Cron function also helps improve the site by:
- Executing tasks at times when there are fewer visitors to reduce pressure on the server during peak times
- Helping to manage the site better so that the site owner is freed from the restrictions of remembering to carry out various tasks
- Reducing the work team because tasks are written once so that they work permanently and automatically without intervention
Disadvantages of WordPress Cron Job
In addition to the advantages of WordPress Cron, it also has some disadvantages and risks, which may include:
Creating pressure on the server or hosting
As we explained above, the WordPress Cron function runs every time someone visits your site. Therefore, if the number of visitors to your site is low, there are no problems here, but if you have a large number of visitors, the wp-cron.php file will be executed thousands of times, which puts pressure on the site and drains hosting resources.
Causing slowness in the site
This results from repeatedly running wp-cron.php for each visitor, and checking scheduled tasks in the database, which causes a noticeable slowdown in the site.
Failure to complete some tasks on time
If the number of visitors to your site is low and you set up a Cron job to be executed at 12:00:00, for example, and no visitors come at this time, the job is postponed until the first visitor arrives after this time.
It can be exploited to carry out an attack on the site
Older versions of WordPress were vulnerable to SQL injection attacks using the wp-cron.php file, but this vulnerability has been closed in recent versions.
Let us delve deeper into the WordPress cron function and learn how to stop or start it at a specific time, regardless of visitors. We will also explain to you how to add a new WordPress cron function to your site in several ways in order to remove any confusion you have regarding this concept.
Using a WordPress cron job
Let us now explain to you how to deal with WordPress Cron so that you can reset it to work regardless of visits, as well as create a new cron job on WordPress:
If your WordPress site is subject to large visits, it is recommended that you disable and reset the wp-cron.php file so that it does not run every time a visitor views your site and this negatively affects the performance of your site. If your site is small and only receives a small number of visitors 24 hours a day, you will not have a problem with this part.
To improve your site’s performance and avoid the negative effects caused by the WordPress Cron job, there are two steps you should perform:
- Step one: Disable wp-cron built into WordPress
- The second step: Run wp-cron every specified period through the Cron job built into the hosting or server
First: Disable wp-cron (WordPress Cron)
To disable the WordPress Cron function, you must disable the wp-cron.php file by adding code to the wp-config.php file by following these steps:
1. Go to Control PanelFor hosting your website
2. From the files section, choose File manager, as the image below shows:
3. Click on the public_html folder and then open the folder containing your site.
4. Open the wp-config.php file by right-clicking on it and then choosing the edit command as the image below shows:
5. Add the following code to the file, then click Save Changes as the image shows:
Note: There are many add-ons through which you can also modify the wp-config.php file. You can also access it through an FTP client program such as Filezilla.
Run Wp-cron using Cron job for hosting
After disabling the WordPress Cron function by modifying the wp-config.php file, we move to the second step, which is to run wp-cron every specified period, for example every one hour:
1. Go to the cPanel control panel of your site hosting, then choose Cron job from the Advanced section , as the image below shows:
2. Set up a Cron job as the image below shows, adding the following code in the last line:
Warning: Do not forget to change https://domain.com to your website’s domain.
The following code can also be used:
It is also the path to the wp-cron file, with user-name having to be changed to the user name of the files.
NB:
In newer versions of WordPress (WordPress 3.3 and above), the WordPress Cron job can be reset by adding the following code to the wp-config.php file. This code forces wp-cron to run every specified amount of time you choose (in seconds) regardless of visits. This saves you the trouble of adding a Cron job from your hosting or server.
Code used:
Note that 3600 is the time period in seconds, i.e. in this case the cron job will run every 60 minutes.
Create a new WordPress Cron job
If you are a programmer or website developer and know well how to deal with WordPress functions and hooks, the best way to create a new WordPress cron job is by writing the code yourself.
Alternatively, you can use custom plugins to accomplish this task. In the following paragraphs, we will explain to you the two methods and choose what suits you.
Create a WordPress Cron Job Manually (For Developers)
To create a WordPress Cron job to send a daily message to someone, the following simple code enables you to do this:
Code explanation:
- First, we check whether a Cron job named send_new_mail_hook exists or not using wp_next_scheduled()
- If send_new_mail_hook is not active, it is activated or scheduled once a day using wp_schedule_event
- We then created a callback function called send_new_mail to send the email to the requested person
- Finally we ran everything via add_action and called the calling function send_new_mail to be executed if the send_new_mail_hook event was fired
Note: The previous code is the simplest code to create a WordPress Cron job, so be sure to write an ideal code that assumes all the different cases. It is also illogical to write the email you want to send to yourself, but you can, for example, choose all the emails registered on your site, and this in turn It requires good knowledge of WordPress functions and how to work with them.
Manage and create WordPress Cron using WP Crontrol plugin
You can manage all WordPress Cron jobs using the wp Control add-on, which is a free add-on that enables you to view all the Cron jobs on your site and control it as you want. It also enables you to create your own WP-Cron job. This is as follows:
1. From the site control panel, go to the tab: Add-ons > Add new
2. In the search box, type the name of the add-on WP Crontrol , so it will appear in the search results and you can install and activate the add-on as the image below shows.
If you need a greater explanation of the plugin installation process, you can see how to install a WordPress plugin and activate it on your site correctly .
3. Now to manage or add WordPress Cron jobs, click on Tools and then choose cron events. You will notice that a window will appear showing you all the Cron events available on your site that cause Cron jobs to be executed in the background and when each of them will be executed.
These events are defined and launched by WordPress itself or by the plugins installed on the site. You can create a new WordPress cron event and link it to a new cron task, by clicking on Add New, as the following image shows.
First, fill out the form shown in the following image:
1- In this text box, add the name of the hook without spaces (you will need it later to create a function or callback)
2- In the Next Run section, specify when the event will run for the first time
3- Choose from the Recurrence drop-down list the recurrence date, or the time at which the event will be executed Either daily, weekly, or otherwise as needed
You still need to specify the task that will be executed every time the event occurs (every minute in this example) as we defined in the previous step. Go to the functions.php file and add the following code to it:
The previous example sends a message every one minute. Note that the hook name created in step 1 must also be the same name in the code as shown.
I believe that you are now able to create your first WordPress Cron job on a WordPress site. I also believe that you are able to create a Cron job in your site’s hosting through the cPanel hosting control panel to better control your site and improve its performance.
Conclusion
Through this article, we learned about both Cron jobs and WordPress cron and explained the difference between them.
We also discussed in some detail the advantages and disadvantages of WordPress Cron and how to overcome these disadvantages either through hosting or through the wp-config.php file. In the end, we explained how to deal with WordPress cron by using the software solutions provided by functions and hooks in WordPress, as well as by using the free wp control plugin.
Now it is your turn to try dealing with new WordPress Cron jobs on your site, and try to run that system via cron jobs on the server as we explained to you in detail. If you encounter any problem, please leave your inquiry in the comments and you will be answered as soon as possible.
Leave a Reply