Support

Account

Home Forums Front-end Issues Front End Captcha Reply To: Front End Captcha

  • I nearly have a Captcha working for front end forms using the Captcha plugin from http://bestwebsoft.com/plugin/ and based on code by Jake at http://wordpress.stackexchange.com/questions/66376/advanced-custom-fields-validation

    In the form template add:

    
    if( function_exists( 'cptch_display_captcha_custom' ) ) {
        $html_after_fields .= '<div id="directory-footer" style="" class="field required"><label for="cptch_number">Anti-spam verification <span class="required">*</span></label>';
        $html_after_fields .= "<input type='hidden' name='cntctfrm_contact_action' value='true' />"; 
        $html_after_fields .= cptch_display_captcha_custom();
        $html_after_fields .= "</div>";
    }
    

    Then in your functions.php or wherever you handle your custom code something like:

    
    class found_directory_frontend {
        public $submitted_fields = '';
        public $validation_errors = '';
    
        public function __construct() {
            add_filter('acf/pre_save_post', array($this, 'pre_save_post'));
            add_action('acf/save_post', array($this, 'custom_handle_error'), 1);
            ... any other handlers
        }
    
        function acf_create_fields($fields, $post_id) {
            foreach ($fields as &$field) {
                if ( array_key_exists($field['name'], $this->submitted_fields) )
                    $field['value'] = $this->submitted_fields[$field['name']];
            }
            return $fields;
        }
    
        function add_error() {
            echo '<div id="message" class="error">';
            foreach ( $this->validation_errors as $key => $error ) {
                echo '<p class="' . $key . '">' . $error . '</p>';
            }
            echo '</div>';
        }
        
        function custom_handle_error($post_id) {
            if ('error' == $post_id) {
                unset($_POST['return']);
            }
        }
        
        function pre_save_post($post_id) {
            
            if (empty($_POST['fields']) || !is_array($_POST['fields'])){
                $this->validation_errors['empty'] = "One or more required fields have not been completed";
            } else {
                foreach($_POST['fields'] as $k => $v) {
                    // get field
                    $f = apply_filters('acf/load_field', false, $k );
                    $f['value'] = $v;
                    $this->submitted_fields[$f['name']] = $f;
                }
                $this->submitted_fields['post_title'] = $_POST['post_title'];
            }
            
            if( function_exists( 'cptch_check_custom_form' ) && cptch_check_custom_form() !== true ) {
                $this->validation_errors['empty'] = "Please complete the Anti-spam verification.";
            }
            
            if ($this->validation_errors) {
                // Output the messges area on the form
                add_filter( 'acf/get_post_id', array($this, 'add_error') );
                
                // Turn the post_id into an error
                $post_id = 'error';
                
                // Add submitted values to fields
                add_action('acf/create_fields', array($this, 'acf_create_fields'), 10, 2 );
                
                return $post_id;
            }
            ... otherwise carry on with whatever it is your going to do
        }
    }
    

    The Captcha works perfectly and the error message displays fine. However the issue is the server side validation then re-loading the form with the POSTed values. All standard values load fine thanks to the code above but I have several image fields and two taxonomy fields in my form and these lose their values. Does anyone have any idea how to load those? If so then we have a working front end form Captcha :o)