thingelstad

Jamie Thingelstad's personal website

Adding Email Mention Notification to WordPress P2

I’m using the P2 theme on a couple of websites, one of them is for the team minne✱ to help coordination and collaboration. P2 supports the ability to use a @user notation to mention other users on the site, but it doesn’t do anything other than highlight the user. This snippet of PHP added to the bottom of the P2 functions.php will send email notifications on those mentions. This isn’t heavily tested but it works well for me.

/**
 * Custom code added to P2 to enable email notifications
 * when a user is @mentioned in the site.
 *
 * Original at http://trepmal.com/2011/06/24/using-wordpress-multisite-p2-and-more/
 * and modified from there.
 */
add_action('publish_post','send_email_notification_once',9);
function send_email_notification_once($postID) {
	$post = get_post($postID);
	$author = get_userdata($post->post_author);
	global $p2;
	$mentions = $p2->components['mentions']->find_mentions($post->post_content);
	$permalink = get_permalink($postID);
	$blog_title = get_bloginfo('name');
	foreach ( $mentions as $match ) {
		$email = get_user_by('slug',$match)->user_email;
		$message = "You have been mentioned in this post:\n $permalink \n\n {$post->post_content} ";
		wp_mail($email, "[$blog_title] You've been mentioned by {$author->display_name}", $message);
	}
}
 
add_action('comment_post','send_email_notification_once_comment',9);
function send_email_notification_once_comment($commentID){
	$comment = get_comment($commentID);
	global $p2;
	$mentions = $p2->components['mentions']->find_mentions($comment->comment_content);
	$permalink = get_permalink($comment->comment_post_ID);
	$blog_title = get_bloginfo('name');
	foreach ( $mentions as $match ) {
		$email = get_user_by('slug',$match)->user_email;
		$message = "You have been mentioned by {$comment->comment_author} in this comment:\n $permalink \n\n {$comment->comment_content} ";
		wp_mail($email, "[$blog_title] You've been mentioned in a comment by {$comment->comment_author}", $message);
	}
}

7 Comments

  1. Thanks a tons!

    This is exactly what I’ve been looking 4!

    Actually, I am using your script with my additional “CC:(post’s author)” function to yours so that an author can confirm that his e-mail is sent all right.

    Would you mind me introducing your post in the topics of WP forum below?
    http://wordpress.org/support/topic/p2-mentions-notificationsemails?replies=5

  2. Hi there,

    Thanks for this snippet! Any idea if there’s an easy way to prevent user mention notifications when a post or comment is edited (e.g. adding a tag)?

    Cheers,
    Thomas

    • Not sure. I’m sure there is some way to detect if the post is being edited or created. However, if you simply didn’t send anything on edit it would also not notify you if the @ was added on the edit. Really this would need to store the list of people already notified in a meta field for the post and then it would know to not resend because you already were notified. I’m sure that’s not too hard to do, but not something I know how to do easily.

      • That’s a good point. At least for my purposes, I’d rather under-notify than over. So if someone forgets and goes back to add it, I’d be okay with not sending that notification, as opposed to sending multiple ones. But I know other applications of this would be different.

        Here’s an idea, since we’re discussing it: a plugin that adds a widget you can add to the sidebar. This would only work with P2, but it adds a “notifications” to the sidebar. This could just be some simple text like “Your mentions” and links to your unique /mentions/author/ page. But it would use Ajax or Javascript to use “highlighted” styling until you click it to review your latest mentions. Then after clicking it, it goes to normal link styling. Just a (hopefully simple) way to display notifications in-site, when people visit, instead of (or along with) email notifications.

  3. I’d also like to know the answer to Thomas L’s question. Is there some way to ensure emails go out on 1st posting, but not when updated?

    Also, is anyone using this with the latest 1.5 version of P2?

  4. Hi, can you tell me if there is a way to show live notfications (like on facebook topbar)?
    - sorry for my bad english

Leave a Reply

Your email address will not be published.

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

© 2014 thingelstad

Theme by Anders NorenUp ↑