How To Optimize Your WordPress Cron Job For Optimal Performance

WordPress is a great CMS, but man ... its wp_cron is a daemon from hell.

As you know, WordPress uses a file named wp-cron.php as the way to scheduled automated tasks.

It uses these to check for updates, running plugins such as Akismet to filter comments for spam, sending emails and automated tasks like backing up.

It’s a virtual cron job which is triggered whenever a scheduled task is due to run, which can be either due to someone visiting and generating an automatic email, or having your backup set to run on a regular basis.

On a small website with a few pages and a few hundred visitors a day wp-cron.php isn't a problem, but imagine a larger website with maybe a 100 visitors an hour.

If each visitor read your home page, and 2 others, that would mean that wp-cron.php is being called 300 times an hour, 7200 times a day and so on.

Plus, on a busy/large website, wp-cron.php might take a few minutes to run.

There are two issues which can be created by wp-cron.php not working as intended.

The first is that plugins and functionality which relies on cron jobs may not work properly, or might generate errors. So that includes things like scheduled posts, but also a lot of plug-in functionality for tasks like automated back-ups, auto-generated emails etc.

The other issue is potentially worse, as you may not spot it until you go looking. By creating a duplicate of your article with the same URL gaining ?doing_wp_cron on the end, essentially you end up with at least two versions of every WordPress post you create.

Here are some of the benefits of using server cron jobs: - more reliable & accurate cron jobs that fire on time as expected - helps make your WordPress website faster to your users/visitors - eliminates high CPU usage caused by WordPress

So, please consider using manual cron jobs for your scripts.

To fix the problem, you just need to follow 3 simple steps.

  • Disable the wp-cron.php from firing when someone visits your website.
  • Set up a manual cron job to run on a set schedule. If you run multiple sites on one server, then stagger the times to avoid firing everything at once.
  • Redirect the incorrect URLs so that they point to the article addresses you actually want to rank, and stop confusing search engines.

Edit your wp.config.php file by selecting it and clicking to view and edit.

You should see some text relating to your specific installation, and then:


/** Database Charset to use in creating database tables. */ define(‘DB_CHARSET’, ‘utf8’);
/** The Database Collate type. Don’t change this if in doubt. */ define(‘DB_COLLATE’, ”);

Underneath that text, simply add the following line:

	define( ‘DISABLE_WP_CRON’, true );
  • Log in to cPanel.
  • In the Advanced section of the cPanel home screen, click Cron jobs.
  • Under Cron Email, type the e-mail address that you want to receive notifications, and then click Update Email. Every time the cron job runs, the e-mail account will receive a message.

    If you do not want to receive e-mail notifications for the cron job, you can append >/dev/null 2>&1 to the command, which redirects all output to /dev/null.

  • Under Add New Cron Job, in the Common Settings list box, select Twice an hour.

    You can run cron jobs a maximum of every 15 minutes on shared and reseller accounts. A 30-minute interval for the WordPress cron job should be more than sufficient.

  • In the Command text box, type in PHP command example:
    	cd /home/$userName/public_html; php -q wp-cron.php
    

    (**replace "$userName" with your account's username keeping in mind that the /home/$userName/public_html path would be for a primary domain> If you're using an addon domain, or have WordPress installed in a sub-directory you'll want to be sure to update your path.**)

  • Click Add New Cron Job. The new cron job settings take effect immediately.

You can use tools such as these below to create a cron job:

To recap:

  • Turn off any existing wp-cron.php tasks by adding define(‘DISABLE_WP_CRON’, ‘true’); to your wp.config.php.
  • Set up a manual cron job via the cPanel control panel.
  • Redirect all the duplicate rubbish urls to the right ones by adding this below 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]
    
  • Use one of the old duplicates?wp_doing_cron urls to check it is now redirecting correctly.

If you are using WordPress Multi-Site Server, then you either have to create a cron for each site/blog on the WordPress multi-site network.

Alternatively, you can create a new file named wp-cron-multi-site.php in the WordPress root (same location as the current wp-cron.php file) and put this code into it.

 

Then use the following for the crontab command in cPanel:

	wget -q -O - http://www.yourwebsite.com/wp-cron-multi-site.php?doing_wp_cron >/dev/null 2>&1
  • WordPress, cron, wp_cron
  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

How To Manage Your WordPress Website With cPanel WordPress Manager

The cPanel WordPress Manager interface found at Applications >> WordPress Managerallows you...

How To Access WordPress Admin

WordPress Dashboard (a.k.a wp-admin) is the first screen you see when you log into the...

How To Change Change WordPress Language

WordPress comes with an extensive collection of languages but the primary language has always...

How To Change WordPress Password

There are several ways to reset your WordPress password. The tutorial below should be taken with...

How To Log Into WordPress

To log in to WordPress or WordPress admin dashboard, after completing WordPress installation, you...