From 297cef1c5c8cd55dea9a0dd455c642741e350fde Mon Sep 17 00:00:00 2001 From: COQUARD Cyrille Date: Sat, 23 Nov 2024 10:30:04 +0100 Subject: [PATCH 1/2] Added first version from the script --- .../12-Cron/01-Introduction-to-WP-Cron.md | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 plugin-developer-learning-pathway/12-Cron/01-Introduction-to-WP-Cron.md diff --git a/plugin-developer-learning-pathway/12-Cron/01-Introduction-to-WP-Cron.md b/plugin-developer-learning-pathway/12-Cron/01-Introduction-to-WP-Cron.md new file mode 100644 index 0000000..bae44b1 --- /dev/null +++ b/plugin-developer-learning-pathway/12-Cron/01-Introduction-to-WP-Cron.md @@ -0,0 +1,34 @@ +# Introduction to WP Cron + +On some occasions, we need to have some actions that need to be executed periodically instead of executing when a user has an interaction. + +A good example of this would be a subscription. + +First, we would need some logic to proceed with the registration from the customer to the subscription with some usual logic. + +Then we would have to bill the user monthly, and for this part of the logic we would need a Cron. This is due to the fact we don't want the customer to pay manually each month, but instead we want the payment to proceed automatically. + +## Understanding how Cron works + +A Cron is always based on two parts: +- A recurrence that will define the time when the Cron are executed. +- A task that will be executed when the Cron is fired. + +## WP Cron + +Real crons needs to be settled up by the WordPress user to be able to run, which is not something straightforward nor an easy process. + +That is why WordPress introduced WP Crons, they are emulated crons that run by default on a WordPress website when real crons are not configured. + +That way, users are not left with a broken website if they forgot to set up the crons. + +However, WP Cron also have their own drawbacks compared to crons. + +They rely on a user to load the page to run, and so they can be unreliable if you have little traffic on your website. + +## Switching from WP Cron to Cron + +At the level of the plugin, it is not possible to know of we are using WP Crons or Crons as it will be handled inside WordPress configuration. + +This, even if it can seem like a problem, is in fact good news for us plugin developers. +This due to the fact there will be one syntax to register Crons and WordPress will handle us which one from WP Crons or Crons will be used. \ No newline at end of file From 1dcafc451e3c5fce5467ae118681d1e5287f25cb Mon Sep 17 00:00:00 2001 From: Jonathan Bossenger Date: Tue, 3 Dec 2024 17:32:56 +0200 Subject: [PATCH 2/2] Update 01-Introduction-to-WP-Cron.md --- .../12-Cron/01-Introduction-to-WP-Cron.md | 54 +++++++++++++------ 1 file changed, 38 insertions(+), 16 deletions(-) diff --git a/plugin-developer-learning-pathway/12-Cron/01-Introduction-to-WP-Cron.md b/plugin-developer-learning-pathway/12-Cron/01-Introduction-to-WP-Cron.md index bae44b1..78cc363 100644 --- a/plugin-developer-learning-pathway/12-Cron/01-Introduction-to-WP-Cron.md +++ b/plugin-developer-learning-pathway/12-Cron/01-Introduction-to-WP-Cron.md @@ -1,34 +1,56 @@ # Introduction to WP Cron -On some occasions, we need to have some actions that need to be executed periodically instead of executing when a user has an interaction. +In some instances, you might need a way to trigger an action that needs to be executed at a specific time interval instead of being based on a user interaction. -A good example of this would be a subscription. +More commonly known as scheduled tasks, these actions are often used to automate repetitive tasks that need to be executed. -First, we would need some logic to proceed with the registration from the customer to the subscription with some usual logic. +Let's look at some examples of scheduled tasks that you might need to automate, and what this looks like in the context of a WordPress site. -Then we would have to bill the user monthly, and for this part of the logic we would need a Cron. This is due to the fact we don't want the customer to pay manually each month, but instead we want the payment to proceed automatically. +## Common examples of scheduled tasks + +Some common examples of scheduled tasks your plugin might need to offer include sending subscription reminder emails to users, automatically publishing scheduled content, or regularly checking an external data API for updates. + +The subscription reminder email might need to be sent one week before the end of the month, the scheduled content might need to be published at 8:00 am every day, and the data API updates might need to be checked and imported every 12 hours. + +A web server also has its own scheduled tasks, such as checking for updates, backing up the database, and clearing the cache. + +These tasks are automated using a command line utility known as cron, which gets its name from the word Chronos, the Greek word for time. ## Understanding how Cron works -A Cron is always based on two parts: -- A recurrence that will define the time when the Cron are executed. -- A task that will be executed when the Cron is fired. +On a web server, a scheduled task is known as a cron job, and it's always made up of two parts: + +- A recurring time when the task will be executed. +- A task that will be executed when the cron job is run. + +Cron jobs are defined in a file called a crontab, which is a configuration file that contains a list of cron jobs. -## WP Cron +But being able to create a cron job requires access to the server's crontab, as well as understanding the crontab syntax. -Real crons needs to be settled up by the WordPress user to be able to run, which is not something straightforward nor an easy process. +This is not something that all WordPress site owners have, and even if they did, it's not a straightforward or easy process. -That is why WordPress introduced WP Crons, they are emulated crons that run by default on a WordPress website when real crons are not configured. +## Introducing WP-Cron -That way, users are not left with a broken website if they forgot to set up the crons. +That is why WordPress introduced WP-Cron. -However, WP Cron also have their own drawbacks compared to crons. +WP-Cron emulates server crons and allows WordPress developers to create scheduled tasks without needing to access the server's crontab. -They rely on a user to load the page to run, and so they can be unreliable if you have little traffic on your website. +WP-Cron is implemented by making an internal HTTP request to the wp-cron.php file in the root of the WordPress install whenever the site receives a visit. + +This will then will trigger any events that have been scheduled and stored in the WordPress database, by using the relevant WordPress scheduling functions. + +While this makes it easier for plugin developers to implement scheduled tasks, it does have some limitations. + +The primary limitation is that it relies on a user to load the page to run. This means it can be unreliable if the site has a drop in traffic, as the scheduled tasks may not run when expected. + +Fortunately, WordPress does provide a mechanism to overcome this limitation, which you can learn about in lesson on Hooking WP-Cron Into the System Task Scheduler ## Switching from WP Cron to Cron -At the level of the plugin, it is not possible to know of we are using WP Crons or Crons as it will be handled inside WordPress configuration. +As the developer of a plugin, it is not possible to know of a site that has your plugin installed is using the default WP-Cron implementation or server crons, as this will be different on every WordPress configuration. + +While this might seem like a problem, is in fact good news. + +This is because the mechanism for hooking WP-Cron into the system cron makes use of the same core functionality. -This, even if it can seem like a problem, is in fact good news for us plugin developers. -This due to the fact there will be one syntax to register Crons and WordPress will handle us which one from WP Crons or Crons will be used. \ No newline at end of file +This means as long as your plugin uses the WordPress scheduling functions, it will work regardless of whether the site is using WP-Cron or server crons. \ No newline at end of file