New Post Notification

| Category: Web Design

Here is a snippet of code to add to your site that will allow anyone who is a member of your site to receive an email notification whenever a new post is added. This is not a plugin; you really don't need a plugin to turn this feature on.

Here is a snippet of code to add to your site that will allow anyone who is a member of your site to receive an email notification whenever a new post is added. This is not a plugin; you really don’t need a plugin to turn this feature on.

I did find some samples of this online, but they were too basic; simply sent an email to the member saying, “a new post has been added! Go here!” and included a generic link to the site. The one I’ve created does much more than that. Here’s a sample of the email that will be spit out once I publish this post:

New Post Notification

Posted in Web Design by Sharon Murphy on Apr 27, 2012 at 2:10pm

Here is a snippet of code to add to your site that will allow anyone who is a member of your site to …read more…

Neat, huh? πŸ˜‰

I’ve included the code needed to create this, and I’ve also included details next to the code to explain what I’ve done to create what you see here.

  1. First thing, you’ll need your own theme. I highly recommend this, because once you make changes, if the original author posts an update and you accidentally commit the update, you’ll lose any changes you made. Go HERE to get a basic child theme if you don’t have one yet.
  2. Next, you need FTP access (or some other access) to allow you to open and edit the functions.php file located inside your theme directory (within wp-content/themes in your WordPress install).
  3. Lastly, copy and paste the following code into your functions.php file (note: there is no closing ?> tag, don’t add one!):
    <?php
    
    /**
    * Theme specific functions and definitions
    * Author: Sharon Murphy
    * URL: http://clarinerd.com/blog/web-design/child-themes/
    * @package WordPress
    * @subpackage Twenty_Eleven
    * @since Twenty Eleven 1.0
    */
    
    function email_members($post_ID) {
      $wp_user_search = new WP_User_Query( array( 'fields' => array('user_email') ) );
      $usersarray = $wp_user_search->get_results();
      $arrUsers = array ();
      for ($arr = $usersarray, $mU = count ($arr), $iU = 0; $iU < $mU; $iU++) {
        $arrUsers[] = $arr[$iU]->user_email;
      }
      $adminEmail = get_option('admin_email'); //email for admin of WordPress install
      $users = implode(",", $arrUsers); //send email to all users
      $siteLink = get_bloginfo('siteurl'); //return URL to site
      $category = get_the_category(); //category array
      $postCategory = $category[0]->cat_name; //first category name
      $catSlug = $category[0]->category_nicename; //first category slug - for permalink build
      $authorFN = get_the_author_meta(first_name, get_post($post_ID)->post_author); //author first name
      $authorLN = get_the_author_meta(last_name, get_post($post_ID)->post_author); //author last name
      $postTitle = get_post($post_ID)->post_title; //text title of post
      $postDate = mysql2date('M j, Y', get_post($post_ID)->post_date); //date posted
      $postTime = mysql2date('g:ia', get_post($post_ID)->post_date); //time posted
      $postLink = get_permalink(get_post($post_ID)); //permalink to post
      $postExcerpt = substr(get_post($post_ID)->post_content,0,144); //144-character limited excerpt of post
      add_filter('wp_mail_content_type',create_function('', 'return "text/html";'));
      wp_mail(
        $adminEmail,
        'New post: ' . $postTitle,
        '<h2>' . $postTitle . '</h2>
        <p>Posted in <a href="'. $siteLink . '/category/' . $catSlug . '">' . $postCategory . '</a> by ' . $authorFN . ' ' . $authorLN . ' on ' . $postDate . ' at ' . $postTime . '</p>
        <p><i>' . $postExcerpt . ' <a href="' . $postLink . '">...read more...</a></i></p>',
        array (
          'BCC:' . $users,
          'From:' . $adminEmail
        )
      );
      return $post_ID;
    }
    add_action('publish_post', 'email_members');

Just as before, if you’d rather not do this yourself, I’ve got the file available for download right here:

[wpfilebase tag=file id=5 /]

As always, please feel free to contact me if you have problems or questions. Thanks! πŸ™‚

Leave a Reply

%d bloggers like this: