WordPress Cron function
Have your posts not published at the right time? What happened then? How can you handle scheduling events? The answer is – WordPress Cron (wp-cron.php).
What is WordPress Cron (wp-cron.php)?
In WordPress, wp-cron.php is a small file located in the root folder. It is responsible for scheduling many events & tasks. Including publishing scheduled posts, checking for plugin or theme updates, sending email notifications and more. It can be rightly said “Time-Based Job Scheduler”. Whenever a page opens up on your site, WordPress Cron will check all the scheduled events to find if any event needs to fire at the moment.
Limitations of WordPress Cron Jobs
It only fires a scheduled event if any visitor comes. In case, there is no visitor, no traffic on site then it will not check the event. For example, suppose there is a reminder message that is required to send today to some event tomorrow. If any visitor doesn’t come until tomorrow morning, there is no relevance of sending that message tomorrow . It is a serious issue with wp-cron.php as if no traffic is there, Cron will not be prompted. And, the event goes missed.
Secondly, it checks that an event is to fire or not on each & every visit. If there is huge traffic then checking many times for a scheduled task also increases the resource usage. That is also inefficient.
How To Disable The Normal Behavior Of wp-cron.php?
To overcome the issues related to cron as discussed above, sometimes it’s necessary to disable it. Here at Templatetoaster WordPress theme builder, You can follow these simple steps to disable wp-cron.php manually
- Open the wp-config.php file with the cPanel File Manager Code Editor.
- Navigate to the bottom of the database settings in wp-config.php typically around line 37.
- Now, add the code – define(‘DISABLE_WP_CRON’, ‘true’);
- Click Save.
This will not automatically run the wp-cron.php script each time your site gets a new visitor.
How To Trigger wp-cron.php From Server?
You can set a cron job from server to trigger the wp-cron.php after an interval. It can be set up using cPanel. Following are steps:
- Log into cPanel.
- Navigate to Cron Jobs under the Advanced section. Click on Cron Jobs.
- From the Common Settings drop-down, select Twice Per Day. You can specify the execution time in terms of Months, Day, Hours or Minutes as per your needs. For example, If a wp-cron.php script runs every 12 hours, this will be fine for most of the WordPress users. It means executions will take place twice a day. It is preferable to the hundred of times for checking the same scheduled task.
- Lastly, fill in the code to run the WordPress Cron job in Command field as: “cd /home/username/public_html; php -q wp-cron.php” .
(Here, username is your cPanel username.)
Remember that the /home/username/public_html path would be for a primary domain. If you have WordPress installed in a subdirectory, you need to update your path.
- Now, click Add New Cron Job to save the changes.
- Always check that your new Cron Job is added successfully.
Now, WordPress should be safely running scheduled tasks via the wp-cron.php script. But the executions will take place only at newly set intervals.
How To Schedule Tasks In WordPress Using Plugins?
Next, the most important thing to learn is, how to schedule tasks in WordPress Cron from WordPress. This can be very easy using plugins. The best-known plugins for this are – WP Control Plugin and Advanced Cron Manager- debug & control Plugin.
Once installed, go to Tools → Cron Events. You can control Cron event settings from here.
You will see an event list here. The first column is the name of the hook. The hooks give you hint about what an event does. The default hooks majorly contain a wp_ prefix. Remember, never disable or delete a default hook.
This list will also give information about what Cron will run next and after how much time.
The last column will give you options to edit, delete or run a Cron event. By selecting the edit option, you can modify a Cron event. Here, you can schedule an event and specify that how often you want the event to run.
Once you made changes, save and you are done.
Once you install & activate this plugin, go to Tools → Cron Manager.
Here you will see a list of all the default Cron events. But you can add a new Cron event too.
To add new Cron job, click Add New Event. Fill the particulars like Hook name, time etc. as shown below. Click Add Event to save the changes.
To Sum Up
WordPress Cron is a scheduler that manages the recurring automated events. Some Cron events are default but you can also add new Cron events too. And, if these put extra burden on resources you can also disable or modify a Cron event. Now at Templatetoaster WordPress website builder software and website builder software, you have all the required information to disable, trigger or schedule the Cron Jobs as described above. So, you can easily control the wp-cron.php.
If you have any query, you can ask in the comment section below.
Design themes that make your websites stand apart from the rest. Get your dynamic Drag and Drop Theme Builder Software today.Download Now