我有一个名为“Envios”的自定义帖子类型。我在其中创建了一个自定义元框,可以选择用户和帖子。当其中一个自定义帖子类型发布时,它会向所选用户发送一封包含所选帖子内容的电子邮件。要选择帖子,我使用ACF插件和field Post对象。
问题是,有时电子邮件发送正确,但在大多数情况下,什么都没有发送。
代码如下:
// Adds Custom Meta Box
function email_delivery_munda_add_custom_box() {
$screens = [\'envios\', \'md_cpt\'];
foreach ($screens as $screen) {
add_meta_box(
\'md_email_delivery_box_id\', // Unique ID
\'Detalles de email\', // Box title
\'md_email_delivery_custom_box_html\', // Content callback, must be of type callable
$screen, // Post type
\'side\',
\'default\'
);
}
}
add_action(\'add_meta_boxes\', \'email_delivery_munda_add_custom_box\');
// Adds the content of the Custom Meta Box
function md_email_delivery_custom_box_html($post)
{
$asunto = get_post_meta($post->ID, \'email_delivery_asunto\', true);
wp_nonce_field( \'save_md_email\', \'md_email_nonce\' );
if ($asunto == "") {
$asunto = "Informe - " . date("d-m-Y");
}
?>
<div class="email-delivery-asunto">
<label for="email_delivery_asunto">Asunto</label>
<input type="text" name="email_delivery_asunto" id="email_delivery_asunto" class="postbox" value=" <?php echo $asunto ?>" placeholder="Informe - <?php echo date("d-m-Y") ?>">
</div>
<div class="email-delivery-receptores">
<label for="select-emails">Destinatarios</label>
<?php
$blogusers = get_users();
// Array of WP_User objects.
echo \'<select name="clients-email-input[]" id="select-emails" class="admin-email-receptores" multiple="multiple"><option></option>\';
foreach ( $blogusers as $user ) {
echo \'<option value="\'. esc_html( $user->user_email ) .\'">\' . esc_html( $user->display_name ) . \'</option>\';
}
echo \'</select>\';
?>
</div>
<?php $enviados = get_post_meta($post->ID, \'email_send\', true); ?>
<div class="email-delivery-asunto">
<label for="email_send">Ultimo envio</label>
<p id="email_send"><?php echo $enviados ?></p>
</div>
<?php
}
// Saves post data
function md_email_save_postdata($post_id) {
if ( ! isset( $_POST[\'md_email_nonce\'] ) ) {
return $post_id;
}
if ( ! wp_verify_nonce( $_POST[\'md_email_nonce\'], \'save_md_email\' ) ) {
return $post_id;
}
$asunto = sanitize_text_field( $_POST[\'email_delivery_asunto\'] );
update_post_meta( $post_id, \'email_delivery_asunto\', $asunto );
}
add_action(\'save_post\', \'md_email_save_postdata\');
// Sends an email when custom post type Envios is published
function md_email_publish_postdata($post_id) {
$asunto = sanitize_text_field( $_POST[\'email_delivery_asunto\'] );
$headers = array(
\'Content-Type: text/html; charset=UTF-8\'
);
$enviados = sanitize_text_field( $_POST[\'email_send\'] );
$blogusers = get_users();
foreach ($_REQUEST[\'clients-email-input\'] as $selectedOption) {
array_push($headers, "BCC:" . $selectedOption);
foreach ( $blogusers as $user ) {
if ($user->user_email === $selectedOption) {
$enviados .= "<b>" . esc_html( $user->display_name ) . "</b> " . esc_html( $user->user_email) . "<br>";
}
}
}
update_post_meta( $post_id, \'email_send\', $enviados );
$content = \'\';
// Removing from here makes it work.
$posts = get_field(\'casos\',$post_id);
if( $posts ): ?>
<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post);
$content .= \'<h1>\' . get_the_title( $post ) . \'</h1><br>\';
$content .= \'<p>\' . get_field( \'descripcion_breve\', $post) . \'</p><br>\';
endforeach; ?>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif;
// until here.
$clients_list = "[email protected]";
wp_mail( $clients_list, $asunto , $content, $headers );
}
add_action(\'publish_envios\', \'md_email_publish_postdata\', 10, 2 );
如果我将ACF部件从
// Removing from here makes it work. 到
// until here. 电子邮件每次都可以发送,但没有选定的帖子内容。
我真的不知道是哪个问题,但也许你可以在这件事上传授一些智慧。提前感谢!
<小时>
SOLUTION:
ACF在帖子发布后使用操作ACF/save\\u post,所以我试图获取一个还没有的值。因此,我将操作更改为acf save\\u post,并在两者之间使用
if (get_post_status($post_id) == \'publish\') {
检查帖子的状态。
此外,我还更新了对象Post输出,如下所示:
$posts = get_field( \'casos\', $post_id );
if( $posts ):
foreach( $posts as $post):
$content .= \'<h1>\' . get_the_title( $post ) . \'</h1><br>\';
if ( get_field( \'descripcion_breve\', $post->ID )) {
$content .= \'<p>\' . get_field( \'descripcion_breve\', $post->ID ) . \'</p><br>\';
} else {
$content .= \'<p>Description empty</p><br>\';
}
endforeach;
endif;