Skip to content

Instantly share code, notes, and snippets.

@nfsarmento
Forked from dibakarjana/functions.php
Last active March 13, 2023 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nfsarmento/c629d3aea50adc028b2530b49b778288 to your computer and use it in GitHub Desktop.
Save nfsarmento/c629d3aea50adc028b2530b49b778288 to your computer and use it in GitHub Desktop.
Wordpress: Send Email after any Post type status change
/**
*
* https://codex.wordpress.org/Post_Status_Transitions
*
* Send email to admin after user create new product from frontend form.
* Send emails on products publication
* @param WP_Post $post
*
*
*/
function ns_send_emails_on_new_event($post){
$emails = "test@test.com";
$title = wp_strip_all_tags(get_the_title($post->ID));
$url = get_edit_post_link( $post->ID, $context );
$message = "A new product has been created on in your site.\nProduct Title: \n{$title} \nProduct URL: \n{$url}";
if(get_post_type($post->ID) === 'product') {
wp_mail($emails, "New product has been created", $message);
}
}
add_action('future_to_pending', 'ns_send_emails_on_new_event');
add_action('new_to_pending', 'ns_send_emails_on_new_event');
add_action('draft_to_pending', 'ns_send_emails_on_new_event');
add_action('auto-draft_to_pending', 'ns_send_emails_on_new_event');
add_action('pending_to_publish', 'ns_send_emails_on_deal_publish');
function ns_send_emails_on_deal_publish($post){
$author_id=$post->post_author;
$emails = get_the_author_meta( 'user_email' , $author_id ); ; //If you want to send to site administrator, use $emails = get_option('admin_email');
$title = wp_strip_all_tags(get_the_title($post->ID));
$url = get_permalink( $post->ID );
$message = "Your Deal is Live.\n Check Deal: \n{$url}";
if(get_post_type($post->ID) === 'product')
{
wp_mail($emails, "Your Deal on Site Name is Live: {$title}", $message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment