Welcome to Dagon Design. In addition to free scripts, WordPress plugins, and articles, we offer a variety of services including custom theme design, plugin creation, and PHP scripting. Contact me for more information.

Version 1.22   Updated Wednesday, July 1st, 2009 at 8:20pm

Draft Notification Plugin for WordPress

This WordPress plugin automatically emails the admin when a new draft is saved. The email contains the post’s title, the author, and a link. There is no options page currently, because there is really nothing to configure. The plugin now supports the new ‘pending’ status.

Download

  If you have found this page useful, please consider donating. Thanks!

Instructions

  • Download the above file, rename it from .txt to .php, and upload it to your plugins directory.
  • Enable the plugin.

Change Log

  • 07-01-09 Version 1.22 – You can now choose to send the emails to all users on or above a certain level (eg: editors).
  • 04-27-08 Version 1.21 – Updated to work with WordPress 2.5 and the ‘pending’ status.
  • 05-16-07 Version 1.2 – Updated to work with WordPress 2.2.
  • 10-20-06 Version 1.1 – The emails now contain the author’s name, instead of ID.
  • 04-22-06 Version 1.0 – First release.

Notes

Because of the way this plugin works, it also sends out an email when an existing draft has been saved again – This includes auto-saves. A solution to this is being researched.

By default, the author’s display_name is shown in the email. If you would prefer to see the login_name, or nice_name field, just look for the line in the script that says “Choose one of the following options to show the author’s name” and uncomment the option you would like to use.

Changing the user level for notifications

In the script, look for this line:

$email_user_level = 7;

