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;
}