Skip to content

Instantly share code, notes, and snippets.

@NateJacobs
Created October 23, 2011 00:54
Show Gist options
  • Save NateJacobs/1306710 to your computer and use it in GitHub Desktop.
Save NateJacobs/1306710 to your computer and use it in GitHub Desktop.
WordPress Plugin: Pick the specific users to send an email to notifying them about a new post.
<?php
/*
Plugin Name: Specific User Notification
Plugin URI: https://gist.github.com/1306710
Description: Pick the specific users to send an email to notifying them about a new post.
Version: 0.2
Author: Nate Jacobs
Author URI: http://natejacobs.org
License: GPLv2 or later
*/
class UserPostNotificationByEmail
{
/**
* Construct
*
* Hook into the necessary actions needed to add the meta box and send email.
*
* @author Nate Jacobs
* @since 0.1
*/
public function __construct()
{
add_action( 'add_meta_boxes', array( __CLASS__, 'add_user_list_meta' ) );
add_action( 'publish_post', array( __CLASS__, 'send_email_users' ), 10, 2 );
add_action( 'admin_notices', array( __CLASS__, 'post_admin_notices') );
}
/**
* Add User List Meta Box
*
* Registers the custom post meta box to display on all post edit pages.
*
* @author Nate Jacobs
* @since 0.1
*/
public function add_user_list_meta()
{
add_meta_box( 'user_list', 'Users', array( __CLASS__, 'display_users_meta_box' ), 'post', 'side' );
add_meta_box( 'email_list', 'Enter email addresses to send notice of the post to. Separate addresses with a comma.', array( __CLASS__, 'display_email_meta_box' ), 'post', 'normal' );
}
/**
* Display Users Meta Box
*
* Creates the actual post meta box. It lists all users of the site with a
* a checkbox next to their display name.
*
* @author Nate Jacobs
* @since 0.1
*/
public function display_users_meta_box( $post )
{
$users = get_users();
// loop through each user
foreach ( $users as $user )
{
echo '<input type="checkbox" name="users_email['.$user->ID.']" value='.$user->user_email.'> '.$user->display_name;
echo '<br>';
}
}
/**
* Display Email Meta Box
*
* Creates the text field to enter email addresses manually
*
* @author Nate Jacobs
* @since 0.2
*/
public function display_email_meta_box()
{
echo '<input type="text" name="additional_email" size="120">';
}
/**
* Post Redirect
*
* Adds a query string to the url to indicate one of the email addresses was invalid
*
* @author Nate Jacobs
* @since 1.0
*/
public function post_redirect( $location )
{
remove_filter( 'redirect_post_location', array( __CLASS__, 'post_redirect' ), '99' );
return add_query_arg( 'bad_email', 1, $location );
}
/**
* Post Admin Notices
*
* Adds an error message if one of the email addresses was formatted inforrectly
*
* @author Nate Jacobs
* @since 0.2
*/
public function post_admin_notices()
{
if ( !isset( $_GET['bad_email'] ) )
return;
switch ( absint( $_GET['bad_email'] ) )
{
case 1:
$message = 'One of the email addresses you entered was invalid. Please check them for errors.';
break;
default:
$message = 'Unexpected error';
}
echo '<div id="notice" class="error"><p>' . $message . '</p></div>';
}
/**
* Send Email to Users
*
* If the post is being published for the first time send an email
* to the users specified in the edit post screen.
*
* Email Format
* Hello [display name].
* Just wanted to let you know there is a new post on [blog name].
* Here is the link [permalink]
*
* @author Nate Jacobs
* @since 0.1
* @update 0.2 Added additional wp_mail for manually entered email addresses
*/
public function send_email_users( $post_id )
{
if( $_POST['post_status'] == 'publish' ) //&& ( $_POST['original_post_status'] != 'publish' ) )
{
if ( isset( $_POST['users_email'] ) )
{
foreach( $_POST['users_email'] as $user )
{
$user_value = get_user_by( 'email', $user );
$message = sprintf( __( 'Hello %s.' ), $user_value->display_name ). "\r\n\r\n";
$message .= sprintf( __( 'Just wanted to let you know there is a new post on %s.' ), get_option( 'blogname' ) ). "\r\n\r\n";
$message .= sprintf( __( 'Here is the link %s' ), get_permalink( $post_id ) );
wp_mail( $user, sprintf( __( '[%s] A New Post' ), get_option( 'blogname' ) ), $message );
}
}
if ( isset( $_POST['additional_email'] ) )
{
$email_addy = explode( ',', $_POST['additional_email'] );
foreach( $email_addy as $email )
{
// check if email addresses are valid
if ( !is_email( $email ) )
{
// if not, set a page redirect
add_filter( 'redirect_post_location', array( __CLASS__, 'post_redirect' ), '99' );
}
else
{
$message = sprintf( __( 'Hello %s.' ), $email ). "\r\n\r\n";
$message .= sprintf( __( 'Just wanted to let you know there is a new post on %s.' ), get_option( 'blogname' ) ). "\r\n\r\n";
$message .= sprintf( __( 'Here is the link %s' ), get_permalink( $post_id ) );
wp_mail( $email, sprintf( __( '[%s] A New Post' ), get_option( 'blogname' ) ), $message );
}
}
}
}
}
}
new UserPostNotificationByEmail;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment