• Resolved Chris

    (@comradeseidl)


    Hi,

    I have been poking around your code but haven’t found a convenient way to add an anti-spam question to the newsletter subscription form. For example I like to use the question, “What color is an orange?” and place it on my forms but I don’t see how I can do that and validate the answer. If you could explicitly point me in the right direction (what function or file should I edit or hook into?) or even provide me with a snippet, that would be great.

    To be clear, one reason why this is a necessary feature for me to add is I just got an email from SendGrid, which we use for sending emails from our cloud server. They noticed the high number of bounces that we have been getting from sending out our newsletters; these bounces owe to the high number of bot subscriptions that we get. We are using the double opt-in feature but I would like to provide one more layer of security because SendGrid is threatening to suspend our account which would pretty much shut down our site’s operations.

    Thanks!

    http://wordpress.org/extend/plugins/alo-easymail/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author eventualo

    (@eventualo)

    Hi Comrade, in EasyMail you can add custom fields using some hooks:
    http://www.eventualo.net/blog/easymail-newsletter-for-developers/#tutorial-customfields
    I wrote some code for you. It’s a draft starting point to show the hooks you can use. In this sample I set a global var ($alo_em_captcha) and I use it to (potentially) create a captcha and check the submitted value. You have to make all the hard work, mine it’s just a suggestion… I hope it helps you.

    /**
     * Add a captcha input field in subscription form
     */
    function custom_easymail_add_captcha ( $fields ) {
    
    	global $alo_em_captcha;
    
    	// Set a captcha global variable you can use to verify
    	// e.g. it could be an array with more parameters
    	$alo_em_captcha = rand( 1, 1000 );
    
    	// Custom field: Captcha
    	$fields['cf_captcha'] = array(
    		'humans_name'		=> __("CAPTCHA", "alo-easymail"),
    		'sql_attr' 			=> "VARCHAR(1) NOT NULL",
    		'input_type' 		=> "text",
    		'input_mandatory' 	=> true,
    		'input_validation' 	=> 'custom_easymail_cf_check_captcha',
    		'input_attr'		=> ' maxlength="20" '
    	);
    
    	return $fields;
    }
    add_filter ( 'alo_easymail_newsletter_set_custom_fields', 'custom_easymail_add_captcha' );
    
    /**
     * Sample of validation function
     */
    function custom_easymail_cf_check_captcha ($data) {
    	global $alo_em_captcha;
    
    	// check the submitted $data in captcha against a value based on $alo_em_captcha, and return true or false
    }
    
    /**
     * If your captcha needs some javascript, you can add it in your theme footer
     */
    function custom_easymail_add_captcha_js ( $fields ) {
    	global $alo_em_captcha; ?>
    <script type="text/javascript">
    //<![CDATA[
    
    // create your captcha javascript here, based on $alo_em_captcha:
    // <?php echo $alo_em_captcha; ?>
    
    //]]>
    </script>
    <?php
    }
    add_action ( 'wp_footer', 'custom_easymail_add_captcha_js' );
    
    /**
     * You can translate the captcha label, based on $alo_em_captcha
     */
    function custom_easymail_gettext ( $translated_text, $untranslated_text, $domain ) {
    	global $alo_em_captcha;
    
    	if ( $untranslated_text === 'CAPTCHA' && $domain == "alo-easymail" ) {
    
    		// Set the question text, based on $alo_em_captcha
    		return __('Your captcha question...'). $alo_em_captcha;
    
    	}
    	return $translated_text;
    }
    add_filter('gettext', 'custom_easymail_gettext', 20, 3);
    Thread Starter Chris

    (@comradeseidl)

    Thanks! I had looked at the custom field stuff prior to posting my original question and didn’t think it would work, but now that I’m looking at your code I really don’t know what I was thinking. :-/ Anyway your solution works perfectly just like your documentation says. Here is the code that I used:

    /**
     * Add an anti-spam question to Easymail newsletter subscription form
     */
    function easymail_add_antispam ( $fields ) {
    
    	// Custom field: Captcha
    	$fields['cf_captcha'] = array(
    		'humans_name'		=> __("What color is an orange?", "alo-easymail"),
    		'sql_attr' 			=> "VARCHAR(1) NOT NULL",
    		'input_type' 		=> "text",
    		'input_mandatory' 	=> true,
    		'input_validation' 	=> 'easymail_validate_antispam',
    		'input_attr'		=> ' maxlength="20" '
    	);
    
    	return $fields;
    }
    add_filter ( 'alo_easymail_newsletter_set_custom_fields', 'easymail_add_antispam' );
    
    /**
     * Validate input of antispam question
     */
    function easymail_validate_antispam ($data) {
    	$text = strtolower($data);
    	if ($text == 'orange')
    		return true;
    	else
    		return false;
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Adding an anti-spam question to subscription form’ is closed to new replies.