With so many different demands vying for your attention in modern life, it can be easy to lose track of the many important tasks you are supposed to carry out and hopefully be reminded of. After all, there is only so much a desk full of scribbled post-it notes can do to ensure you are reminded about something important, so there is a good chance you have forgotten about something recently. However, there is a way to get Slack to remind you of all these tasks as and when you require, and the good news is we are going to show you how to set it up!   

There can be a large number of things you may need Slack to remind you of. The who, what, when and why of a reminder is hugely important and can cover anything from setting a reminder of a meeting with your bank manager, submitting your travelling expenses on time or a reminder for a deadline of an important project coming up. The reminder example we will be using in this blog is setting up a “blog post Slack reminder” message. Though feel free to substitute this for any kind of reminder that is more appropriate to you and your situation.     

Given the importance of SEO, most people who run websites are aware of vital regularly updating and posting new blogs is. While the tasks needed to succeed at SEO are a closely guarded secret, we do know that regularly posting a new blog is a great way to move up the Google SEO ranking. As such, website owners may be interested in an app for Slack that will send a notification to your Slack channel if you have not posted a new article on your blog within the last 7 days. These reminders will hopefully help you manage your articles and blogs better and also help to remind you to publish them on time.

In this article, we show you how you can integrate Slack notification with your WordPress blog using an app you can create yourself.

Getting started – Create a Slack application

To get started, you first need to create an application within your Slack account. To do this, go to the Slack API page and hit the ‘Create New App’ button.

This will create a popup window. Complete your details requested and you will be redirected to a ”settings page”. In this settings page, click on the ‘Incoming Webhooks’ from the left section. Toggle this option to “On”.

Scroll down to the bottom and click on the ‘Request to Add New Webhook’ button. If you are not an Admin then you need to get admin approval to access this option.

Once the app is approved, click on the ‘Install APP’ from the left side menu. And then press the ‘Install App to Workspace’ button.

Choose the desired channel on the next page and click on the ‘Allow’ button. You will then be redirected to a page where you will get your Webhook URL. Copy this URL as you will require it shortly.

Send notification on Slack channel from WordPress

You are now ready to proceed as you have the Webhook URL and have also configured the Slack channel, where the notification will be sent. Next, you need to write a code that sends a POST request along with a message to the webhook URL. This webhook URL will automatically post the received message to the chosen Slack channel. 

So, let’s do some coding.

Calculate days when last article is posted

When sending a notification to the Slack channel, you first need to calculate days from when the last article was posted. The below code will go inside your active themes:functions.php file.

<?php

function get_daycount_of_last_blog_posted(){
   $args=array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => 1,       
    );  

    $datediff = 0;
    $blog_posts = new WP_Query( $args );
    if ( $blog_posts->have_posts() ) :
 
 while ( $blog_posts->have_posts() ) : $blog_posts->the_post();

   $datediff = time() - strtotime(get_the_date());
      endwhile;
        wp_reset_postdata();
    endif;

    if ($datediff) return round($datediff / (60 * 60 * 24));

   return $datediff;
}

This code and method will get the last added post from the WordPress database and calculate the days between when you requested it and when the last article was posted.

Send POST request to Webhook

WordPress provides a function wp_remote_post() that performs an HTTP request and receives the response. Using this method we will send our custom message to the webhook.

Create a file notification.php in the root directory of your WordPress project and add the below code to it.

<?php 
require_once('wp-load.php');
$slack_webhook_url = 'YOUR_WEBHOOK_URL';
$last_blog_day_count = get_daycount_of_last_blog_posted();
if($last_blog_day_count > 5) {

 $msg = 'Hey team, our app found that we have not added new article on UpdraftPlus
 for more than 5 days. Please look into it.';

    // Prepare the data / payload to be posted to Slack
  $data = array(
        'payload'   => json_encode( array(
                "text" =>  $msg,
            )
        )
    );

    // Post our data via the slack webhook endpoint using wp_remote_post
  wp_remote_post( $slack_webhook_url, array(
            'method' => 'POST',
            'body' => $data,
        )
    );
}

This code will post a message to Webhook URL if a new article is not added for more than 5 days on your blog. As a result, you get a notification on your selected Slack channel.

Set cron on your server

Our goal is to create an automated system which will notify the team about this task on a specific Slack channel. Because of this, we created a PHP file in the root folder of the WordPress project. By setting cron with this file on your server, you can set up your Slack app to run automatically.

Setting cron on the server may differ on the basis of your hosting provider. For the sake of this tutorial, we will give an example on how to set cron on Media Temple web hosting’s CPanel.

Login to your CPanel. Select ‘Cron Jobs’ under the “Advanced” section.

On the next page, choose the ‘Once Per Day(0 0 * * *)’ option from the ‘Common Settings’ dropdown. This means the cron will execute the script once a day.

Finally, in the Command field add the below statement by adjusting your notification.php file path.

<pre>/usr/local/bin/php /home/xyz/public_html/notification.php</pre>

That’s it! 

By following this tutorial, you are now able to create your own Slack app for you and your team. This will certainly help everyone keep up to date with scheduled blogs and ensure you do not miss out on any important dates. 

The post How to integrate Slack reminder notification with WordPress appeared first on UpdraftPlus. UpdraftPlus – Backup, restore and migration plugin for WordPress.