Categories
Plugins WordPress

🚨Get Slack notifications when your WordPress website is down

Since version 5.2, we now have a native recovery mode in WordPress. The feature automatically triggers itself when your website is down, mostly because of some PHP fatal errors.

When your website is in recovery mode, an email is sent to the owner of the website (the email used in Settings > General) to alert them that there is an issue. The email contains some very useful information to help to debug the issue. The recovery mode also allows to access the broken website with the eventual guilty plugin/theme deactivated.

🐰The email alert rabbit hole

But when you’re in charge of many websites (as we are at Whodunit Agency), this email may not be the best way to send you an efficient alert. First, the email address may not be yours but your client email so you won’t receive the alert. Secondary, emails are not synchronous enough when your are in charge of many websites or when your are running a website for a big company. Third argument… who still reads emails in 2020?!🚪🏃

‍😉

🍾A free WordPress plugin to be notified on Slack when your WordPress site is broken

Samy Kantari (one of our beloved developers) and I developed a nice little WordPress plugin to automatically send alerts to our company Slack team, in a dedicated Slack channel.

After having developed, tested and implemented it in many of the websites we are maintaining, we decided to release it publicly on WordPress.org.

Say hello to WP Down Slack Alert, a WordPress plugin that send notifications to your Slack team when your websites are down! 😍

⚙️How does it work?

Once the plugin is installed and activated on your WordPress installation, there is a new settings screen available in Tools > Slack Alert.

The plugin provides a full set of settings to easily customize your own Slack notification system:

  • Deactivate WordPress native email notification 💌
    If you (or your client) don’t want to be notified by email when the website is down, just check this option to get rid of emails 🙂
  • Slack notifications frequency 📅
    Every 30 minutes, 1, 2, 6 or 24 hours, choose the frequency you want to be notified when your website appears to be broken
  • The Slack channel where you want to be notified
  • Name of the custom Slackbot we should use 🤖
  • Custom Slackbot avatar image for the plugin
  • Notification title
  • Custom Slack notification footer message
    Pro-tip: if it’s for a client, customize it with the name of your company!
  • Activate or deactivate our “Green notifications” 🍏
    It’s an automatic notification that is sent when the website is back in business.
The whole settings screen in WP Admin

Be creative!

Here is a example of a customized Slack notification:

WordPress recovery mode slack notification example

Our plugin needs a Slack API token to connect to your Slack team and be able to send notifications.

🔗Easily connect your WordPress website to your Slack team

To easily get your Slack API token, the plugin settings page contains a small tutorial with screenshots. This only takes few minutes and you won’t need any technical skills.

Technically, the plugin works with a mu-plugin (must-use plugin) that is automatically generated and activated once the plugin is activated on your WordPress install.

We use a mu-plugin because when the recovery mode is triggered, it may deactivate your plugins, but not your mu-plugins. Therefore, when the website is broken and in recovery mode, our mu-plugin takes over and send its notification to your Slack team.

It was a pretty simple idea, somebody just had to think of it 🙂

🚀For developer’s goodness: an efficient deployment method

Icing on the cake: we also thought of agencies and freelancers who manage a large number of sites! If you manage a large amount of websites, it can be quite boring to login to the back office of each site to configure the plugin.

As it’s also our case, we used a very simple a mechanism to configure the plugin without having to log in to the website: PHP constants.

The following PHP constants are available and allow you to activate, deactivate or configure each setting of the plugin. Here is the list of constants that can be used with an example of use for each of them.

// Disable the admin settings screen (false to disable)
define( 'WPDSA_SETTINGS', false );

// Define the Slack API Token (string)
define( 'WPDSA_NOTIFICATION_TOKEN', 'qcsqkjcssjcksqh' );

// Define the notification channel (string - slugified)
define( 'WPDSA_NOTIFICATION_CHANNEL', 'my_slack_channel' );

// Disable the notification email (true to disable)
define( 'WPDSA_NOTIFICATION_DISABLE_EMAIL', true );

// Define the Slack notification recurrence (string - accepted values: '0,5', '1', '2', '6', '24', 'anytime')
define( 'WPDSA_NOTIFICATION_RECURRENCE', '2' );

// Define the notification bot name (string - slugified)
define( 'WPDSA_NOTIFICATION_BOTNAME', 'My_bot_name' );

// Define the notification message title text (string)
define( 'WPDSA_NOTIFICATION_MESSAGE_TITLE', 'My message title' );

// Define the notification message footer text (string)
define( 'WPDSA_NOTIFICATION_MESSAGE_FOOTER', 'My message footer text' );

// Define the notification message image (string - absolute link to an online image)
define( 'WPDSA_NOTIFICATION_MESSAGE_IMAGE', 'http://assets.whodunit.fr/brand/logo_whodunit_petit.png' );

// Disable the "Green" notification message (true to disable)
define( 'WPDSA_NOTIFICATION_DISABLE_GREEN', true );

You can use these constants in the wp-config.php file of your WordPress installation or in your own mu-plugin. We chose this last option to deploy it automatically on our 150 sites under maintenance!

You may also want to download this sample plugin and adapt it to define your own PHP constants:

To use it, just download and unzip it, open the template.php file and modify the values of the constants to fit your needs. Then just re-zip the extension folder. Finally, upload the plugin to your WordPress installation. Once activated, this plugin will automatically create a mu-plugin dedicated to define your PHP constants, and your Slack notifications will be automatically configured 🙌

WP Down Slack Alert plugin is available in the official plugins repository on WordPress.org:

Note: this post is a translation of the post I wrote in French on Whodunit WordPress Agency’s blog.

Leave a Reply

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