在PUBLISH_POST操作中检索wp_mail的ACF字段

时间:2018-10-17 作者:Nicolas Landgraf

我有一个名为“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;

1 个回复
最合适的回答,由SO网友:djboris 整理而成

。。。删除了我以前的“解决方案”。。。

所以,这里真正的问题是这个函数publish_post 操作,如果是新帖子,此时可能尚未保存帖子。只有当它是一个已经保存但具有其他状态的帖子时,我们才能使用访问ACF值get_field().

解决方案可能是使用挂钩acf/save_post 优先级为10或更大,因为它在保存帖子后激发,因此允许我们使用get_field().

这是使用此挂钩的一个简短示例:

function my_acf_save_post( $post_id ) {

    // bail if wrong post type
    if( get_post_type( $post_id ) !== \'envios\' ) {
         return;
    }

    // get new value
    $posts = get_field( \'casos\', $post_id );


    // do something

}

add_action(\'acf/save_post\', \'my_acf_save_post\', 20);

结束

相关推荐

如何让`wp-list-table`显示我在Custom-Post中的`Custom-Fields`

一切都好吗<我需要wp-list-table 也要显示custom-fields 在每个custom-post 我有,但我不知道如何做到这一点,在这幅图中,它显示了带有字段的表格:Title, Author and Publication Date: 我想要的是能够选择custom-fields 将出现,例如以下示例Title, Carta, Naipe, Author, and Date of Publication:

在PUBLISH_POST操作中检索wp_mail的ACF字段 - 小码农CODE - 行之有效找到问题解决它

在PUBLISH_POST操作中检索wp_mail的ACF字段

时间:2018-10-17 作者:Nicolas Landgraf

我有一个名为“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;

1 个回复
最合适的回答,由SO网友:djboris 整理而成

。。。删除了我以前的“解决方案”。。。

所以,这里真正的问题是这个函数publish_post 操作,如果是新帖子,此时可能尚未保存帖子。只有当它是一个已经保存但具有其他状态的帖子时,我们才能使用访问ACF值get_field().

解决方案可能是使用挂钩acf/save_post 优先级为10或更大,因为它在保存帖子后激发,因此允许我们使用get_field().

这是使用此挂钩的一个简短示例:

function my_acf_save_post( $post_id ) {

    // bail if wrong post type
    if( get_post_type( $post_id ) !== \'envios\' ) {
         return;
    }

    // get new value
    $posts = get_field( \'casos\', $post_id );


    // do something

}

add_action(\'acf/save_post\', \'my_acf_save_post\', 20);

相关推荐

如何让`wp-list-table`显示我在Custom-Post中的`Custom-Fields`

一切都好吗<我需要wp-list-table 也要显示custom-fields 在每个custom-post 我有,但我不知道如何做到这一点,在这幅图中,它显示了带有字段的表格:Title, Author and Publication Date: 我想要的是能够选择custom-fields 将出现,例如以下示例Title, Carta, Naipe, Author, and Date of Publication: