In today’s fast-paced digital world, it’s crucial for websites to run smoothly and efficiently. That’s where WordPress scheduling and task automation come in. These tools help automate repetitive tasks and improve the overall performance of your website. One of the key components of this automation is WordPress Cron, also known simply as “wp cron“.
WP Cron is a built-in feature in WordPress that helps you schedule and automate tasks on your site, such as publishing posts, checking for updates, and sending emails. However, WP Cron has some limitations and may not be suitable for all websites. That’s where the WP cron PHP alternative comes in.
In this article, we’ll dive into the world of WP Cron and explore its benefits, how to set it up, and alternatives such as PHP Cron. Whether you’re a WordPress beginner or an experienced user, this article will provide you with the tools and knowledge to optimize your website’s performance through task automation.
Understanding WP Cron
WP Cron is a built-in feature in WordPress that acts as a virtual scheduler for your website. It helps automate repetitive tasks such as publishing posts, checking for updates, and sending emails. WP Cron works by checking your website’s schedule at regular intervals and executing any tasks that need to be performed.
WP Cron is different from traditional Cron jobs, which are typically run on a server at a specified time. WP Cron, on the other hand, is triggered by website visitors, which means it only runs when someone visits your site. This can cause issues if your website doesn’t receive a lot of traffic, as your tasks may not be executed as frequently as you need them to be.
One of the benefits of WP Cron is that it’s easy to set up and use, making it a popular choice for many WordPress users. However, it’s important to keep in mind that WP Cron is not suitable for all websites and may not perform optimally for sites with heavy traffic. In these cases, it may be necessary to run WP cron manually.
To run WP cron manually, you can add a simple code snippet to your site’s functions.php file. This code will trigger WP cron to run when you visit a specific URL on your site. You can also run WP cron manually using a plugin, such as WP Crontrol, which provides a simple interface for managing your site’s cron jobs.
To run WP cron manually, you can add the following code snippet to your site’s functions.php file:
function run_cron_manually() { if ( ! wp_next_scheduled( 'my_cron_hook' ) ) { wp_schedule_event( time(), 'hourly', 'my_cron_hook' ); } } add_action( 'wp', 'run_cron_manually' );
This code will trigger WP cron to run when you visit a specific URL on your site. You can change the interval (hourly in this example) to any value that suits your needs.
It’s important to note that you should only add this code to your functions.php file if you’re familiar with PHP and understand the potential consequences of modifying your site’s code. Always make a backup of your site before making any changes.
WordPress Cron is a powerful tool for automating tasks on your WordPress site. Understanding how it works and how to run it manually is an important part of optimizing your site’s performance and keeping your tasks running smoothly.
Benefits of Using WP Cron
WP Cron is a powerful tool that can help you optimize your WordPress site’s performance by automating repetitive tasks and improving background processes. Here are some of the benefits of using WP Cron:
Improved Efficiency and Productivity: WP-Cron automates tasks such as publishing posts, checking for updates, and sending emails, freeing up time and allowing you to focus on other aspects of your site.
Better Background Processes and Scheduled Events: WP-Cron helps schedule tasks to run in the background, improving the overall performance of your site and ensuring that scheduled events run smoothly.
More Control Over Automated Tasks: With WP Cron, you have more control over the tasks that run on your site, including the frequency and timing of these tasks.
What Does WP Cron Do?
WP Cron is a built-in feature in WordPress that enables you to schedule and automate tasks on your site. It’s responsible for triggering tasks, such as publishing posts, checking for updates, and sending emails, at specified intervals. WP Cron runs in the background when the page is loaded and does not require manual intervention, making it an efficient and convenient tool for automating tasks on your site.
How WP-CRON Functions and their Impact on Site Performance?
Unlike a traditional cron job, WP-CRON operates differently by checking for scheduled events each time a website page is loaded. While this method is suitable for sites with a moderate traffic flow, it can pose issues for sites with either extremely high or low traffic.
For sites with high traffic, constant WP-CRON checks increase the workload for the server and negatively affect performance. On the other hand, sites with low traffic may not check for scheduled events frequently enough, leading to missed tasks such as scheduled backups or posts.
Improving the functionality of WP-CRON involves two steps: creating a standard cron to run scheduled events and preventing WP-CRON from continuously checking for these events. By disabling WP-CRON, overall site performance can be improved.
Disable WP-CRON to Improve Site Performance
To ensure WP-CRON is disabled, check if the following code exists in your wp-config.php file:
define( 'DISABLE_WP_CRON', true );
If this line of code is present, WP-CRON is disabled. To enable it, simply remove this line or add the following code to your wp-config.php file:
define('DISABLE_WP_CRON', false);
WP Cron Alternatives
While WP Cron is a useful tool for scheduling and automating tasks in WordPress, it’s not perfect. Some users experience limitations and performance issues when using WP Cron. That’s why many WordPress users choose to replace WP Cron with an alternative solution.
One popular alternative to WP Cron is PHP Cron. PHP Cron is a more traditional cron job that runs on a server rather than within the WordPress environment. Unlike WP Cron, which only runs when someone visits your website, PHP Cron runs at regular intervals regardless of website traffic. This means that PHP Cron can be more reliable and efficient, especially for high-traffic websites.
Another advantage of PHP Cron is its ability to run multiple cron jobs at once. With WP Cron, each task is run one at a time, which can slow down your website if you have many tasks to run. With PHP Cron, you can run multiple tasks simultaneously, improving performance and efficiency.
Switching from WP Cron to PHP Cron is relatively easy, and most shared hosting providers offer an easy-to-use interface for setting up and managing cron jobs. However, it’s important to understand the technical aspects of PHP Cron and how it differs from WP Cron before making the switch.
Replacing WordPress Cron with PHP Cron on OpenLiteSpeed Server
Recently, we made a switch from a traditional LAMP (Linux-Apache-MySQL-PHP) server to OpenLiteSpeed servers for our websites. The primary reason for this was to enjoy a significant performance boost.
However, this change required us to learn how to configure a new server architecture, which was a challenge but also a great learning opportunity. If you’re reading this tutorial, you’re likely familiar with this situation.
Despite some similarities with the LAMP setup, OpenLiteSpeed has a few unique configurations that can be tricky. One of these is the location of PHP binary files, which can vary depending on the version of PHP you’re running. For example, if you’re using PHP 8.2, the binary file is located at /usr/local/lsws/lsphp82/bin/lsphp
The challenge with the WordPress cron (WP-cron) is that it does not run continuously. Instead, the wp-cron.php file runs only when a page is loaded on your website.
This can put a strain on your server resources if you have high traffic, and the schedules can easily be missed on low-traffic sites. To overcome this, we need to disable WP-cron and enable PHP cron. Here’s how to do it:
Add define('DISABLE_WP_CRON', 'true');
to the wp-config.php
file.
Edit the crontab for the nobody user by entering “sudo crontab -u nobody -e
” in the command line.
In the editor, paste the following lines, but remember to replace “82” with the actual PHP version number and the “/usr/local/lsws/huyhoa.net/wp-cron.php
” with the actual path to your WordPress installation.
# Run WP cron via PHP. Runs every 30 minutes. */30 * * * * /usr/local/lsws/lsphp82/bin/lsphp /usr/local/lsws/huyhoa.net/wp-cron.php > /dev/null 2>&1 # Crontab must end on a blank line. Ensure that such a line exists after this comment
Save and exit the editor by pressing ESC
key and :wq
then enter. You have now successfully enabled PHP cron.
To run PHP Cron on DreamHost shared hosting, we also need to disable WordPress Cron first by adding define('DISABLE_WP_CRON', 'true');
to the wp-config.php
file.
If you’re considering Dreamhost Shared Hosting, be sure to check out our comprehensive review to see if it’s the right choice for you!
Next, we create a cron job to run wp-cron.php
Navigate to the Cron Jobs page.
The Cron Jobs page appears:
Click the Add New Cron Job button.
The Creating New Cron Job page opens:
To create the cron, fill in the following fields:
- User: From the dropdown menu choose an existing SFTP or shell user you’d like the cron job to run under.
- Title: Give the cron job a name you’ll remember.
- Email output: Enter an email address you’d like to receive the cron output. You can also leave this blank to not receive emails.
- Command to run:
wget -q -O - https://huyhoa.net/wp-cron.php?doing_wp_cron
- Use locking: Enabling this if you want to prevent the job from running more than once at a time.
- When to run: From the dropdown menu, choose how often you’d like this job to execute.
Optimize WordPress Cron Job Optimal Performance in cPanel
For seamless and efficient website performance, optimizing your WP Cron is crucial. You can do this by following these simple steps:
Disable the wp-cron.php from firing automatically whenever someone visits your website by adding define('DISABLE_WP_CRON', true);
to your wp-config.php file.
Set up a manual cron job to run on a schedule in cPanel.
Login to cPanel.
In the Advanced section of the cPanel home screen, click Cron Job.
The Cron Job screen appears, select create a new Cron Job. If you want to receive an email notification every time cron is run, enter your email in the Cron Email field. However, because this cron job runs WP Cron, it runs continuously, I recommend leaving the Cron Email box blank, no each time it runs it sends 1 email, so it’s like spam mail.
If you don’t want to receive e-mail notifications for cron jobs, you can append >/dev/null 2>&1
to the command, which will redirect all output to /dev/null
.
Under Add new cron job, in the General list box, select Twice an hour.
You can run cron jobs up to once every 15 minutes on shared hosting accounts. I think 30 minutes for a WordPress cron job is more than enough.
In the Command text box, enter the following command line:
cd /home/username/public_html; php -q wp-cron.php
Or you can also use the following command:
/usr/bin/php -q /home/username/public_html/wp-cron.php
If you want to save the log of each run, fix it like this:
/usr/bin/php -q /home/username/public_html/wp-cron.php > /home/username/cronjob_log.txt
Remember to replace username with the correct user on your hosting
Once you have completed all the necessary steps, click the “Add New Cron Job” button. Your updated cron job settings will take effect immediately, and you can start experiencing a more optimized and efficient WordPress website.
To recap, here’s how to replace your current WordPress Cron with PHP Cron on servers Cpanel:
- Disable any existing wp-cron.php tasks by adding the code “define(‘DISABLE_WP_CRON’, ‘true’);” to your wp.config.php file.
- Set up a manual cron job through the cPanel control panel.
Eliminate any duplicate URLs by adding the following code to your .htaccess file:
#Rewrite Wp Cron Errors: RewriteCond %{QUERY_STRING} (^|&)doing_wp_cron=[0-9]+.[0-9]+(&|$) [NC] RewriteRule ^ %{REQUEST_URI}? [R=301,L]
Test your new cron job by using one of the old duplicate URLs.
For WordPress Multi-Site Server, you can either create a separate cron for each site or create a new file named wp-cron-multi-site.php
in the WordPress root and insert the code.
<?php if (!defined('ABSPATH')) { require_once(dirname( __FILE__ ) . '/wp-load.php'); } global $wpdb; $sites = $wpdb -> get_results("SELECT `domain`, `path` FROM " . $wpdb -> blogs); foreach($sites as $site) { $url = "http://" . $site -> domain . ((!empty($site -> path)) ? $site -> path : '/') . 'wp-cron.php'; $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_exec($ch); curl_close($ch); } ?>
Then use the following for the crontab command in cPanel:
wget -q -O - http://website.tld/wp-cron-multi-site.php?doing_wp_cron >/dev/null 2>&1
With these easy steps, you’ll have your WordPress cron job up and running in no time!
In conclusion, while WP Cron is a convenient tool for scheduling and automating tasks in WordPress, it’s not suitable for all websites. For those experiencing limitations or performance issues with WP Cron, switching to PHP Cron may provide a more reliable and efficient solution.