How To Automatically Notify Members When New Posts Are Published

Snippets by IsItWP

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.


All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:

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

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!