Welcome to the first installment on what I hope to be a semi-regular chronicling of my journey as a full time developer and front-end engineer.
Hi, my name is Adam, and I work at a digital marketing company in northern Colorado. My main job responsibility is to work on client requested custom work. This usually falls within building wordpress plugins, applications, API integrations, and the occasional graphic design task.
I have also been known to take photos, do some woodworking, and hang out in the mountains with my fam.
You can find some of my work on Github,
Unsplash, Dribbble, and elsewhere.
The purpose of this series is just to get me into the habit of journaling my process of learning code. Hopefully my writing will help someone learn something new and useful as well!
Off to the races now. One somewhat interesting thing I did today was look at some issues with a website that is supposed to automatically pull in automotive inventory data on a regular basis. It was failing to do this. Enter the wp-cron for Wordpress.
Wordpress describes the WP-Cron feature as:
"Cron is the time-based task scheduling system that is available on UNIX systems. WP-Cron is how WordPress handles scheduling time-based tasks in WordPress. Several WordPress core features, such as checking for updates and publishing scheduled post, utilize WP-Cron."
For more info visit:
A lot of theme developers and plugin creators have created features that hook into the wordpress cron system including automated social media posting of content, email scheduling, and more. Recently I built a feature so that once a specific estate sale company had finished their estate sale event, the estate sale was automatically deleted from the site 24 hours after it ended.
This wp-cron system can be tough to diagnose when you are trying to hook into it and build something cool with it.
Typically a wp-cron is set up as:
Super Simple One Liner (PHP) <?php wp_schedule_event( $timestamp, 'daily', 'hook_for_cron_event', $args_for_hook ); ?> // $timestamp (integer) // When you want the event to start. // Basically you could set up an event to have it's first execution // happen in the future by setting this to a UNIX timestamp value in the future. // I usually just set this value to time() which produces an immediate timestamp // that way my event starts immediately and conitinues on the inteval of my choosing // $recurrence ie 'daily' (string) // How often the event should reoccur. // The default values are: // hourly, twicedaily, or daily // If you need a different recurrence you can create your own // custom reocurrence schedule. // (look up the filer cron_schedules for more info on custom schedules) // $hook_for_cron_event (string) // This is the name of the action hook that you // want to execute when the event occurs. // This is where your write all the stuff you need to do // $args_for_hook (array) (optional) // Put any arguments that you need to pass into the $hook_for_cron_event // into this array. Being optional, it will default to none if you don't provide any args.
Anywho, if you have to work with wordpress crons or debug their potential issues that they could cause, I have a few resources for you.
WP Crontrol is a plugin that can show you all your currently running wp crons, and also enables you to run these job manually at any time, so that you can debug them easily.
There are a couple reasons that you may want to totally disable the wp-cron funcotinality on a site. The first reason is that sometimes, especially when you are running several sites on the same server, your sites can cause high cpu/memory load because of the tasks that are running during this wp-cron script.
You could have tons of different tasks that are registered to this wp-cron functionality, and they all may not, in reality, need to run as ofter as they are scheduled. If you have tons of sites running on the same server this load can compound and slow down the whole server, or even crash the server completely.
An alternative to this "pseudo" cron is to actually set up traditional, server based, real cron jobs that activate the wp-cron functionality. That way you can control when the wp-cron script runs and set it for a concrete schedule. Your site may only need to really execute a specific wp-cron task every 6 hours, so setting up a regular server cron for this can help a ton.
To learn more about how the wp-cron is different that traditional crons, and also how to disable the wp-cron, you can check out this helpful article from Kinsta.
Traditional server based crontab jobs do admitidly have tough syntax.
Crontab Guru makes writing traditional cron job syntax a bit easier
If you do implement traditional cron jobs, the debugging and monitoring of these jobs can be just as tricky as wp-crons.
Cronitor provides cron monitoring for your servers, and its pretty painless.
Want a quick guide to show you the ins and outs of cron jobs. I refer to this resource from WhoIsHostingThis? all the time.
Do you have tricks or tips to working with wp-cron or regular cron jobs?
A repo of code for common crons you use, or anything else your would like to share?
What else would you like to to ramble about within the subject of code :)
Please let me know your thoughts, and thanks for reading!