Automate sending Discourse invite emails with Zapier の議論に続きます:
目標達成への道中で、このトピックを見つけました。素晴らしいですが、Zapier は結局無料ではないので
、以下の 2 つを作成しました。誰かの役に立てば幸いです。これらをテーマの functions.php に追加してください。
もし、これに似た便利なコードがあれば教えてください
!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 );

