Sunday, November 28, 2010

How to Replace WordPress Cron With A Real Cron Job

wpcron-missed-scheduleWe are all too familiar with the "Missed Schedule" status, aren't we? I am sure it is very frustrating to see that all your scheduled posts went MIA.

WordPress comes with its own cron job that allows you to schedule your posts and events. However, in many situations, the WP-Cron is not working well and leads to posts missed their publication schedule and/or scheduled events not executed.

To understand why this happen, we need to know that the WP-Cron is not a real cron job. It is in fact a virtual cron that only works when a page is loaded. In short, when a page is requested on the frontend/backend, WordPress will first load WP-Cron, follow by the necessary page to display to your reader. The loaded WP-Cron will then check the database to see if there is any thing that needs to be done.

Reasons for WP-Cron to fail could be due to:

  • DNS issue in the server.

  • Plugins conflict

  • Heavy load in the server which results in WP-Cron not executed fully

  • WordPress bug

  • Using of cache plugins that prevent the WP-Cron from loading

  • And many other reasons



There are many ways to solve the WP-Cron issue, but the one that I am going to propose here is to disable the virtual WP-Cron and use a real cron job instead.

Why use a real cron job?


By using a real cron job, you can be sure that all your scheduled items are executed. For popular blogs with high traffic, using a real cron job can also reduce the server bandwidth and reduce the chances of your server crashing, especially when you are experiencing Digg/Slashdot effect.

Scheduling a real cron job


To configure a real cron job, you will need access to your cPanel or Admin panel (we will be using cPanel in this tutorial).

1. Log into your cPanel.

2. Scroll down the list of applications until you see the "cron jobs" link. Click on it.

wpcron-cpanel

3. Under the Add New Cron Job section, choose the interval that you want it to run the cron job. I have set it to run every 15minutes, but you can change it according to your liking.

wpcron-add-new-cron-job

4. In the Command field, enter the following:
[bash]wget -q -O - http://yourwebsite.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1[/bash]

Replace http://yourwebsite.com with your website URL.

5. Click the "Add New Cron Job" button. You should now see a message like this:

wpcron-current-cron-job

6. Next, using a FTP program, connect to your server and download the wp-config.php file.

7. Open the wp-config.php file with a text editor and paste the following line:

[php]define('DISABLE_WP_CRON', true);[/php]

8. Save and upload (and replace) this file back to the server. This will disable WordPress internal cron job.

That's it.

To test it out, it is best to schedule a post 5 - 10 minutes after you have completed the above configuration. Wait and see if the WordPress internal cron job is fired. If yes, then congratulation, you have successfully scheduled a real cron job for WordPress. If no, go through the steps above and see if you have made any mistake. Alternatively, contact your web host and get them to configure for you.

No comments:

Post a Comment