Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'd like to have an email automatically sent out to my website's subscribers when I publish a post for a specific custom post type. I've found a few plugins that will do this but only for regular posts (or for any post type that gets published, not allowing you to specify a particular post type). Any suggestions would be greatly appreciated! Thanks.

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

Hook into transition_post_status, fetch the users and send an email to all users.

Sample code, not tested:

add_action( 'transition_post_status', 'send_mails_on_publish', 10, 3 );

function send_mails_on_publish( $new_status, $old_status, $post )
{
    if ( 'publish' !== $new_status or 'publish' === $old_status
        or 'my_custom_type' !== get_post_type( $post ) )
        return;

    $subscribers = get_users( array ( 'role' => 'subscriber' ) );
    $emails      = array ();

    foreach ( $subscribers as $subscriber )
        $emails[] = $subscriber->user_email;

    $body = sprintf( 'Hey there is a new entry!
        See <%s>',
        get_permalink( $post )
    );


    wp_mail( $emails, 'New entry!', $body );
}

You should probably use the Bcc field.

share|improve this answer
    
Thanks very much for the help toscho! I gave your code a try and it appears to be working. Cheers! –  furio Jun 2 '13 at 19:28
    
@toscho or furio, this is working almost perfect for me, thanks, do either of you know how to modify it so each user just sees that it was sent to them, not to user1, user2, user3... –  Chuck Nov 3 '13 at 23:07
1  
@Chuck See my update or wordpress.stackexchange.com/a/45844/73. –  toscho Nov 4 '13 at 1:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.