web-dev-qa-db-fra.com

Courriel d'alerte quand une publication ou une page est modifiée

Existe-t-il un moyen pour que Wordpress m'envoie un courrier électronique chaque fois qu'une page ou une publication est publiée?

10
GavinR

Il existe quelques quelques plugins qui gèrent les notifications par courrier électronique , mais ils semblent tous se comporter comme un service d'abonnement pour (tous) les utilisateurs de WordPress.

Pour notifier simplement you quand un article ou une page est publié:

/**
 * Send an email notification to the administrator when a post is published.
 * 
 * @param   string  $new_status
 * @param   string  $old_status
 * @param   object  $post
 */
function wpse_19040_notify_admin_on_publish( $new_status, $old_status, $post ) {
    if ( $new_status !== 'publish' || $old_status === 'publish' )
        return;
    if ( ! $post_type = get_post_type_object( $post->post_type ) )
        return;

    // Recipient, in this case the administrator email
    $emailto = get_option( 'admin_email' );

    // Email subject, "New {post_type_label}"
    $subject = 'New ' . $post_type->labels->singular_name;

    // Email body
    $message = 'View it: ' . get_permalink( $post->ID ) . "\nEdit it: " . get_edit_post_link( $post->ID );

    wp_mail( $emailto, $subject, $message );
}

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

Vous pouvez soit déposer ceci dans le functions.php de votre thème, soit l'enregistrer en tant que plugin (ce qui pourrait être plus approprié, car ce n'est pas exactement lié au thème).

18
TheDeadMedic

sha - il répond à la question en indiquant que la solution publiée ne fonctionne pas dans tous les cas.

Après 24 heures, je peux mettre à jour les connaissances que j'ai apportées. La solution à cet emplacement ( Notifier l'administrateur lorsque la page est modifiée? ) fonctionne sur le serveur, contrairement à la solution publiée ci-dessus. Pour citer le fil avec la solution qui fonctionne mieux dans les deux contextes que j'ai essayés:

Le script original du wpcodex fonctionne bien:

 add_action( 'save_post', 'my_project_updated_send_email' ); 
 function my_project_updated_send_email( $post_id ) { 
    //verify post is not a revision 
    if ( !wp_is_post_revision( $post_id ) ) { 
         $post_title = get_the_title( $post_id ); 
         $post_url = get_permalink( $post_id ); 
         $subject = 'A post has been updated'; 
         $message = "A post has been updated on your website:\n\n";
         $message .= "<a href='". $post_url. "'>" .$post_title. "</a>\n\n"; 
         //send email to admin 
         wp_mail( get_option( 'admin_email' ), $subject, $message ); 
   } 
} 
3
Doorwhey

Bien sûr, vous devrez utiliser/ Transition post-statut appropriée crochet ou crochets et wp_mail() .

1
Rarst