Continuing the discussion from Automate sending Discourse invite emails with Zapier:
On the way to reach my goals, I found this topic. That’s great, but Zapier is not free after all, so I created these two, hope they’ll be useful for someone. Put them in your theme functions.php
And if you have some useful codes similar to that, tell me I want to learn more about Discourse API and free way to connect them with wordpress just by coding ^^!
send Discourse invitation to after they submit a comment (pending status)
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 ) {
// invite user
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' => 'Thanks for comment, while waiting for moderation, why not join our forum?!')
)
);
};
}
add_action('comment_post', 'invite_forum_from_comment');
**Invite user to Discourse after making a woocommerce order **
function invite_forum_from_order( $order_id ) {
// get all the order data
$order = new WC_Order($order_id);
//get the user email from the order
$order_email = $order->billing_email;
// invite user
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' => 'Thanks for your order, why waste your time waiting, just join our forum!')
)
);
}
//add this newly created function to the thank you page
add_action( 'woocommerce_thankyou', 'invite_forum_from_order', 10, 1 );