I use CloudFlare for my site, and for the most part find it works fairly well, except at times I have cron issues. I could set up custom page rules, but in my case, it is just more overkill than I need. Initially, I had set up a cron job which used curl to fetch the wp-cron.php file from a temporary URL (e.g. server_hostname.host.com/~username/wp-cron.php) however sadly the temporary URL feature was depreciated.
Once this had been depreciated, I started to search around for other ways to run wp-cron local to the server and discovered that with hostgator (affiliate-link), even on shared servers, the wp-cli extension is installed (as at 18/1/2017). This is a command line interface for WordPress which allows you to interact with your WordPress installation via ssh / shell.
The command structure is fairly straight forward over SSH, simply calling wp then the command. Because the extension is installed in a different directory to that of WordPress the command structure changes to:
wp cron event run --due-now --path="/home_dir/username/public_html"
Running the above command will, using the wp command line, trigger the cron for the WordPress installation in public HTML (for a single site installation) and run any cron event which is due now, also reporting what was run via shell. This is a great feature, and I was keen to give it a go. Thinking it would be easy I simply copied the above into the server cron (using CPanel).
The cron job ran, but gave the error below;
/bin/sh: wp: command not found
This error was because the aliases were not set beyond the SSH, so if we wanted to use WP-CLI, we would need to define the path(s) for PHP (since it is a PHP application), the application filename (wp-cli.phar), and the wp commands and any final parameters since we did not have full access to the server (e.g. as root).
I spent some 45 minutes on the support line to Hostgator (affiliate-link) but managed to find a way around this using the below. If you knew where the wp-cli.phar file resided on the server, you could skip the (once off) download procedure and just change the path to that, but since I did not know, I had to download it using the below.
Log into SSH on the server,
Change the Dir to your home directory e.g. /homex/username/
execute the command
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
This should download the wp command line application to your home directory, and once that is done you are ready to set up your cron job. It is important to note that without the phar.so extension none of this will work!
Once the above is set up, you should be ready to configure the CPanel Cron job using the below command
cd /home_dir/username/public_html; /opt/php70/bin/php -d extension=phar.so /home_dir/username/wp-cli.phar cron event run --due-now --path="/home_dir/username/public_html" >/dev/null 2>&1
In the above code, you will need to replace the home_dir and username with what you have been supplied (you can find this in your CPanel), the location of wp-cli.phar if it is not installed in your home directory (e.g. /home_directory/username/), the PHP location if you are not using PHP7.0, and finally the path to your WordPress installation if it is not in the public_html directory.
I hope this helps, have any issues why not post it in the comments…
Comments are closed.