WordPress-Server-Ubuntu-24.04
5 Cron and Emailing

Configuring WordPress Cron Jobs and Outgoing Email with WP Mail SMTP

In the previous chapter, we covered WordPress caching. Now, we'll delve into configuring WordPress cron jobs and setting up outgoing email.

WordPress Cron Jobs

  • WordPress includes a built-in task scheduling system, allowing specific actions to be performed at set intervals. By default, WordPress uses its cron system to handle:

  • Automatic updates (for security fixes)

  • Checking for core updates

  • Plugin and theme update checks

  • Publishing scheduled posts

Why Use a System Cron?

While convenient, WordPress’ cron is triggered only when a page is requested. This can lead to missed or delayed scheduled tasks, particularly if your site has low traffic or uses aggressive caching like Nginx FastCGI Cache (discussed earlier). Page caching prevents WordPress from processing each request, which can delay cron jobs.

On high-traffic sites, the opposite problem can occur: the server can become overwhelmed by checking for cron jobs on every request.

To overcome these limitations, it’s best to offload cron to the system's cron daemon, available on Linux and Unix-based systems. Unlike WordPress cron, system cron runs independently of page requests and is triggered by the server's system time, ensuring more reliable execution.

Disabling WordPress Cron

To use the system cron, start by disabling the built-in WordPress cron. Add this line to your wp-config.php file:

define('DISABLE_WP_CRON', true);

Configuring System Cron (Crontab)

On Linux systems, scheduled tasks are managed using a text file called crontab. Each line in this file represents a cron job. If you manage multiple WordPress sites on one server, you’ll need a cron entry for each site and should stagger them to avoid resource overload.

Basic Cron Syntax:

* * * * * command_to_run

Each * represents a time field:

  • Minute (0-59)
  • Hour (0-23)
  • Day of the month (1-31)
  • Month (1-12)
  • Day of the week (0-7, where 0 and 7 are Sunday)

For example, to run a task every day at midnight:

0 0 * * * command_to_run

Cron Cheatsheet:

ExpressionDescription
* * * * *Every minute
*/5 * * * *Every 5 minutes
0 0 * * *Every day at midnight
0 0 * * 1Every Monday at midnight
0 9 * * *Every day at 9 AM
0 0 1 * *Every 1st day of the month at midnight
0 0 1 1 *Every New Year’s Day

Example: Running WordPress Cron Every 5 Minutes for www.example.com

To configure the cron job for WordPress, the cron should be executed as the www-data user. Here’s how to set this up:

  1. Switch to the www-data user: Since www-data typically doesn't have a password, you can edit its cron jobs using:

    sudo -u www-data crontab -e
  2. Add the cron job: Once you're in the www-data user's crontab, add the following cron job that runs every 5 minutes:

    */5 * * * * cd /var/www/www.example.com/public; /usr/local/bin/wp cron event run --due-now >/dev/null 2>&1

    This command navigates to the WordPress installation directory and triggers WordPress cron jobs using WP-CLI. The >/dev/null 2>&1 part ensures no output is generated or emailed.

  3. Check Permissions: Ensure that the www-data user has the necessary permissions to access the WordPress installation. You can update the file ownership to www-data using:

    sudo chown -R www-data:www-data /var/www/www.example.com/public

This ensures that the cron job will run with the appropriate permissions, without requiring excessive privilege escalation or insecure methods.


Outgoing Email with WP Mail SMTP

WordPress relies on email for essential functions like password resets and admin notifications. To ensure reliable email delivery, we recommend using a third-party service via a plugin like WP Mail SMTP.

Why WP Mail SMTP?

Sending emails through your web server can lead to poor deliverability or security risks. WP Mail SMTP helps solve this by routing emails through an external email service (such as Gmail, SMTP, or a dedicated transactional service).

Step-by-Step Guide to Set Up WP Mail SMTP with Google API

  1. Install WP Mail SMTP:

    • From your WordPress dashboard, navigate to Plugins > Add New.
    • Search for WP Mail SMTP and install the plugin.
    • Activate the plugin.
  2. Configure WP Mail SMTP:

    • Go to WP Mail SMTP > Settings.
    • Under the "Mailer" tab, select Google / Gmail.
  3. Create a Google API Project:

    • Visit the Google Cloud Console (opens in a new tab).
    • Create a new project.
    • Navigate to APIs & Services > OAuth consent screen, and configure the consent screen.
    • Under APIs & Services > Credentials, create new credentials (OAuth 2.0 client ID). Set up the authorized redirect URI using the URL provided in the WP Mail SMTP settings.
    • Copy the Client ID and Client Secret from Google and paste them into WP Mail SMTP.
  4. Authorize Your Gmail Account:

    • After saving the settings, click Allow plugin to send emails using your Gmail account.
    • Google will prompt you to log in and grant access to the WP Mail SMTP plugin.
  5. Test Email Delivery:

    • WP Mail SMTP provides a testing feature under WP Mail SMTP > Email Test. Enter an email address to send a test email to verify everything is working.

For additional setup and troubleshooting, refer to the official WP Mail SMTP documentation (opens in a new tab).


WordPress Plugin Directory

WordPress plugins are stored in the /wp-content/plugins/ directory of your WordPress installation. You can upload new plugins by:

  • Using the WordPress admin area under Plugins > Add New
  • Uploading plugin files via FTP/SFTP to /wp-content/plugins/

Once uploaded, plugins need to be activated in the WordPress admin dashboard to function.


With cron configured and WP Mail SMTP set up, your WordPress site should now reliably handle scheduled tasks and outgoing emails.