Free ways to automate sending Discoures invite emails with wordpress (and other platform)

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, :frowning: 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 :smiley: 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 );
5 Likes

Update: This code will invite only approved comment to prevent from inviting some spammers.

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 ) {
  // invite user 
  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' => 'Thank you. Your comment is approved and you earned an invitation to join my forum.')
        )
  );  
 };
}
add_action('comment_unapproved_to_approved', 'invite_forum_from_approved_comment');
1 Like

And on the way to invite my contacts on a Mautic system to the forum,
I found that Mautic already had a very good webhook function that could send a POST to Discourse :D, ^^! Just create an action inside a campaign, then it’s done! no code needed :smiley:



Feel sorry for Zapier, they are good, but I like free solution with no limitations :blush:

2 Likes