0

I've made a custom post type that works as a ticket service. I need to send notifications to the owner of that post (author) when the post is edited (changes status or has been saved), usually edited by the site admin (admin made some changes). I've take a look to some plugins but i think they are not good for me...

Update I followed the suggestions of Max Yudin and learned from the tutorials in that WP codex page, so the result is in the post below.

    /*Save post metadata when a post is saved.*/

function save_riparazioni_meta( $post_id, $post, $update ) {

    $post_type = get_post_type($post_id);

    // If this isn't a 'book' post, don't update it.
    if ( "riparazioni" != $post_type ) return;

    // - Update the post's metadata.

    if ( isset( $_POST['author'] ) ) {
        update_post_meta( $post_id, 'author', sanitize_text_field( $_POST['author'] ) );
    }


}
add_action( 'save_post', 'save_riparazioni_meta', 10, 3 );

/* Send email when a post is updated */
function riparazioni_updated_send_email( $post_id ) {

    $post_type = get_post_type($post_id);

    // If this is not a custom post type, don't send the email.
    if ( $post_type != 'riparazioni' )
        return;

    $post_title = get_the_title( $post_id );
    $post_url = get_permalink( $post_id );
    $author_name = get_the_author($post_id);
    $author_id = get_post_field ('post_author', $post_id);

    $user_info = get_userdata($author_id);
    $author_email = $user_info->user_email;

    $subject = 'A post has been updated '.$author_id." ".$author_email;
    $message = "Author ".$author_name." Your post has been updated on your website:\n\n";
    $message .= $post_title . ": " . $post_url;

    // Send email to admin.
    wp_mail( $author_email, $subject, $message );
}
add_action( 'save_post', 'riparazioni_updated_send_email' );

I inserted that code in a custom plugin and then activated it, but we can also put it in the function.php file of you child-theme.

  • See save_post action reference example. There is the complete code you need. Research before you ask. – Max Yudin Jan 16 '18 at 17:27
  • thank you Max, your suggest is appreciated. Some times I even don't know if my questions are correct, I think communities are here even for this. Thanks again. – Dario Filidei Jan 17 '18 at 9:31
  • It is desirable to post your developed solution here as the answer for generations to come. – Max Yudin Jan 17 '18 at 9:42
  • yes as I made in the past, I will update the post as soon as possible. – Dario Filidei Jan 17 '18 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.