How to auto send email when publishing a custom post type in WordPress?

Hi, 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 was search and 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!  Can anyone help me? Thanks.
M
Mohi UddinProfessorAsked on October 21, 2016 in Programming.
|

1 Answer

Hi Mohi Uddin,
You can try this code although I haven't test yet but I think this code will works for you.

[code]

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 );
}
[/code]

Thanks for your questions, If this code doesn't works for you then let me know. I shall try to help you as soon as possible.

You can also see my website - http://itinfoworld.com/onlineedu/

I
IT Info WorldSupporterAnswered on October 21, 2016,
|

Your Answer

Your Answer