Sendy.co + Bricks Form Custom Action Integration (FINALLY)

Hello there!

After my last post outlining our arduous efforts at creating a Sendy.co + native Bricks Forms integration via custom actions and then resorting to a custom element, and also after a week or so of learning the most basic of PHP basics, I am glad to let you know that I finally developed a Bricks Forms Custom Action that integrates the self-hosted mailing platform Sendy.

I used the Subscribe via AJAX example API provided here which allowed me to avoid Sendy’s redirects altogether and just subscribe a user to a given list.

I also extensively read the Bricks documentation on Custom Actions in order to understand Bricks form functions and the structure of the arrays they provide. The method I ended up using was calling the different form fields needed by their name attribute rather than ID, in order for the Custom Action to be easily transferable from website to website. Essentially if the name HTML attribute of your Name field is ‘name’, and the name HTML attribute of your email field is ‘email’ your form should work, but here’s the kicker.

I also made the Sendy list ID dynamic, so you can use this custom action with multiple fields. All you need to do is create a hidden field like below and give it a name attribute of ‘sendy-list-id’ then pass your list ID as the value of the field, and voila: A real Bricks Builder Sendy.co multi mailing list integration.

WhatsApp Image 2024-05-29 at 00.51.43

I didn’t write any error checking for now, but this is definitely on my to do list now that I got this working. Hope this helps somebody, although I realise not many people self-host their mailing campaign software. Here’s the code for the custom action, which can be plugged in a child theme’s functions.php. Of course, you would need to replace the placeholders with your Sendy API key and your Sendy installation link.

Maybe this could even lay the groundworks for a Sendy.co integration with vanilla Bricks in the future, in the same way we have a Mailchimp and a Sendgrid integration, since the process doesn’t look that hard.

Cheers,
Tudor.

add_action( 'bricks/form/custom_action', 'sendy_subscription_form', 10, 1 );

function sendy_subscription_form( $form ) {

	$form_settings = $form->get_settings();

  	$form_fields   = $form_settings['fields'];

  	$form_completed_fields = $form->get_fields();

  	foreach ($form_fields as $field_number => $field_values) {

  		switch ($field_values['name']) {

  			case 'name':
				$name_field_id = $field_values['id'];
  				break;

  			case 'email':
				$email_field_id = $field_values['id'];
  				break;

  			case 'sendy-list-id':
				$sendy_list_field_id = $field_values['id'];
  				break;
  		}

  	}

  	$name = $form_completed_fields['form-field-'.$name_field_id];
  	$email = $form_completed_fields['form-field-'.$email_field_id];
  	$sendy_list_id = $form_completed_fields['form-field-'.$sendy_list_field_id];

	//-------------------------- You need to set these --------------------------//
	$sendy_url = 'Your Sendy installation link'; //Your Sendy installation (without the trailing slash)
	$api_key = 'Your Sendy API key'; //Can be retrieved from your Sendy's main settings
	//---------------------------------------------------------------------------//

	//POST variables
	// $name = $_POST['name'];
	// $email = $_POST['email'];
	
	//subscribe
	$postdata = http_build_query(
	    array(
	    'name' => $name,
	    'email' => $email,
	    'list' => $sendy_list_id,
	    'api_key' => $api_key,
	    'boolean' => 'true'
	    )
	);
	$opts = array('http' => array('method'  => 'POST', 'header'  => 'Content-type: application/x-www-form-urlencoded', 'content' => $postdata));
	$context  = stream_context_create($opts);
	$result = file_get_contents($sendy_url.'/subscribe', false, $context);
	//--------------------------------------------------//
	
	echo $result;
}
3 Likes

Thank you for this. I modified the code to be more robust, but this was amazing starting point. TY!