functions - Send mail to author when custom post type is saved

admin2025-01-07  5

I have a custom post type called "documents". I can add documents (ACF repeater field) there and the users can see them in a frontend-view. Now i need to send an email-notification to the author of the post when i change the custom-post-type in the backend and save it.

In another project i worked with the following function in the functions.php file, but this code is just working when i create a new post. I don't need to send mails on creating a new post but when i save them again.

/* SEND MAIL WHEN CPT PRODUKTIONSAUFTRAG ERSTELLT */
add_action('future_to_publish', 'send_emails_on_new_event');
add_action('new_to_publish', 'send_emails_on_new_event');
add_action('draft_to_publish' ,'send_emails_on_new_event');
add_action('auto-draft_to_publish' ,'send_emails_on_new_event');

function send_emails_on_new_event($post) {
    $emails = "[email protected]";
    $headers = 'From: Name <[email protected]>';
    $title = wp_strip_all_tags(get_the_title($post->ID));
    $message = "New post created";
    if(get_post_type($post->ID) === 'documents') {
        wp_mail($emails, "New Post", $message, $headers);
    } 
}

I have a custom post type called "documents". I can add documents (ACF repeater field) there and the users can see them in a frontend-view. Now i need to send an email-notification to the author of the post when i change the custom-post-type in the backend and save it.

In another project i worked with the following function in the functions.php file, but this code is just working when i create a new post. I don't need to send mails on creating a new post but when i save them again.

/* SEND MAIL WHEN CPT PRODUKTIONSAUFTRAG ERSTELLT */
add_action('future_to_publish', 'send_emails_on_new_event');
add_action('new_to_publish', 'send_emails_on_new_event');
add_action('draft_to_publish' ,'send_emails_on_new_event');
add_action('auto-draft_to_publish' ,'send_emails_on_new_event');

function send_emails_on_new_event($post) {
    $emails = "[email protected]";
    $headers = 'From: Name <[email protected]>';
    $title = wp_strip_all_tags(get_the_title($post->ID));
    $message = "New post created";
    if(get_post_type($post->ID) === 'documents') {
        wp_mail($emails, "New Post", $message, $headers);
    } 
}
Share Improve this question asked Sep 21, 2018 at 8:13 public9nfpublic9nf 3961 gold badge9 silver badges20 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Try this way:

function send_emails_on_new_event( $post ) {

    $emails  = '[email protected]';
    $headers = 'From: Name <[email protected]>';
    $title   = wp_strip_all_tags( get_the_title( $post->ID ) );
    $message = 'New post created';

    if ( get_post_type( $post->ID ) === 'documents' ) {
        wp_mail( $emails, 'New Post', $message, $headers );
    }
}
add_action( 'pre_post_update', 'send_emails_on_new_event' );

From the Code Reference:

pre_post_update: Fires immediately before an existing post is updated in the database.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736252111a19.html

最新回复(0)