How to work with WP-CRON in WordPress

Today I'm going to talk about, what is written only on the English Internet, there is no special information in Russian. I want to talk about such a great invention in CMS as pseudo cron. I will answer right away, that this cron is great for plugins, since it does not require any additional settings on the server, which means that you can develop your plugins and those, who buy your plugin will not make any extra gestures.

A little about the CRON daemon

What is CRON used for? - the cron daemon is most often used to re-process some commands on the site, i used to test external api, from which my plugin pulled data. I needed to check the data once an hour, and if new data is found, then add them to your site.

A simple example of how CRON works on WordPress

So, here is a little code, which does a time check and if the time is up, then a special Hook is triggered.

This code can work like in your theme's functions.php file, and in the plugin you created.

What is in the function myprefix_function_to_run() will repeat every 2 minutes, or rather every 120 seconds, in our case, this is sending a test email. Don't forget to change the text to check ваш-email@wp-admin.com.ua on that, where you will receive emails every 2 minutes.

For those, who is lazy there is a simpler and worse option, worse because, that when developing premium themes or plugins, you will additionally have to do an automatic installation using the plugin TGM Plugin Activation.

We have digressed a little from the topic of conversation., in order not to prescribe code to trigger the automatic execution of your functions (wp cron code, which we discussed above). In order not to understand the code that I showed above, I can recommend a plugin WP Crontrol (WP-Cron Events), for the work of which you will only need to add a function and a hook in the plugin code or file functions.php.

After adding the hook and installing the plugin, activate it, then go to the admin panel in the item Tools -> Control.

wordpress wp coontrol plugin

After the transition, you will see a window with all running processes and even topics, that you didn't create, those that are already running by other plugins. Below you will be able to add a new process.

To add our process (хука myprefix_my_cron_action), we will need to fill in the fields, as shown in the picture below.

wordpress cron control - cron в wordpressConsider the procedure

  1. Hook name - specify the name of the action, which was created before myprefix_my_cron_action.
  2. Next run - indicate, when to start cron execution.
  3. Event schedule - indicate, how often to run a cron event.
  4. After filling out the form, add an event.

After adding, your event will appear in the list of all events.

Show list of cron events

That's actually all, I would advise you to use plugins less and use the first method more, since it is more correct and when developing a plugin, you do not have to teach users how to, that you need to install another plugin and without it, the work of that plugin is impossible, which you are selling.

Good luck developing plugins and using CRON, and I will try to continue to please you with new lessons.


Buy WordPress hosting
/* WordPress tutor
Online tutoring services. List of courses I teach
  • Basic web design course;
  • Site layout;
  • General course on CMS WordPress and continuation of the course on template development;
  • Website development in PHP.
Read more on the page WordPress tutor
*/

Nikolaenko Maxim

Director of web studies ProGrafika. I am developing, website design and promotion. Always glad to new blog readers and good clients.


You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Templates for WordPress
The best hosting in Ukraine
Stable hosting for Drupal