Home › Forums › Front-end Issues › Front End Captcha
Hi @trasek
I assume so. I have not worked with captcha before, but ACF does allow you to create a custom field type.
You could create a new field type which renders out a captcha and handles the $_POST data.
Here’s a tutorial to get you started:
http://www.advancedcustomfields.com/resources/tutorials/creating-a-new-field-type/
Thanks
E
Ok i display the recaptcha but how i can get $_POST object and how i can check it’s right ? I know the function of reCaptcha to do that, but in what function i need to do that and how i can get from one function to another, send this post ?
Hi @trasek
you can use the update_value function to run the validation.
The only issue is that currently, ACF does not have any PHP validation functions!
This will be part of v5, so for now, I’m not sure how you will prevent the saving.. Perhaps remove all $_POST[‘fields’] data?
I’m not too sure on this one.
Thanks
E
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)
Hi Elliot,
any update on this?
Is there any easy way to add captcha to front end form in v5?
yep, forms spamming is a real pain in the ass 🙁
If someone succeed to put a captcha or another solution to avoid spams I think a lot of us would be happy 🙂
I’ve tried the solution above ( from slightlydifferent ), but sadly I can’t make it functionnal.
in current project we used gravity forms as it has support for recaptcha/nocaptcha, but it would be great to be able to use acf as we’re using it anyway in the backend
I had just created an add-on for ACF v5 that allows reCAPTCHA on frontend forms.
Check it out at https://github.com/irvinlim/acf-recaptcha, do let me know if you encounter any issues!
great plugin, one issue found – https://wordpress.org/support/topic/translation-issue-30?replies=1#post-7259485
The topic ‘Front End Captcha’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.