Skip to content

Quick WordPress snippet to create a Sendy subscribe shortcode, validates and submits via JS

edited July 2021 in Contributions

This PHP code snippet creates a WordPress shortcode named [sendy-subscribe] which can then be placed almost anywhere to output a nice Sendy subscribe form.

Replace YOUR-POST-URL-HERE with your Sendy install URL such as
Replace YOUR-LIST-ID-HERE with your List ID.
BuyerType field is an example custom field drop-down, rename it, delete it, switch to radio, text, etc. Note that custom fields are Case-Sensitive, match them exactly.

I'd recommend using the Snippets plugin to easily add and manage code snippets, much safer than using functions.php, or create your own custom plugin. Once the snippet is added and enabled on the front-end, you can use the shortcode [sendy-subscribe] almost anywhere to add a form to the website.

It will submit via JS to enforce the required name/email fields.

Here's the PHP code snippet:

 function sendy_shortcode() {
 return '<script>
     document.getElementById("name").required = true;
     document.getElementById("email").required = true;
     var form = document.getElementById(\'sendy-subscribe-form\');
     form.addEventListener(\'submit\', function(ev) {
     }, false);
 <div id="submitted-msg" class="" style="display:none"></div>
 <form name="sendy-subscribe-form" id="sendy-subscribe-form" action="https://YOUR-POST-URL-HERE/subscribe" method="post">
     <label for="name">Name</label><br/>
     <input type="text" name="name" id="name"/>
     <label for="email">Email</label><br/>
     <input type="email" name="email" id="email"/>
     <p><label for="BuyerType">I am a:</label> <select name="BuyerType" class="select " id="BuyerType">
         <option value="">I am a:</option>
         <option value="Retail Consumer">Retail Consumer</option>
         <option value="Wholesale Buyer">Wholesale Buyer</option></select></p>
     <p style="display:none;"><input type="text" name="hp" class="hidden " id="hp" value=""></p>
     <p><input type="submit" name="submit" id="form-submit-btn" value="Subscribe" class="button"></p>
     <input type="hidden" name="list" value="YOUR-LIST-ID-HERE"/>
     <input type="hidden" name="subform" value="yes"/>
 add_shortcode('sendy-subscribe', 'sendy_shortcode');

If there's any interest, I may create a more mature WordPress plugin that adds hiding the form, avoiding the Sendy response pages, and just displays a message... reach out.


This discussion has been closed.