免费自动发送 Discourse 邀请邮件的方法(适用于 WordPress 及其他平台)

继续讨论来自 使用 Zapier 自动发送 Discourse 邀请邮件

在实现目标的过程中,我发现了这个主题。这很棒,但 Zapier 毕竟不是免费的,:frowning: 所以我创建了这两个代码,希望它们对某人有用。请将它们放入您的主题 functions.php 文件中。

如果您也有类似的有用代码,请告诉我 :smiley: 我想更多地了解 Discourse API,以及仅通过编码免费将它们与 WordPress 连接的方法 ^^!

在用户提交评论后(待审核状态)发送 Discourse 邀请

function invite_forum_from_comment($comment_ID) {
    $comment = get_comment($comment_ID);
    $comment_email = get_comment_author_email($comment);
    $maybe_notify = ('0' == $comment->comment_approved);
    if ($maybe_notify) {
        // 邀请用户
        wp_remote_post('https://forum.yourdomain.com/invites', array(
            'method' => 'POST',
            'headers' => array(
                'Content-Type' => 'multipart/form-data',
                'Api-key' => 'yourapikey',
                'Api-Username' => 'yourusername'
            ),
            'body' => array(
                'email' => $comment_email,
                'custom_message' => '感谢您的评论,在等待审核期间,为什么不加入我们的论坛呢?!'
            )
        ));
    }
}
add_action('comment_post', 'invite_forum_from_comment');

在用户完成 WooCommerce 订单后邀请其加入 Discourse

function invite_forum_from_order($order_id) {
    // 获取所有订单数据
    $order = new WC_Order($order_id);
    // 从订单中获取用户邮箱
    $order_email = $order->billing_email;
    // 邀请用户
    wp_remote_post('https://forum.yourdomain.com/invites', array(
        'method' => 'POST',
        'headers' => array(
            'Content-Type' => 'multipart/form-data',
            'Api-key' => 'yourapikey',
            'Api-Username' => 'yourusername'
        ),
        'body' => array(
            'email' => $order_email,
            'custom_message' => '感谢您的订单,何必浪费时间等待,直接加入我们的论坛吧!'
        )
    ));
}
// 将此新创建的函数添加到感谢页面
add_action('woocommerce_thankyou', 'invite_forum_from_order', 10, 1);

更新:此代码将仅邀请已批准的评论,以防止邀请垃圾邮件发送者。

function invite_forum_from_approved_comment($comment_ID) {
 $comment = get_comment( $comment_ID );
 $comment_email = get_comment_author_email($comment);
 $maybe_notify = ( '1' == $comment->comment_approved );
 if ( $maybe_notify ) {
  // 邀请用户
  wp_remote_post( 'https://forum.fususu.com/invites', array(
      'method' => 'POST',
      'headers' => array('Content-Type' => 'multipart/form-data', 
                         'Api-key' => 'xxxxxxxxxxxxxxxxxxxx',
                         'Api-Username' => 'xxxx'),
      'body' => array('email' => $comment_email, 
                     'custom_message' => '感谢您的评论。您的评论已获批准,您已获得加入我论坛的邀请。')
        )
  );  
 };
}
add_action('comment_unapproved_to_approved', 'invite_forum_from_approved_comment');

在邀请我的联系人加入 Mautic 系统的论坛途中,
我发现 Mautic 已经具备一个非常出色的 webhook 功能,可以向 Discourse 发送 POST 请求 :D,^^!只需在活动中创建一个操作,然后就完成了!无需编写任何代码 :smiley:



为 Zapier 感到惋惜,它们确实不错,但我更喜欢无限制且免费的解决方案 :blush: