WordPress post 在 ACF 字段更新后更新
WordPress post update AFTER ACF field updates
每次我(更改 STATUS 字段然后)safe/update post 时,下面的代码都会发送一封电子邮件。但它没有按照我想要的方式工作,我知道出了什么问题:
字段更新总是在 post 保存之后保存,因此它总是发送上一个字段值(就像我总是落后 1 步一样)。
如何让 POST UPDATE 最后进行,以便随后触发的操作(发送电子邮件)包含最新的字段值?
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 );
更新:
从 ACF 团队获得一些信息后,我不得不更改我的代码。见下文。我没有收到任何错误,但没有发送任何电子邮件...有什么想法吗?
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 );
}
}
}
}
更新 2
得到它的工作。代码如下:
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 );
}
}
完整的工作代码(给喜欢用的人):
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 );
}
}
}
每次我(更改 STATUS 字段然后)safe/update post 时,下面的代码都会发送一封电子邮件。但它没有按照我想要的方式工作,我知道出了什么问题:
字段更新总是在 post 保存之后保存,因此它总是发送上一个字段值(就像我总是落后 1 步一样)。
如何让 POST UPDATE 最后进行,以便随后触发的操作(发送电子邮件)包含最新的字段值?
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 );
更新: 从 ACF 团队获得一些信息后,我不得不更改我的代码。见下文。我没有收到任何错误,但没有发送任何电子邮件...有什么想法吗?
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 );
}
}
}
}
更新 2 得到它的工作。代码如下:
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 );
}
}
完整的工作代码(给喜欢用的人):
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 );
}
}
}