add_action( 'transition_post_status', 'acf_notify_on_initial_publish', 10, 3 ); function acf_notify_on_initial_publish( $new_status, $old_status, $post ) { // Nur wenn Beitrag veröffentlicht wird (erstmals) if ( $new_status !== 'publish' || $old_status === 'publish' ) { return; } // Nur für Beitragstyp "post" if ( get_post_type( $post ) !== 'post' ) { return; } $post_id = $post->ID; // Bereits versendet? if ( get_post_meta( $post_id, '_notification_sent', true ) ) { return; } // Felder aus ACF laden $recipient_name = get_field( 'review_recipient_name', $post_id ); $recipient_email = get_field( 'review_recipient_email', $post_id ); if ( ! $recipient_email ) { $recipient_email = get_post_meta( $post_id, 'review_recipient_email', true ); } if ( ! $recipient_name ) { $recipient_name = get_post_meta( $post_id, 'review_recipient_name', true ); } if ( ! $recipient_email || ! is_email( $recipient_email ) ) { $log = '❌ Ungültige oder fehlende E-Mail-Adresse am ' . current_time( 'mysql' ); update_post_meta( $post_id, '_notification_log', $log ); error_log( "ACF Notify: Ungültige oder fehlende E-Mail-Adresse für Beitrag #{$post_id}" ); return; } // HTML-Inhalt $title = get_the_title( $post_id ); $permalink = get_permalink( $post_id ); $subject = 'Dein Beitrag wurde veröffentlicht'; $message = "

Hallo {$recipient_name},

der folgende Beitrag wurde soeben auf unserem Blog veröffentlicht:

{$title}

Viele Grüße
Dein Blog-Team

"; $headers = array( 'Content-Type: text/html; charset=UTF-8' ); if ( defined( 'ACF_NOTIFY_BCC_EMAIL' ) && is_email( ACF_NOTIFY_BCC_EMAIL ) ) { $headers[] = 'Bcc: ' . ACF_NOTIFY_BCC_EMAIL; } $mail_sent = wp_mail( $recipient_email, $subject, $message, $headers ); if ( $mail_sent ) { update_post_meta( $post_id, '_notification_sent', true ); $log = '✅ E-Mail erfolgreich an ' . $recipient_email . ' gesendet am ' . current_time( 'mysql' ); } else { $log = '❌ Fehler beim Versand an ' . $recipient_email . ' am ' . current_time( 'mysql' ); error_log( "ACF Notify: Fehler beim E-Mail-Versand an {$recipient_email} für Beitrag #{$post_id}" ); } update_post_meta( $post_id, '_notification_log', $log ); }