Are you looking for a way to automatically notify members when new posts are published? This snippet sends an email to all registered users when a post is published.
Instructions:
All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:
<?php 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; } // for $users = implode(",", $arrUsers); mail($users, "New post notification : " . get_bloginfo('name') , "A new post has been published on " . get_bloginfo('siteurl') ); return $post_ID; } add_action('publish_post', 'email_members'); ?>
Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.
If you liked this code snippet, please consider checking out our other articles on the site like: WP Mail SMTP Review: #1 SMTP Plugin for WordPress.
Comments Leave a Reply