ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Internet & the Web»
  • Web Page & Web Site Development

Cron Your WordPress! How to Set Up Cron Jobs Without Any Plugins (Examples Included)

Updated on January 18, 2018
itknol profile image

Yavor 'ItKnol' Mladenov is a Bulgarian freelancer, working as online business and IT consultant for more than a decade.

Source

Table of Contents

  1. Stats
  2. Tech Used
  3. Reliable Cron Setup for WordPress
  4. What Are Cron Jobs
  5. WP-Cron.php
  6. When WP-Cron Runs
  7. False Positives
  8. The Solution
  9. Testing if WP-Cron.php Works
  10. Resources

Stats:

  • Time to complete: 15-30 minutes
  • Difficulty: Intermediate

Tech Used:

  • cPanel hosting account with Cron support
  • Working installation of WordPress 4.4.13*

*should work with any other version as well

Reliable Cron Setup for WordPress

There are some tutorials out there, offering different answers to that question. Since different web hosting companies set up their servers differently, things tend to work. Until they don’t.

What I wanted was a command that would trigger WordPress’ cron and that would be the most native syntax there is, so it would work on any Linux host.

If you have already spent enough time googling, to the point where you question your sanity, you might be better off reading from “False Positives” onward.

If you want some sort of foundation to what you are doing, tag along!

What Are Cron Jobs

Simply put, cron is the app for Unix-based systems (such as Linux) that is used for executing a command at a specific time. That would be a time interval of days, hours, minutes and seconds that you can explicitly specify.

Cron can also run in cycles. Such as: every day, every hour, every minute and every second. Great for triggering commands routinely. Such as scheduling posts in WordPress.

Okay but what does a “Cron job” mean? – you ask.

Once you schedule a task (ex. “launch program X”) via cron, it adds an entry to an internal schedule. And that entry is called a cron job.

WP-Cron.php

Since some web hosting companies don’t offer cron jobs (although I am yet to stumble upon one) the folks at WordPress have created an internal cron scheduler.

That means it is available within WordPress. In case you are wondering – it is located in the root directory of your WP installation. Right next to wp-config.

When WP-Cron Runs

This is where things get tricky and a lot of people who want reliability in scheduling prefer to switch to the host’s regular cron.

WordPress is just a big php script that runs everytime a page is requested by a visitor on your website. This “script” includes everything you can think of: from loading plugins, displaying website content to checking for updates and other scheduled tasks.

Or simply put:

Wp-Cron runs every time someone sends a request to your site.

While an internal cron is nice, it comes as a one-size-fits-all solution. If we crudely separate websites into 2 main categories – big sites and small websites, we can easily see where it falls short.

Small Web Sites

If you don’t have people visiting your website every minute, you cannot rely on WP-Cron to publish that awesome new article you had scheduled in the exact, intended moment.

Having your article out of schedule, can lead to a bunch of problems with your marketing plan, most commonly – having your scheduled social media posts link to non-existing content.

Big Web Sites

Big web sites have plenty of visitors, so a missed schedule is a rarity. However, going big brings other problems to the table. Resource on your host tend to get precious. You might not want to have a check for scheduled cron jobs run every few seconds. Or even worse – few times per second.

False Positives

Before providing the actual solution, here are cron commands that either worked on one host and didn’t on another or don't work at all.

The Cron app on the cPanel page of my host. Note that icon and name vary from host to host.
The Cron app on the cPanel page of my host. Note that icon and name vary from host to host.

Just to make crystal-clear what I am talking about here – when you log in to your hosts cPanel → cron Jobs, the content you enter in the Command field is either:

1) http://yoursite.com/wp-cron.php
2) http://yoursite.com/wp-cron.php?doing_wp_cron
3) php http://yoursite.com/wp-cron.php
4) /usr/local/bin/php /home/cpanelUserName/public_html/wp-cron.php
  1. worked on one host and stopped on another

  2. same as 1.)

  3. didn’t work

  4. didn’t work as well

4 commands, 0 success.

The Solution

WordPress is written in PHP which runs on Unix/Linux web hosts. Therefore I needed the most appropriate command for a Unix system and something that would be independent as to how the local environment is set.

We first call the PHP intepretator:

php

and then we add the local path to the script it should run:

/home/cpanelUserName/public_html/wp-cron.php

Local path because we tell the OS to do something. Absolute path would be for addressing something outside the server.

And the full command would be:

php /home/cpanelUserName/public_html/wp-cron.php

I like this one as it would call the PHP interpretator regardless of where it is located (unlike command #4 on the list above).

But that didn’t work as well!

What I didn't realize all along is that WP-Cron checks for $_GET attributes, so it would require HTTP call in order to work. Although the command above is valid, the call is done through a local path.

For this reason we need to have a PHP script that calls WP-Cron through HTTP and then call it through a regular cron command.

So, create a file such as “cron-do.php” in the root directory of your website and add/edit the following code:

<?php
$result = file_get_contents('http://yoursite.com/wp-cron.php?doing_wp_cron');
echo $result;
?>

Basically, this sends an HTTP request to the WP-Cron file with a $_GET attribute. Just what we need!

Now head to your host's cPanel → cron Jobs and create a new cron job. Specify the frequency you want it running and in the Command field add the following:

/usr/local/bin/php /home/cpanelUserName/public_html/cron-do.php

One More Thing

The General Settings page of WordPress 4.4
The General Settings page of WordPress 4.4 | Source

If you are using a newer version of WordPress you might want to double-check you have set your timezone right. You can do so by going WP-Admin → Settings. Depending on the language of your WordPress select either a UTC timezone or a major city corresponding to a UTC timezone.

Testing if WP-Cron.php Works

How Not to Test WP-Cron.php

  1. Create a new cron job that runs every minute.

  2. Head to your WP-Admin → Posts and add a new post.

  3. Use “Test” for title and content of the post, then schedule it one or two minutes from now.

  4. Return to the Posts screen and start refreshing around the time of the scheduled post.

The Actual Way

There is one little detail that fails the test above. It was what first came to my mind, so that's why I decided to leave it as an example.

It's step #4 that breaks the test. That's because you are actually triggering wp-cron.php every time you do a page request.

According to an awesome article on WordPress initialization3, the best way to test for post scheduling is through the RSS feed. WordPress loads wp-cron.php several files after the RSS feed, so if you are requesting the feed, Cron won't load.

Here are the steps:

  1. Cron job running every minute
  2. Add a new post
  3. Schedule it a minute from now
  4. Close the tab with WP-Admin
  5. Visit yoursite.com/feed
  6. Locate that latest post in the XML tree
  7. Keep refreshing every few seconds until your scheduled post appears

Done!

Resources

(in no particular order)

  1. Using cron to Trigger wp-cron.php (bitswapping.com)
  2. Execute PHP script in cron job (stackoverflow.com)
  3. WordPress Initialization (humanshell.net)
  4. Cron Job Tutorial (easycron.com)
  5. Properly Setting Up WordPress Cron Jobs (tommcfarlin.com)

© 2018 ItKnol

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: "https://hubpages.com/privacy-policy#gdpr"

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)