Cron is a time-based job scheduler and WordPress uses a file called
wp-cron.php as a cron job to run scheduled tasks like publishing scheduled posts, managing updates to plugins and themes etc. WordPress is configured to
wp-cron.php every time a user visits your WordPress web app to check if a scheduled task is to be executed.
If your web app does get hundreds of users or more simultaneously , this adds significant overhead to your web app slowly it down for the users visiting it. For such cases it would be beneficial to isolate the
wp-cron.php execution from the request made to your web app. Azure app service offers Web Jobs or Azure functions to build make a call to
wp-cron.php file when it needs to be executed.
Advantages of Functions over Web Jobs for your WordPress app
Few things to understand about your application before you make the choice :
- If the cron job is CPU intensive and takes a long time to execute , it may put a large burden on your existing resources on the App service plan causing overhead. In such cases it would be more beneficial to use Azure Functions.
- Cost is the second criteria . With Azure functions you get 400,000 GB-s execution time for Free which might be sufficient for WordPress app. Even if the execution is higher than 400,000 GB-s , the cost is 0.20 cents for million executions. Functions is still cost effective and it does not add overhead on your web app compute resources.
For this blog post I will show you how to use
Azure Functions so you can leverage server less architecture and low cost since you pay for the number of times the code is executed due to the event based model.
You can use the same
sample script for Web jobs if you intend to use the resources on your we app's app service plan. Click
here to learn more about Web Jobs.
Follow the steps below to isolate
wp-cron.php execution for your WordPress web app.
STEP 1: Disable WordPress cron
Update
wp-config.php file and add this setting shown below to disable
wp-cron.php from being executed at every user request.
define('DISABLE_WP_CRON', 'true');
STEP 2 : Create a Function App using Timer trigger
Refer to instructions here to
create a Function app or use this
sample function app to run WordPress cron on Github . Azure function supports
continuous deployment with Github .
Traditionally
wp-cron.php executes at every request to check if a task is scheduled to run. You can alternatively use the timer triggers which calls the azure functions based on a schedule, one time or recurring. For example if you want the
wp-cron.php to run every 15 mins you can use the following schedule which is Unix Cron like expression.
"schedule": "0 */15 * * * *"
To learn more about Timer trigger binding , click
here.
There are two primary files :
- function.json: This file specified the bindings for the function app. In this case its a timer trigger as seen below which is scheduled to run every 15 minutes. You can view the json file here.
{
"bindings": [
{
"type": "timerTrigger",
"name": "timerInfo",
"direction": "in",
"schedule": "0 */15 * * * *"
}
]
}
- run.php : This script just make a HTTP call using PHP CURL and gets a response . Click here to view the script. The logs will display the information of function starting , completing and the response received from the CURL call to wp-cron.php. In this example with the sample function app return a message "Running WordPress CRON .."
Note: PHP support is still experiment for Azure Functions. You can use C# or Node.js or F# languages to make a HTTP call to the wordpress app wp-cron.php file , for example the URL would be like http://wordpress3295.azurewebsites.net/wp-cron.php?doing_wp_cron. This will trigger the cron to kickoff any scheduled tasks for WordPress app.
References
Developer Reference for Azure Functions
Best practices for Azure fuctions