This tells the script to send notification emails to anyone on, or above, that user level. Just change this for your particular needs.

  1. Hey, great plugins. I modded this one to force it to go only to admin. I changed line 45 from this:
    $editors = $wpdb->get_results("SELECT user_id FROM {$tp}usermeta WHERE {$tp}usermeta.meta_value >= " . $email_user_level);
    to this:
    $editors = $wpdb->get_results("SELECT user_id FROM {$tp}usermeta WHERE {$tp}usermeta.meta_value = 10");

  2. 54
    Serge

    HI,

    I have to admit that I don’t know anything about scripts, In our blog, there is five administrators (same level). How can ths script be modified to make sure the alert is sent to only one recipient (myself) ?
    Thanks in advance for your reply.

    P.-S. Sorry for the bad English.

  3. Tested but the email is not sent out. no problem with new user registration email.

  4. Jerod: This plugin does not currently have a settings page.

    I just tested again on one of my WP 2.8 installs and it worked fine for me. Me sure you have your admin email address set properly in your WP user profile. Also, you might make sure it is not getting blocked by a spam filter, etc..

  5. I have uploaded the plugin and really want to use it. I am hoping that it will integrate nicely with P2 and alert me when a new post is written. However, after activating the plugin and I cannot find any place in the dashboard where I can change the settings. And I have not gotten it to work yet, as no emails have come my way. Using WordPress 2.8. Any helpful hints on what my problem could be?

  6. ron: That is just where the variable is defined. In the actual SQL statement, the proper code is used.

  7. Hey Admin, I didn’t check your actual code but in your posting, I believe you need “$email_user_level = 7;” to be “>= 7″ to include all level 7 users and ABOVE. Currently it would only email level 7 users.

  8. Version 1.22 has been released

    You can now choose to send the email notifications by user level. See above for the option. This release has been tested, and works, in the latest release of WordPress – 2.8

    ron G: Thanks!

  9. Sorry I forgot a small but major piece of code. Where it says:

    
    foreach ($editors as $editor) {			
       $user_info = get_userdata($editor);
       $recipient .= $user_info->user_email . ',';
    }
    

    it should be:

    
    foreach ($editors as $editor) {			
       $user_info = get_userdata($editor->user_id);
       $recipient .= $user_info->user_email . ',';
    }
    

  10. I added some additional code to email all editors (in my case all users with a user-level 7 and above) when there is a post pending. Hope this helps.

    
    
    <?php
    /*
    Plugin Name: Draft Notification
    Plugin URI: http://www.dagondesign.com/articles/draft-notification-plugin-for-wordpress/
    Description: Sends an email to the site admin when a draft is saved.
    Author: Dagon Design
    Version: 1.21
    Author URI: http://www.dagondesign.com
    */
    
    
    function dddn_process($id) {
    
    	global $wpdb;
    	
    	$tp = $wpdb->prefix;
    
    	$result = $wpdb->get_row("
    		SELECT post_status, post_title, user_login, user_nicename, display_name 
    		FROM {$tp}posts, {$tp}users 
    		WHERE {$tp}posts.post_author = {$tp}users.ID 
    		AND {$tp}posts.ID = '$id'
    	");
    
    	
    
    	if ($result->post_status == "pending") {
    
    		$message = "";
    		$message .= "A draft was updated on '" . get_bloginfo('name') . "'\n\n";
    		$message .= "Title: " . $result->post_title . "\n\n";
    
    			// *** Choose one of the following options to show the author's name
    	
    		$message .= "Author: " . $result->display_name . "\n\n";
    		// $message .= "Author: " . $result->user_nicename . "\n\n";
    		// $message .= "Author: " . $result->user_login . "\n\n";
    
    		$message .= "Link: " . get_permalink($id);
    
    		$subject = "Draft Updated on '" . get_bloginfo('name') . "'";
    
    		$editors = $wpdb->get_results("SELECT user_id FROM {$tp}usermeta WHERE {$tp}usermeta.meta_value >= 7");
    		
    		$recipient = "";
    	
    			foreach ($editors as $editor) {			
    
    				$user_info = get_userdata($editor);
    				$recipient .= $user_info->user_email . ',';
    			}
    
    		mail($recipient, $subject, $message);
    
    	}
    
    }
    
    
    add_action('save_post', 'dddn_process');
    
    ?>
    
    

  11. If anyone is having problems with not receiving emails from Wordpress, there is a solution here: http://www.roblayton.net/archive/wordpress-not-sending-emails-anymore-solved/

    Good luck!

  12. This plugin has been tested, and works in the latest WordPress release (2.7.1).

    The only current issue is that an email is sent every time the draft is auto-saved. While the auto-save feature of WordPress can be turned off, I plan to come up with a solution to this.

  13. 43
    Brian

    Will you be updating this plugin for use with Wordpress 2.7?

  14. There is currently an issue with this plugin when the auto-save feature of new versions of WP is enabled. Everytime WP automatically safes the drafts, an email is sent. It will be looked into soon.

  15. 41
    Simon

    The method for sending to authors didn’t work for me, so I changed the code to the following using get_userdata, which worked:

    
    
    function dddn_process($id) {
    
    	global $wpdb;
    	
    	$tp = $wpdb->prefix;
    
    	$result = $wpdb->get_row("
    		SELECT post_status, post_author, post_title, user_login, user_nicename, display_name 
    		FROM {$tp}posts, {$tp}users 
    		WHERE {$tp}posts.post_author = {$tp}users.ID 
    		AND {$tp}posts.ID = '$id'
    	");
    
    
    	if ($result->post_status == "pending") {
    
    		$message = "";
    		$message .= "Thanks for your post. \n\n";
    		$message .= "Title: " . $result->post_title . "\n\n";
    
    		$message .= "Message body .";
    
    		$message .= "Many thanks";
    
    		$subject = "Subject";
    
                    $user_info = get_userdata($result->post_author);
    
    		$recipient = $user_info->user_email;
    
    		mail($recipient, $subject, $message);
    
    	}
    
    }
    
    
    add_action('save_post', 'dddn_process');
    
    ?>
    
    

  16. thank! it works now. i tried to inform the author /and/ the admin this wat:

    $admin = get_bloginfo(‘admin_email’);
    $poster = get_the_author_email();

    mail($admin, $subject, $message);
    mail($poster, $subject, $message);

    but i end up with both emails going to the admin – do you see the problem?

    PAT

  17. Pat: I am still planning to check all the plugins for compatibility :) I know most of them work with no problems (Sitemap generator, Contact form), but there are a couple others I am going to fix when I get a chance.

  18. hi, could you make it compatible with vesion wordpress 2.5? it seems the db structure canged and the query is not working anymore..

  19. Hello,

    I want use your plugin for my blog but I don’t get the recipient bis the function

    $recipient = get_the_author_email();

    I checked it to input the recipient into the message, but this part is empty. I don’t get the author email.

    I hope, you can help me.

    Thanks a lot
    Uwe from germany

  20. 36
    josh

    Great plugin!! Thanks for making it available.

    In case anyone else wants to be notified upon “Pending Review” instead of Draft (with the introduction of Pending Review in 2.3), it’s easy to do. Just change the line that says:

    if ($result->post_status == “draft”)

    to

    if ($result->post_status == “pending”)

    and it seems to work great.

    -Josh

  21. 35
    mifouane

    Works now. The USERS table prefix was different in my DB.

  22. 34
    mifouane

    Oops! I just saw the change log that says this now works with Wordpress 2.2. However, has anyone tried this with Wordpress MU?

  23. 33
    mifouane

    hi,
    Thanks, this is exactly what I’m looking for. Unfortunately, I haven’t been able to make it work so far. I am using Wordpress MU 1.0 (which uses the core Wordpress version 2.2). Any idea if this plugin should work with Wordpress MU or at least with Wordpress 2.2?

  24. 32
    Rumman

    hi,
    I’ve slightly modified your handy plugin. I’ve add an administration menu, where admins can choose, which users will get notifications. Here’s the code:

    
    
    <?php
    
    function dddn_menu () {
      add_options_page('Notifikation', 'Notifikation', 8, basename(__FILE__), 'dddn_options_page');
    }
    function dddn_options_page() {
      global $wpdb;
    
      // get all users
      $tp = $wpdb->prefix;
    	$users = $wpdb->get_results("
    		SELECT ID, user_login
        FROM {$tp}users    
    	");
      if ($_POST['hidden_field']==='Y') { // form processing
        $notify= $_POST['notify'];  
        for ($j=0; $j < count($users); $j++) {
          if ($notify[$j] != get_usermeta($users[$j]->ID,'notify') ) {
            update_usermeta($users[$j]->ID,'notify',$notify[$j]);
            $output.=$users[$j]->user_login."<br />\n";
          }
        }
        if ($output) {
          ?>
          <div class="updated"><h3>Updated users:</h3><p><strong><?php echo $output; ?></strong></p></div>
          <?php
        }
      }
      // form	
      ?>  
      <div class="wrap">
        <h2>Notifikation</h2>
        <p>Sends an email to selected users when a draft is saved.
        </p>
        <form name="form1" method="post" action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>">
          <input type="hidden" name="hidden_field" value="Y">
          <p><?php 
            $i=0;
            foreach ($users as $user) {  
              if (get_usermeta($user->ID,"notify")) $checked.=" checked=\"checked\" ";
              echo "<input type=\"checkbox\" name=\"notify[$i]\" value=\"1\" id=\"notify[$i]\" $checked>&nbsp;";
              echo "<label for=\"notify[$i]\" >{$user->user_login}</label><br />\n";
              unset($checked);
              $i++;
            }    
          
            ?> 
          </p><hr />
          <p class="submit">
            <input type="submit" name="Submit" value="<?php _e('Update Options') ?>" />
          </p>
        </form>
      </div>
      <?php
    } // dddn_options_page
    
    function dddn_process($id) {
    
    	global $wpdb;
    	
    	$tp = $wpdb->prefix;
    
    	$result = $wpdb->get_row("
    		SELECT post_status, post_title, user_login, user_nicename, display_name 
    		FROM {$tp}posts, {$tp}users 
    		WHERE {$tp}posts.post_author = {$tp}users.ID 
    		AND {$tp}posts.ID = '$id'
    	");
    
    	if ($result->post_status == "draft") {
    
    		$message = "";
    		$message .= "Draft updated on '" . get_bloginfo('name') . "'\n\n";
    		$message .= "Title: " . $result->post_title . "\n\n";
    
    	
    		// *** Choose one of the following options to show the author's name
    	
    		$message .= "Author: " . $result->display_name . "\n\n";
    		// $message .= "Author: " . $result->user_nicename . "\n\n";
    		// $message .= "Author: " . $result->user_login . "\n\n";
    
    
    
    		$message .= "Link: " . get_permalink($id);
    
    		$subject = "Draft updated on '" . get_bloginfo('name') . "'";
    
    		// get recipients from db
        $recipients = $wpdb->get_results("
    		  SELECT {$tp}users.user_email 
          FROM {$tp}users, {$tp}usermeta
          WHERE {$tp}users.ID={$tp}usermeta.user_id
          AND {$tp}usermeta.meta_key= 'notify' AND {$tp}usermeta.meta_value= '1' 
    	  ");
    	  // send mails
    	  foreach ($recipients as $recipient) {
    		  mail($recipient, $subject, $message);
        }
    	}
    
    }
    
    if (function_exists('add_action')) {
      add_action('save_post', 'dddn_process');
      add_action('admin_menu', 'dddn_menu');
    }
    ?>
    
    
    

  25. Graham: In the WordPress admin panel, go to ‘Manage -> Plugins’

  26. I have installed the above plug in; how do I enable it?
    Thansk

  27. tyson: That should work fine, except you do not want that ‘echo’ there.

  28. 28
    tyson

    How could the $recipient line be changed so that one (or more ) static e-mail addresses could be entered? Something like this?

    $recipient = echo "jon@mail.com, mary@mail.com";

  29. Manne: I just added some alternate code you can try.

  30. 26
    Manne

    Unfortunately the author mod didn’t work for me…

  31. 25
    Manne

    Many thanks, I will try it out right away

  32. Manne: I just added instructions in the ‘Other Modifications’ section above. Let me know if it works for you.

  33. 23
    Manne

    Hello, and many thanks for a great plugin.

    I’m trying to figure out how to change it to email the author instead of the admin, any ideas?

  34. Josh: Very nice method :)

  35. I have added just a little bit of code to this to make it just a little better. With this code you can have the draft set to a time instead of every save. For example it will email you when the first draft is saved then the next time that the draft is saved it will only email you it is past your time set, I have at 10 minutes.

    Here is the code:

    
    //Original code
    $recipient = get_bloginfo('admin_email');
    
    // New code
    if ( isset($_COOKIE['draft_page_id_'.$id])  )
    {
    	//there is a cookie for this draft under the set time 
    	$minDelay = 10;// this is how many minutes to delay before a resend
    	if( ($_COOKIE['draft_page_id_'.$id]+(60*$minDelay) ) < time() )
    	{
    		//set the cookie for the current Draft
    		setcookie('draft_page_id_'.$id, time(), time() + 30000000, COOKIEPATH, COOKIE_DOMAIN);
    		mail($recipient, "Saved Draft".$subject, $message);
    	}
    }
    else
    {
    	//set the cookie for the first Draft
    	setcookie('draft_page_id_'.$id, time(), time() + 30000000, COOKIEPATH, COOKIE_DOMAIN);
    	//now send email
    	mail($recipient, "Starting Draft".$subject, $message);
    }
    
    

  36. Next time I have a chance, I will see if there is anyway to do it :)

  37. I agree with Scot’s comment (#19), especially with the auto-save feature in WordPress 2.1. If the contributor were able to select to send a draft notification, that would be great (plus, I can be sure that I’m reviewing their final best efforts!). Otherwise, I love the plugin!

  38. Rather than sending automatically upon save (or auto-save), would love to see a checkbox option in the editor to send notification. Also, I agree that it should be possible to customize the destination email (in my case I’d want it to be sent to a mailing list address for editors). And I agree that the link in the email should go to the back-end, not to a public page.

  39. Teeth: I know that WordPress 2.1 has implemented several new hooks. I will have to look through them to see if there is a way to detect the difference.

  40. Great plugin, but with the Wordpress 2.1 it gets a little annoying as a message is sent everytime the system does an autosave. I hope there is a way around to having the message delivered ONLY when the save draft is pressed, I wonder how but just an idea

  41. Thank you for this great plug-in! This saves a lot of time when you have multiple Blogs you administer.

  42. Thank you very much for the plugin.
    It is exactly what I needed at http://www.newrss.net

    Please feel free to stop by and make a post about it. I’m sure there are others who could benefit from this plugin.

    Again, thanks! This is a great plugin.

  43. 13
    Cisco

    This is EXACTLY the plugin that I’ve been looking for. Thank you so much!

  44. Shane: I just uploaded a new version – 1.1

    It shows the display_name for the author now, and includes an option in the code to use login_name or nice_name if you prefer :)

    I had actually intended it to show the name, not ID, so thanks for pointing that out :)

  45. This is great… Just what I needed… Though is there a way to have it show the author’s name in the post rather than their number?

  46. What a cool idea! This was exactly what I was looking for. :)

  47. Opps, I forgot to mention that I have “just” copy&paste the From:-line from Scott Merrill’s (Skippy) commentauth-plugin. So it was not fully my work. ;)

  48. Quix0r: Nice work. One reason I skipped extra headers in this script was because so many people have problems with extra headers in my mail scripts – since a lot of servers disable the use of the extra parameters when PHP is in safe mode. Eventually I will add checks in to determine whether or not the extra info can be used. I like your code though :)

  49. Hello,

    I have patched your plugin slightly. I have added a nicer “From:”-line than the ugly “www-data” one or what ever your webserver is mailing with.

    http://blog.mxchange.org/dd-draft-notification.phps

    It not (yet) tested but it may work. So please feel free to grab and test it.

    Quix0r

  50. Brian: Good to hear :)

  51. 5
    Brian Barnes

    Nevermind, I got it figured out! I edited the recipient list! Thanks for a great plugin!

  52. 4
    Brian Barnes

    Is there a way to edit the list of recipients? I don’t want the admins to get the emails, but rather a list of editors who will proof and publish the post.

  53. Glad it is working for you :)

    As far as the link, it does appear to work on my site, as long as I am logged in as the admin. I will test it out some more though. Either way, pointing it straight to the admin panel would be a good idea :)

  54. Ok. Forget about this. There was a problem on my server side.
    It’s all fixed and it works great now!
    Just one little remark: the link we receive in the email can not be valid until the draft hasn’t been published. The page doesn’t exist.
    It would make more sense to link to the admin. Something like: /wp-admin/post.php?action=edit&post=$post_id.

  55. Hello,
    Thank you for this plugin. It’s a very good idea. I was just looking for something like that.
    Unfortunately I just tried to install it and it’s not working. I activated it but nothing happens!
    I just came across this : http://wordpress.org/support/topic/51296?replies=4
    Nothing to do with it?

Leave a Comment

Before you comment: If you are having an issue with a script, please make sure you have read the entire article. Also, please read through the comments because most common issues have already been discussed many times. Thanks.


Be sure to wrap all code in <code></code> tags.