【问题标题】:Wordpress how to send email confirmations on post status changeWordpress 如何在帖子状态更改时发送电子邮件确认
【发布时间】:2021-09-30 20:33:12
【问题描述】:

我在WP travel 插件中有一个名为“Booking”的自定义帖子类型。当我将该自定义帖子类型的状态从“待定”更改为“已预订”时,我希望它触发一个事件。

事件应向相关用户发送确认电子邮件。

我尝试了以下方法:

function show_alert_msg( $new_status, $old_status, $post ) {
    if ( 'Booked' === $new_status && 'pending' !== $old_status && $post->post_type=='Booking' )
        
     {
         echo '<script>alert(Show message after transition to booked)</script>' ;
    }else{
        echo '<script>alert(failed)</script>' ;

    }
}
add_action( 'transition_post_status', 'show_alert_msg', 10, 3 );

但是每当我改变状态时,什么都没有发生。

我做错了什么?以及如何正确处理此问题,以便当预订状态从待定变为已预订时,将确认电子邮件发送给用户?

【问题讨论】:

    标签: php wordpress plugins wordpress-theming wordpress-admin


    【解决方案1】:

    这是因为您的代码中有几个错误!

    • 您正在使用WP travel plugin。此插件将其自定义帖子类型注册为 "itinerary-booking",但在您的代码中,您正在检查一个名为 "Booking" 的自定义帖子类型,它不存在,因此该条件永远不会返回 true!

      您可以导航到以下路径:
      “您的网站文件夹 > wp-content > 插件 > wp-travel > inc”
      并打开class-post-types.php 在线文件126 你会看到这个register_post_type( 'itinerary-booking', $args );

    • 另外,您正在使用transition_post_status 操作挂钩,用于检查帖子状态。 pendingbooked 的状态是自定义的帖子元数据,并以wp_travel_booking_status 名称保存到数据库中,与帖子状态本身无关!这意味着它与您尝试做的完全不同。

    所以,这个过滤器钩子transition_post_status 不负责获取这些状态。

    • 此外,在尝试调试/测试代码时,特别是在 transition_post_statussave_post 挂钩上,请使用 javascript alertconsole.log 和/或 php echo , print_r, var_dump 等等,因为当你点击这些钩子时,打印、回显、警报将不起作用。只需使用 php die 函数,它会停止应用程序并让您知道您成功了。

    • 大小写也很重要!确保您为您的值使用了正确的大小写,例如,在这种情况下,所有内容都是小写的。所以请留意!


    现在你能做些什么呢?

    1. 好吧,您可以将挂钩更改为 save_post 操作挂钩,这意味着每次您更新帖子时,它都会触发!
    2. 您可以更改条件检查以查找 itinerary-booking 自定义帖子类型。
    3. 你也可以使用get_post_meta函数来检查你的状态变化!

    现在将它们放在一起,您可以使用以下代码:

    add_action( 'save_post', 'sending_email_confirmation_to_user');
    
    function sending_email_confirmation_to_user( $post_id )
    {
    
      if ( 'itinerary-booking' == get_post_type( $post_id ) ){
    
        $post_status_after_update = get_post_meta( $post_id, 'wp_travel_booking_status', true );
    
        if( 'booked' === $post_status_after_update ){
          die("Send your email here!");
        } else {
          die("your post status is not booked!");
        }
      }
    
    }
    

    注意:

    • 我使用die 语句只是为了给你一个例子,所以请确保将die("Send your email here!"); 替换为你的发送电子邮件的自定义函数!
    • booked 的状态是小写,所以不要像您在代码中尝试的那样将其更改为大写!不然不行!
    • 当您的帖子状态不是“已预订”时,您无需执行任何操作,我使用die("your post status is not booked!"); 只是为了给您举个例子!

    更新

    当您使用save_post 挂钩时,它会在您每次更新自定义帖子数据时运行,您不想每次都发送电子邮件。对吧?

    当您将状态从“待定”更改为“已预订”时,您可以在数据库中save a meta data,这样您就知道您已经发送了一封电子邮件确认并且您不想再发送一封电子邮件。你可以使用update_post_meta 函数来做到这一点。

    add_action( 'save_post', 'sending_email_confirmation_to_user');
    
    function sending_email_confirmation_to_user( $post_id )
    {
    
      if ( 'itinerary-booking' == get_post_type( $post_id ) ){
    
        $post_email_status = get_post_meta( $post_id, 'custom_confirmation_email', true );
        $post_status_after_update = get_post_meta( $post_id, 'wp_travel_booking_status', true );
    
        if( 'booked' === $post_status_after_update && empty( $post_email_status )){
          $email_sent = update_post_meta( $post_id, 'custom_confirmation_email', 'sent-at-'. date( 'M_d_Y-H_i_s' ) );
          die("Send your email here!");
        } 
      }
    
    }
    

    注意:

    • 这将确保您只发送一次电子邮件确认!
    • 如果数据库中的电子邮件确认数据为空,它将发送电子邮件并更新数据库中的值,并且不会发送额外的电子邮件。
    • 另外,我在您的数据库中存储了一个名为custom_confirmation_email 的元数据,并使用'sent-at-'. date('M_d_Y-H_i_s') 作为它的值,如下所示:sent-at-Oct_01_2021-17_42_42 这是状态从“待定”更改为的日期和时间第一次“预订”。

    所有的 sn-ps 和解释都已经过全面测试并且可以正常工作!

    【讨论】:

    • 非常感谢,这解释了我的太多问题。
    • 我尝试了您建议的解决方案,但钩子 'save_post' 没有触发该功能,尝试了其他钩子但只有一个触发了该功能。它是 'update_post_meta' 但问题是它通过了一个参数,它不是帖子 ID,而是其他整数。注意:我在functions.php中编写这段代码
    • 当你说“save_post没有触发函数”,你怎么知道?因为我在将代码发送给您之前已经对其进行了测试。当您将状态从“待定”更改为“已预订”时,您会看到 die("Send your email here!"); 对吗?如果你看到它,这意味着你击中了正确的钩子。如果你想测试它,不要使用 javascript alert!你不会在屏幕上看到它。只需使用die("Send your email here!");
    • 我安装该插件只是为了解决您的问题,并测试了代码。如果你不能让它工作,那么你必须在其他地方有一个错误。我发给你的代码运行良好。
    • 对不起,你是对的,我犯了一个错误,使用 alert 而不是 die 函数进行测试,但它现在工作正常,我唯一的问题是每次更新自定义帖子时它都会触发它具有“已预订”状态,我希望它仅在我将其从 Pending 更改为 Booked 时触发。再次感谢您的帮助
    猜你喜欢
    • 2012-04-04
    • 1970-01-01
    • 2017-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-17
    • 1970-01-01
    相关资源
    最近更新 更多