问题
The code below sends out an email every time I (change the STATUS field and then) safe/update the post. But it's not working the way I want and I know whats's wrong:
The field updates are always saved AFTER the post is saved so it always sends out the PREVIOUS field values (its like I'm always 1 step behind).
How can I let the POST UPDATE come LAST so that the then triggered action (send email) will have the latest field values included?
function yl_send_booking_email_after_post_update( $new_status, $old_status, $post ) {
if ( $new_status == $old_status ) {
if ( 'booking' === get_post_type() ) { // check the custom post type
$send_email = get_post_meta( $post->ID, 'bookings_field_send_email', true );
if ( $send_email === 'yes' ) {
$status = get_post_meta( $post->ID, 'bookings_field_status', true );
if ( $status === 'confirmed' ) {
$subject = get_post_meta( $post->ID, 'bookings_field_email_title_confirmed', true );
} else if ( $status === 'changed' ) {
$subject = get_post_meta( $post->ID, 'bookings_field_email_title_changed', true );
} else if ( $status === 'canceled by guest' ) {
$subject = get_post_meta( $post->ID, 'bookings_field_email_title_canceled_by_guest', true );
} else {
$subject = get_post_meta( $post->ID, 'bookings_field_email_title_canceled_by_us', true );
}
if ( $status === 'confirmed' ) {
$body = get_post_meta( $post->ID, 'bookings_field_email_content_confirmed', true );
} else if ( $status === 'changed' ) {
$body = get_post_meta( $post->ID, 'bookings_field_email_content_changed', true );
} else if ( $status === 'canceled by guest' ) {
$body = get_post_meta( $post->ID, 'bookings_field_email_content_canceled_by_guest', true );
} else {
$body = get_post_meta( $post->ID, 'bookings_field_email_content_canceled_by_us', true );
}
$to = get_post_meta( $post->ID, 'bookings_field_email', true );
$headers = array
(
'From: ' . get_bloginfo('name') . ' <' . get_bloginfo('admin_email') . '>',
'Bcc: ' . get_bloginfo('admin_email'),
'X-Mailer: PHP/' . phpversion(),
'MIME-Version: 1.0',
'Content-type: text/html; charset=iso-8859-1'
);
$headers = implode( "\r\n" , $headers );
wp_mail( $to, $subject, $body, $headers );
}
}
}
}
add_action( 'transition_post_status', 'yl_send_booking_email_after_post_update', 15, 3 );
UPDATE: After getting some info from the ACF team I had to change my code. See below. I get NO errors but there's NO email being send... Any ideas?
add_action( 'acf/save_post', 'yl_send_booking_email_after_post_update', 5 );
function yl_send_booking_email_after_post_update( $post_id ) {
if ( 'booking' === get_post_type() ) { // check the custom post type
// Get submitted values.
$values = $_POST['acf'];
// Check if a specific value was updated.
if( isset($_POST['acf']['bookings_field_status']) ) {
$send_email = $_POST['acf']['bookings_field_send_email'];
if ( $send_email === 'yes' ) {
$status = $_POST['acf']['bookings_field_status'];
if ( $status === 'confirmed' ) {
$subject = $_POST['acf']['bookings_field_email_subject_confirmed'];
} elseif ( $status === 'changed' ) {
$subject = $_POST['acf']['bookings_field_email_subject_changed'];
} elseif ( $status === 'canceled by guest' ) {
$subject = $_POST['acf']['bookings_field_email_subject_canceled_by_guest'];
} else {
$subject = $_POST['acf']['bookings_field_email_subject_canceled_by_us'];
}
if ( $status === 'confirmed' ) {
$subject = $_POST['acf']['bookings_field_email_body_confirmed'];
} elseif ( $status === 'changed' ) {
$subject = $_POST['acf']['bookings_field_email_body_changed'];
} elseif ( $status === 'canceled by guest' ) {
$subject = $_POST['acf']['bookings_field_email_body_canceled_by_guest'];
} else {
$subject = $_POST['acf']['bookings_field_email_body_canceled_by_us'];
}
$to = $_POST['acf']['bookings_field_email'];
$headers = array
(
'From: ' . get_bloginfo('name') . ' <' . get_bloginfo('admin_email') . '>',
'Bcc: ' . get_bloginfo('admin_email'),
'X-Mailer: PHP/' . phpversion(),
'MIME-Version: 1.0',
'Content-type: text/html; charset=iso-8859-1'
);
$headers = implode( "\r\n" , $headers );
wp_mail( $to, $subject, $body, $headers );
}
}
}
}
回答1:
UPDATE 2 Got it working. Code below:
add_action('acf/save_post', 'yl_send_booking_email_after_status_update', 5);
function yl_send_booking_email_after_status_update( $post_id ) {
// Get submitted values.
$values = $_POST['acf'];
// Check if a specific value was updated.
if( isset($_POST['acf']['field_5ed8117407663']) ) {
$to = $_POST['acf']['field_5ed41758ed4b5'];
$subject = $_POST['acf']['field_5ed81174076f2'];
$message = $_POST['acf']['field_5ed8117407741'];
$headers = array
(
'From: ' . get_bloginfo('name') . ' <' . get_bloginfo('admin_email') . '>',
'Bcc: ' . get_bloginfo('admin_email'),
'X-Mailer: PHP/' . phpversion(),
'MIME-Version: 1.0',
'Content-type: text/html; charset=iso-8859-1'
);
$headers = implode( "\r\n" , $headers );
wp_mail( $to, $subject, $message, $headers );
}
}
回答2:
The complete working code (for those who like to use):
add_action('acf/save_post', 'yl_send_booking_email_after_status_update', 5);
function yl_send_booking_email_after_status_update( $post_id ) {
// Get submitted values.
$values = $_POST['acf'];
// Check if a specific value was updated.
if ( isset($_POST['acf']['field_5ed8117407663']) ) {
$booking_status = $_POST['acf']['field_5ed8117407663'];
$send_email = $_POST['acf']['field_5ed81174076ac'];
if ( $send_email == 'yes' ) {
$to = $_POST['acf']['field_5ed41758ed4b5'];
if ( $booking_status == 'confirmed' ) {
$subject = $_POST['acf']['field_5ed81174076f2'];
$message = $_POST['acf']['field_5ed8117407741'];
} elseif ( $booking_status == 'changed' ) {
$subject = $_POST['acf']['field_5ed8117407788'];
$message = $_POST['acf']['field_5ed81174077df'];
} elseif ( $booking_status == 'canceled by guest' ) {
$subject = $_POST['acf']['field_5ed8117407829'];
$message = $_POST['acf']['field_5ed8117407870'];
} else {
$subject = $_POST['acf']['field_5ed81174078b7'];
$message = $_POST['acf']['field_5ed81174078fd'];
}
$headers = array
(
'From: ' . get_bloginfo('name') . ' <' . get_bloginfo('admin_email') . '>',
'Bcc: ' . get_bloginfo('admin_email'),
'X-Mailer: PHP/' . phpversion(),
'MIME-Version: 1.0',
'Content-type: text/html; charset=iso-8859-1'
);
$headers = implode( "\r\n" , $headers );
wp_mail( $to, $subject, $message, $headers );
}
}
}
来源:https://stackoverflow.com/questions/62221356/wordpress-post-update-after-acf-field-updates