Posted By

eniris on 11/16/11


Tagged

email wordpress notification


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Kurt


Automatically Notify Your Members on New Posts on WordPress 3.1+


 / Published in: PHP
 

Send an email to all registered users when a post is published, for wordpress 3.1+

  1. /**
  2.  * Automatically Notify Your Members on New Posts
  3.  * Send an email to all registered users when a post is published, for wordpress 3.1+
  4.  * Simply place this code into your functions.php file.
  5.  * source : http://wp-snippets.com/575/automatically-notify-your-members-on-new-posts/
  6.  */
  7. function email_members($post_ID) {
  8. //global $wpdb;
  9. //$usersarray = $wpdb->get_results("SELECT user_email FROM $wpdb->users;");
  10. $wp_user_search = new WP_User_Query( array( 'fields' => array('user_email') ) );
  11. $usersarray = $wp_user_search->get_results();
  12. $arrUsers = array ();
  13. for ($arr = $usersarray, $mU = count ($arr), $iU = 0; $iU < $mU; $iU++) {
  14. $arrUsers[] = $arr[$iU]->user_email;
  15. } // for
  16. $users = implode(",", $arrUsers);
  17.  
  18. mail($users, "New post notification : " . get_bloginfo('name') , "A new post has been published on " . get_bloginfo('siteurl') );
  19. return $post_ID;
  20. }
  21. add_action('publish_post', 'email_members');

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: leond on December 22, 2012

Use wp_mail instead op mail.

Posted By: franken on February 5, 2013

Thanks! How do you modify that to add a FROM email address? becuase right now, I get a default email from my hosting provider that doesn't work. The from is NOT from the admin or any other registered user. In an older version of the code (that now creates error with WP 3.0+ there was this line: $usersarray = $wpdb->getresults("SELECT useremail FROM $wpdb->users;");> Can it be adapted for this snippet? Thanks again. Franck.

Posted By: franken on February 5, 2013

Using wp_mail instead of mail makes the FROM to be: [email protected]

You need to login to post a comment.