438 – WP Cron and Action Scheduler
Is there a plugin for that?
With more than 50,000 plugins in the WordPress repository, it’s hard to find the perfect one. Each week, I will highlight an interesting plugin from the repository.
For more great plugins, download my 50 Most Useful Plugins eBook.
Notice Manage is a lightweight plugin that lets you organize and hide notifications from your WordPress Dashboard.
WP Cron and Action Scheduler
What is Cron?
Well, first let’s explain what “regular” Cron is…Cron is a technology used for time-based, task scheduling that is available on UNIX systems. Most servers use it to run scheduled tasks for maintenance. WP Cron is how WordPress tackles scheduling time-based tasks. In reality, it’s kind of a “fake” Cron, and although similar, it works a little bit differently than its counterpart. WP Cron kicks in every time a visitor opens your site (or when a page is requested), unlike “regular” Cron, and it runs separately in the background to avoid delays in loading times. Because it’s dependent upon site traffic, however, this can be problematic if you don’t have a lot of site volume. Your scheduled actions will be delayed if the site is not frequented often. On the flip side, if you have too many visitors, this can also cause its share of trouble
Examples of scheduled tasks that can be performed with WP Cron include (just to name a few):
– Checking for theme or plugin updates
– Deleting old comments from the trash
– Scheduling product sales and prices
– Sending emails (ex- Bookings emails)
How to Make Sure Cron Runs
- Log into cPanel
- Navigate to Advanced > Cron jobs (in Plesk it might be something like “Scheduled Tasks”)
- Add New Cron Job and specify the interval you’d like to use
4.Enter this for Command (be sure to change the URL to match your site):
wget -q -O - http://yourdomain.com/wp-cron.php?doing_wp_cron
- Add New Cron Job
- Add to your wp-config:
By taking these steps, you are no longer relying on “virtual” cron, and you are triggering WordPress’s
wp-cron.php script to run in the interval you determined above.
Install either of these plugins:
According to ActionScheduler.org, Action Scheduler (AS) is a “scalable, traceable job queue” that is designed for processing large queues of tasks in WordPress. It’s used everyday by folks in the WordPress Community to process millions of payments for Subscriptions, webhooks for WooCommerce, as well as events and emails for a range of plugins
Thank you to those who use my affiliate links. As you know I make a small commission when someone uses my link and I want to say thank you to the following people. For all my recommended resources, go to my Resources Page
Full TranscriptBusiness Transcription is provided by GMR Transcription.
On today’s episode, we are going to talk about something that we don't really ever talk about, and it's the WP Cron and the Action Scheduler. We'll talk all about that on this show right here on Your Website Engineer podcast Episode No. 438. Hello, and welcome to another episode of Your Website Engineer podcast. My name is Dustin Hartzler, and I’m excited to be here with you today, because it is another great day in the WordPress space. We're learning all kinds of things.
I'm continuing to learn each and every day, and I'm excited to bring things that I learn to the show, and can help to educate and explain some of these behind-the-scenes things of what's happening with WordPress, and what goes on behind the scenes. We’ll get to that in just a couple of minutes, but first let's go ahead and talk about the news and a plug-in to share with you this week. The first news item this week is WooCommerce 3.6 has been launched, and this happened last week, but WooCommerce has been under development since November of 2018, and since that time, there have been 2,279 commits and 122 different contributors.
So WordPress, or WooCommerce 3.6, is actually titled a minor release. It’s backwards-compatible with all the rest of them, but they've added some really cool things. The first thing is product blocks. We’ve talked about blocks in the past, but now you have the ability to add these different blocks to a WordPress post or page, which makes it really easy to talk about a blog post, about a product that you may happen to have on your store, and then add it directly to your page. You can do blocks that have products by category, best-selling products, handpicked products, newest products, on-sale products, top-rated products, products by attribute, and featured products.
So you can do all of those now with WooCommerce 3.6. There have also been a lot of performance improvements. Customers and store owners will experience noticeably faster experiences, since there have been some performance enhancements behind the scene, and this is both on the admin pages and the front-end load time pages. And there's a link in the show notes for the release notes, and you can find out everything that's there, but basically, it's a 62% improvement on the load time when ordering and filtering products, there's a reduced overall load time by bypassing some inactive webhooks, things that aren’t needed, and just a lot of really cool behind-the-scenes things.
It has taken a long time, and the team is really excited to push this out here into the world. I know that there was a point release that happened because of some sites were acting kind of goofy that were on WP Engine, so 3.6.1 is the current version, and if you're running a WooCommerce website, I highly recommend updating it to the latest version. The second piece of news that I want to share with you today is all about Dashicons, and this is another thing that we don't talk about that often, but Dashicons are those a little icons that show up across the WordPress dashboard.
They show up a few different places, but those are the little icons, usually to the left of the menu items if you're using a left-to-right version of WordPress. And what that is is in those icons, there's a ton of them that are already built into WordPress, but they're adding 13 new ones here in WordPress 5.2. And so this is something that really hasn't been updated in the last few years, but there's a bunch of new icons that are coming. There's 13 new that were added, but then there's also an additional 18 that were previously included in font files, but didn't have the right CSS, and so there's a link in the show notes, and you can see all of those and how they're going to be added to Wordpress 5.2.
So we'll keep an eye out of those when we're getting closer to that release. Moving on to the last segment here before the main part of the show, and that is – is there a plug-in for that section? And today I want to talk about a plug-in that's called a notice manager, and I'm usually not all about these types of plug-ins, mainly because it's a plug-in that allows you to organize the notifications and basically turn them off if necessary, and usually we want to make sure that those things are on all the time, but the cool part about this plug-in is it would allow you to turn off notifications for those websites that you don't want people actually clicking on the links and updating plug-ins or reconfiguring things.
So maybe if you are managing a site and you’re managing a site for somebody else, you could turn off the notifications just so they don't see that oh, the database needs an update, and this needs updated, and this plug-in’s up to date, and you need to add your API key for this plug-in. You can manage all of these things with this plug-in, and then just make it easier on the end-user when they're actually using your website. So that's the plug-in I want to share with you; it’s called notice manager. You can find it in the show notes for Episode No. 438, or you can just search on the WordPress repository for notice manager.
We are going to talk about WP Cron Action Scheduler today, and this came up because there was an internal post for the WooCommerce support team, and it was written by one of my colleagues, and she wrote this big long post, and it was all about how the WP Cron works, how Action Scheduler works, and I thought, hey, this is valuable content and it needs to be released here on a podcast episode. So I'm going to go ahead and basically read through this post, and I'll have links in the show notes for different things, and so that way we can get a good understanding of what this is. But it starts off with a story, and so I am going to read the story.
So it says, imagine you're robbing a bank. Of course you cannot go to do this job alone; it would be too risky an attempt to steal from the bank the $20,000 you still owe on your student loans, and it says, come on, we're just pretending. You need to help a driver, or you need a driver to help you out, so you recruit one of your friends to be a driver, and your plan of action, you and your friend decide that you have exactly two minutes to go into the bank and gather the funds you need. And I say ‘gather’ because this is a friendly robbery, remember? And as we continue to plan, however, you discover that your friend’s watch is just a tad off from yours.
And when I say just a tad, I mean it only works when your friend sneezes. Can you trust this? It's not very reliable, is it? Now how would you make a safe getaway? You wouldn't. So reliability is important. Scheduling actions and timing is important, so that's what WordPress Cron and the Action Scheduler is all about. We’re gonna talk about some tools that are important for timing WordPress and the other plug-in actions, similar timing needed in the scenario above. So like it's explained here, you want to make sure that when you schedule something to go out, or schedule something to happen, you want it to actually happen.
A great example is this week, Adam Silver’s show over on Kitchen Sink WP, it didn't fire; it didn't go out on time. And it was one of those things that the Cron job just didn't run, and so what happens is – I guess let’s talk about what the Cron is. First, the Cron is a technology used for time-based task scheduling that's available in UNIX systems, so most servers used to run scheduled tasks for maintenance. The WP Cron is how WordPress tackles scheduling time-based tasks. In reality, it's kind of like a fake Cron, and although it's similar, it works a little bit differently than its counterpart, regular Cron.
WP Cron kicks in every time a visitor opens your website, or when a page is requested, or refreshed, or whatnot. So unlike a regular Cron, it runs separately in the background to avoid delays in loading time. Because it's dependent on site traffic, however, this can be problematic if you don't have a lot of site volume. And your scheduled actions will be delayed if the site isn't frequented often enough. If people don't come to your site, WP Cron’s not going to work. But on the flip side, if you have too many visitors, this can also cause its share of problems.
I'm going to guess that my friend, Adam, just didn't have enough traffic that early in the morning when it was time for his show to be published, and that's why it missed the scheduled publishing time. Why use it then? Let's say most hosting services are shared, and people don't have access to the system Cron, so therefore, the WP Cron is useful in a practical way of providing the timing it said. So it's simple to set up. WP Cron will always run tasks no matter how old they are, because they will sit in a queue until the page is loaded. The neat thing is that plug-ins can actually hook into this, too.
However, using it recklessly can lead to slight slowdowns, especially on shared hosting, so it needs to be used responsibly. The WP Cron has three different scheduling interviews. It has hourly, twice daily, and daily, and if you need more than that, you can actually set up custom intervals that can be used. And so you can have it go off every five seconds, or every three-and-a-half minutes, or whatever the case may be. Five seconds, three minutes, is a bit frequent, but you can adjust accordingly.
Examples of scheduled tasks that can be performed with WP Cron, and this is just to name a few, but you can check for theme or plug-in updates, you can delete old comments from trash on a specific schedule, you can schedule product sales or prices, you can send emails out with plug-ins like follow-up emails, or the WooCommerce order emails, and all those types of things. So those are some of the things that are done when WP Cron runs, so what if I don't get a lot of visitors to my site? How do I rectify this? How do I make sure that my things that are scheduled actually happen?
Well, there are two different routes that we can handle this. The first way is harder, though it’s the more recommended route, but then there's also a less advanced, but easier route. So let's go ahead and dive into the harder route first. The first thing you need to do is to set up a Cron job by performing the following steps of logging into your cPanel, you navigate to Advanced, and then Cron jobs; it might be called something like Schedule Tasks.
So you can go in there, you can add a new Cron job, and specify the interval you'd like to use, and then you can add a command, and this will be in the show notes as well, and then you add the new Cron job, and then you add a line to your WTP Config file, which is basically turning off the WP Cron. And then by taking these steps, you're not relying on the virtual Cron; you're triggering WordPress Cron script to run at the interval you determined above. And so if you wanted to make it every 15 minutes, and so it would go four times an hour, you would specify that in the cPanel route. So you can go ahead and do that; it's not super complicated. Again, all those steps will be in the show notes for Episode 438.
But the easier way to go is you can use a plug-in. The solution to make it a little bit easier, if you want to keep an eye on WP Cron, but you don't like to use the command line, or if you don't want to go into your host’s account, you can use WP Cron Control and WP Crontrol. And so those are the two different versions that are there. Basically, when you add either of these plug-ins, it will give you the events that are happening, and it will basically tell you what hook they're running on, so maybe they're the Action Scheduler, or maybe there's a jetpack clean, or there is a WordPress version check, or a plug-in check, a theme check, and then you can see when those are going to happen.
It shows your reoccurrence, so maybe it checks every minute, maybe it checks once every 10 minutes, once an hour, twice-daily. It’s giving you all that information in an easy-to-consume page that makes it really easy to see what's coming up next, and it kind of tracks everything that's happening. Let's talk about the Action Scheduler. What's that about? According to actionscheduler.org, the Action Scheduler is a scalable, traceable job queue that's designed for processing large queues of task within WordPress.
It’s used every day by folks in the WordPress community to process millions of payments for subscriptions, webhooks for WooCommerce, as well as events and emails for a range of plug-ins all happening on WooCommerce.com. It's really good for batch processing. Action Scheduler sends batches of 20 or so, so it doesn't exhaust the PHP memory, and it can process more than one batch at a time. And it can actually do a max of five queues at a time, and so it it's basically processing batches of a hundred actions at ten times at a time at five queues instead of one. So really, it can do a lot of things.
So it basically uses a WordPress custom post type named Schedule Action, to store the hook name, the arguments, and the scheduled date for an action that should be triggered at the time in the future. And then it will attempt to run every minute by attaching itself to a callback in the Action Scheduler run schedule hook, which is scheduled using the built-in WP Cron system. And so, basically what that is is it's just a big queue, and it’s checking every minute to see if it's time for another – should I run this? Should I do this? Should I check for plug-in updates? Should I do this? Should I do that? That's exactly what it's doing.
And so you can see this on – WooCommerce has one of these built-in, so if you have WooCommerce installed, you can go to WooCommerce status scheduled actions, and you can see it in there, and you can see what's coming up, if you have maybe follow-up emails, or different things that are happening at a scheduled interview after something happens, you can see that in the Action Scheduler inside of WooCommerce. So what you can do with this is you can run a pending action, or you can view the scheduled actions with a specific status, you can view log entries on why an action failed, you can sort actions by hook name, or schedule date, or group name.
So the differences in the two: the WP Cron is suitable for smaller sets of tasks, it's pre-configured with WordPress, and it's stored in the options table in the database. But the Action Scheduler is better suited for larger sites with larger sets of tasks. It’s the installation required, but it's included with WooCommerce, so you can add – it's automatically added with WooCommerce, but otherwise, you have to add an Action Scheduler, and then the events are logged in the WP comments table in the database. And how they are similar is they can work with different hosting companies, there's no server access required, they can be used or managed as a plug-in, and they can also be triggered with WP CLI commands.
And so I know it's a little bit – it's a complicated issue, and it's a complicated thing, but knowing the difference of what these two things are, the WP Cron and Action Scheduler, knowing the difference between them can kind of help you troubleshoot and figure out what might be going on on your site. And the great example is going back to Adam and his example today, like yesterday, when his show didn't come out, it's, what happened there? You could go and you could look in the Action Scheduler if he had that installed, and you could see exactly why that show did not fire, and why didn't it publish at the exact time.
Or if maybe you have a follow-up email that’s supposed to go out 10 minutes after somebody purchases something, and it doesn't go out, you can go in and you can see in the Action Scheduler what happened after 10 minutes, and what happened. Why or why not did it not work? And so that's something cool that you can do.
So, in a nutshell, you'll find a lots of different – you probably find lots of different reasons for digging into the Cron jobs or the scheduled actions, and so this is just a kind of a brief overview of talking through why that looks like it does, and what the differences between the two are, and how you can use this – kind of know the right area to go for once something that’s supposed to be automated, that's working in the background, doesn't go or doesn't fire, then I would highly recommend installing the WP Cron Control or the WP Crontrol plug-ins, and that will give you a nice user interface where you can see exactly what's happening and how things are working.
So that's what I wanted to share with you this week. Hopefully it wasn't too technical, and it made a little bit of sense. Next week, we'll continue talking about Word Press, and until then, take care and we'll talk again soon. Bye-bye.