WordPress: inviare una notifica quando viene pubblicato un nuovo post

In WordPress possiamo inviare una notifica quando viene pubblicato un nuovo post.

WordPress di Gabriele Romanato

In WordPress possiamo inviare una notifica quando viene pubblicato un nuovo post.

Possiamo usare la action transition_post_status in questo modo:


function my_notify_on_post_published( $new_status, $old_status, $post ) {

    if( 'publish' === $new_status && 'publish' !== $old_status && $post->post_type === 'my_post_type' ) {

      $to = 'user@site.tld';
      $subject = __( 'New post published', 'textdomain' );
      $message = "The post $post->post_title has been published";
      $headers = "From: Site <site@site.tld>\r\n";

      wp_mail( $to, $subject, $message, $headers );

    }
}

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

Commenti