This website uses cookies to allow us to see how the site is used. If you continue to use this site, we assume that you are okay with this. If you want to use the sites without cookies, please see our privacy policy.

Notify WordPress users of newly published custom post types

So it’s Christmas Eve and this is the final snippet for this year, it’s been fun and even better than last years! I’d like to thank each and everyone of the submitters and the subscribers without which this would really be nothing so, thank you all so much and Merry Christmas now on with the snippets 🙂

Say you have a custom post type and each time you publish a new custom post you’d like to notify all your users. It could be a product, an event, a tutorial or perhaps a lesson etc.

Usually the action hook would be ‘publish_post’ or ‘publish_page’ but those hooks only work for Posts and Pages, these hooks will not work with custom post types! So, what about custom post types?

The following example shows how this can be done to notify users that a Deputy Head has submitted a lesson for review:

function notify_teachers($post_ID)  {
	// a conditional to check that this is a new post
	if( ( $_POST['post_status'] == 'publish' ) && ( $_POST['original_post_status'] != 'publish' ) ) {
		// get all users email addresses
		$wp_user_search = new WP_User_Query( array( 'fields' => array('user_email') ) );
		$usersarray = $wp_user_search->get_results();
		$arrUsers = array();
		// store each email for later use
		for ($arr = $usersarray, $mU = count($arr), $iU = 0; $iU < $mU; $iU++) {
			$arrUsers[] = $arr[$iU]->user_email;
		}
		// create the from details 
		$headers[] = 'From: Deputy Head <dephead@example.net>';
		// lets cc in the head just because we can 
		$headers[] = 'Cc: The Head <thehead@example.net>';
		// separate the users array
		$users = implode(",", $arrUsers);
		// concatenate a message together
		$message = 'A new lesson has been opened for your review on the site ' . get_bloginfo('siteurl') . "\r\n\r\n";
		$message .= 'You can review it now at ' . get_permalink($post_ID) . "\r\n\r\n";
		$message .= 'Feel free to leave a comment and any suggestions.' . "\r\n\r\n";
		$message .= 'Deputy Head.';
		// and finally send the email
		wp_mail($users, "Lesson Notice: "  . $_POST['post_title'] , $message, $headers );
	    return $post_ID;
    }
}
// hook on to the custom post type by using 'publish_lesson'
add_action('publish_lesson', 'notify_teachers');

Out of the box the action hook would usually be ‘publish_post’ but if you have registered a custom post type called ‘lesson’ you can now use ‘publish_lesson’ to hook your function, it really is as simple as that.

About

A geek who likes to code and be nice ^_^

One thought on “Notify WordPress users of newly published custom post types

  1. THANK YOU!
    I expended 2 days trying to making work a similar script to this, I couldn’t find the error!, now all is working, thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.