Cron Jobs on Wordpress

zilog357

Senior Member
Joined
Nov 27, 2008
Messages
875
Reaction score
1,899
Sharing some experiences here. This is not directly about SEO, but it is about page loading time, which is something that affects SEO too.

One of the things that speed up a little more your Woocommerce store or WordPress website is modifying the cron jobs. This is not taken care of on any optimization plugin that I know of. Here is in my experience, how can cron jobs help page-loading speed if using WordPress.



WHAT IS A CRON JOB?

A Cron job is like a timer you set on your server so it visits your website or activates something on it or anywhere else over a period of time for specific time intervals that you can set.

For example, if you have a plugin using an API (Application Programming Interface) connected to any market like eBay, for the purpose of updating some information on products you are dropshipping, like price and quantity, the only way it can run that update by itself without you visiting your site and pushing a button, is by a cron job set on your server and your server will do it for you beautifully.

You can program the server to do it several times a day, a week, etc. Also at specific hours or specific days and so on. The fastest I have seen is one time per minute.



CRON JOBS ON WORDPRESS

When using WordPress, or Woocommerce and their plugins and extensions, you don't have to take care about this cron thing ever, or do you? Of course not. Some people don't even know that this thing exists and that is completely normal. You are mostly marketers, not programmers or webmasters.

WordPress has its own cron-like function (wp-cron). It was probably made by WordPress so you don't have to mess with the server, setting cron jobs on your own, making that way, WordPress even easier to use. The difference is that the wp-cron function does not work on time interval settings like the cron jobs in your server but works on page-load events. That means that any pending job on your website will only run as soon as anybody, including the admin, visits the page.

SO WHAT SEEMS TO BE THE PROBLEM?

1. If nobody visits your page or if you have very few visits, it will take longer to do the jobs. In the provided example, it will take longer to do any updates (price and qty).

2. Since the jobs to b done are waiting for the next visitor to fire up, when someone visits your website, he, she, or it (bots count) carries all the load of past-due jobs. That load is not on bandwidth but in the time that the visitor needs to wait until your site is ready. In other words, they need to wait until your website has completed all the pending housekeeping, which probably was already past due by far.

3. In the example given, if you list your products in another network like for promotion and that network shows price and availability but it takes long time intervals to update the information taken from your website, if you have had no visitors in the time that someone is driven to your website and also in that time there were major changes either in price or availability, he or she will not only wait some pain-staking extra time while the page loads, but he or she will be shown different numbers from those updates that were triggered by the visit to your website.

In that line, a common case could be that the network may show "3 available", but when the prospect lands on your website, it shows "out of stock".

As you can see, this all translates into slow page-load times and some other not-so-good things and lots of plugins use wp-cron, so visitors will always be waiting for "jobs to complete" if you have many of those plugins.

WHAT DO I DO?

I simply disable the wp-cron function that runs on page load, by putting the following line in the "wp-config.php" file and set a cron job in my server to do it at intervals I set:

Code:
define( 'DISABLE_WP_CRON', true );

Every time the server runs the cron job, it will do a command that you set with the cron job. That command in my case is visiting my page, triggering the wp-cron function, which will update everything. This time, instead of on every visit, it will work every time the server fires up the cron.

That address on your site is as follows:

Code:
https://your-site.com/wp-cron.php


The wp-cron function will work no matter what if you visit that URL above because what we disabled in the "wp-config.php" file was only for the part where wp-cron works on page-loads. That is why the URL for that function will be used as a target (command) for the cron job set on the server.

You should be able to set up a cron job in your hosting company. In my case, I set it up to fire up every 30 minutes. That keeps the website up to date and when a visitor comes, it is all fresh and ready. No extra waiting time for human visitors or bots.

For setting a cron job in your server it is as easy as putting a string of values in the cron job tab. In my case, I log into the control panel (Cpanel) and go to "cron jobs". In your case, it will probably have a visual area where you can choose the time intervals, but if not, the following string means once every 30 minutes:

Code:
0,30 * * * *

The following are some other cron setting examples:

Code:
Fires every minute:
* * * * *

Fires every 5 minutes:
*/5 * * * *

Fires every 30 minutes:
0,30 * * * *

Fires every hour:

0 * * * *

Fires every 12 hours:
0 0,12 * * *

Fires every day:
0 0 * * *

And so on...


If using the target (command) as a plain URL (//your-site.com/wp-cron.php) and it does not work, you may try using cURL as an alternative. For doing that, instead of placing the bare URL in the command area, use the following:

Code:
curl -s https://your-site/wp-cron.php > /dev/null


I advise consulting your server's documentation on how to set a cron job before doing anything else.

Also, cron jobs should be included in your hosting plan by default, even on shared hosting plans, but if for some reason you cannot set a cron job in your server, there are FREE services out there to do that. Many claim that they offer high security, but it is up to you to allow or not some third-party service (as many other services we already use) to connect to your site.

Ah yes, I know you thought about that. Doing it by yourself using some kind of automation? You would need to have a computer turned on 24/7 and connected to the Internet for when any timmer you set fires up to visit the wp-cron.php URL. Not a so good idea.

---

HEARTBEAT

---

By the way, another thing that makes WP faster is disabling the admin's heartbeat (no, not killing the admin). The WP Heartbeat, among other things, takes care of automatically making drafts and backups when you are writing or editing a page or a post and many more times than not, making the admin horribly slow. Turning it off will make the admin area faster also the front end will indirectly benefit too because, in the end, it all is using the same server and same processor(s) and memory.

You can turn it off by using Perfmatters and some other plugins, but a faster and easier way to do it is by just disabling it in the "functions.php" file of your theme. Just add the following code at the end of that file:

Code:
wp_deregister_script( 'heartbeat' );

After doing the cron thing and turning off the heartbeat I now have better page-loading times. From "C" on GTMetrix, now I have a "B" (yes, I am on shared hosting and running a lot of code).

On a curious note, I have a "B", both with and without WP Rocket activated and even registered a few milliseconds of extra lag with the plugin activated. I had a paid subscription to WP Rocket and ditched it. Also after the last update, reCaptcha and "Contact Form 7" stopped working properly when WP Rocket was activated even when I tried many settings.

Perfmatters is preferred by many members here. I checked the code and took away the functions I liked (like the heartbeat disabling thing explained above) and put them in a custom plugin I use to accommodate all the code I create to do specific things on my website.

Why a plugin instead of the theme's "functions.php" file? Because even when both will work flawlessly, the plugin will work for any theme I use and not for the actual theme only. The plugin is global in WP but "functions.php" works only for the theme where the file is located.

Well, too much writing for now.
 
Back
Top