• danidada

    (@danidada)


    I have a problem with a filter: I created a mu-plugin with this code to validate a field hidden by css that must remain empty (as an antibot field):

    add_filter( 'forminator_custom_form_submit_errors', 'check_form_data', 99, 3 );

    function check_form_data( $submit_errors, $form_id, $field_data_array ) {

    ? ? $valid = false;

    ? ? $antibotFieldName = "";

    ? ? foreach( $field_data_array as $val ) {

    ? ? ? ? if( $val['placeholder'] == 'antibot' ) {

    ? ? ? ? ? ? $antibotVal = $val['value'];

    ? ? ? ? ? ? $antibotFieldName = $val['name'];

    ? ? ? ? ? ? if($antibotVal == ""){

    ? ? ? ? ? ? ? ? $valid = true;

    ? ? ? ? ? ? }

    ? ? ? ? ? ? break;

    ? ? ? ? }

    ? ? }

    ? ? if( ! $valid ) {

    ? ? ? ? $submit_errors[][$antibotFieldName] = 'Antibot verifica fallita!';

    ? ? }

    ? ? return $submit_errors;

    }

    but I can’t make it work, it’s ignored..am I missing something?

    Thanks

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Support Saurabh – WPMU DEV Support

    (@wpmudev-support7)

    Hello @danidada

    Hope you’re doing well today!

    The error action hook that you are using seems correct, however, I am afraid reviewing and working on the action code will be out of support scope. For that, you’ll need to hire a developer to provide the required custom code for you.

    WordPress provides a jobs directory here https://jobs.wordpress.net/

    That said, looking at what you are trying to achieve, we have a similar “native” feature within Forminator Form Plugin known as “HoneyPot Protection” Have you already tried that?

    Ref: https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#enable-honeypot-protection

    You can enable it by navigating to the Form >> Behaviour >> Security >> Enable HoneyPot Protection.

    Kind Regards,
    Saurabh

    Thread Starter danidada

    (@danidada)

    thanks for your reply.

    yes honeypot is turned on and hcaptcha is also integrated in the form but there’s still spam.

    I can code so can you confirm how the filter works? Does it apply only to require fields? There’s another filter or hook I can use to add a custom validation for field? The form is submitted with ajax.

    thanks again

    Plugin Support Kris – WPMU DEV Support

    (@wpmudevsupport13)

    Hi @danidada

    I pinged our SLS Team to review your query. We will post an update here as soon as more information is available.

    Kind Regards,
    Kris

    Thread Starter danidada

    (@danidada)

    thank you, I’ll wait for them.

    Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @danidada

    On your code there are some errors in how you use the $field_data_array, you can always check it by using

    error_log( print_r( $field_data_array, true ) );

    Then check the debug.log file ( note you need to have the debug mode enabled )

    The field placeholder is inside of “field_array” key and $field_data_array is an array of each field so you need to target that key to reach the placehoder.

    An example of structure:

    
    (
        [0] => Array
            (
                [name] => name-1
                [value] => 
                [field_type] => name
                [key] => 1
                [field_array] => Array
                    (
                        [element_id] => name-1
                        [type] => name
                        [options] => Array
                            (
                            )
    
                        [cols] => 12
                        [conditions] => Array
                            (
                            )
    
                        [wrapper_id] => wrapper-2752-2028
                        [field_label] => Name
                        [placeholder] => E.g. John Doe
                        [prefix_label] => Prefix
                        [fname_label] => First Name
                        [fname_placeholder] => E.g. John
                        [mname_label] => Middle Name
                        [mname_placeholder] => E.g. Smith
                        [lname_label] => Last Name
                        [lname_placeholder] => E.g. Doe
                        [prefix] => true
                        [fname] => true
                        [mname] => true
                        [lname] => true
                        [required_message] => Name is required.
                        [prefix_required_message] => Prefix is required.
                        [fname_required_message] => First Name is required.
                        [mname_required_message] => Middle Name is required.
                        [lname_required_message] => Last Name is required.
                        [layout_columns] => 2
                        [parent_group] => 
                    )
    
                [form_field_obj] => Forminator_Name Object
                    (
                        [field] => Array
                            (
                            )
    
                        [form_settings] => Array
                            (
                            )
    
                        [name] => Name
                        [slug] => name
                        [category] => standard
                        [type] => name
                        [options] => Array
                            (
                            )
    
                        [settings] => Array
                            (
                            )
    
                        [autofill_settings] => Array
                            (
                                [name] => Array
                                    (
                                        [values] => Array
                                            (
                                                [wp_user] => Array
                                                    (
                                                        [name] => WordPress User
                                                        [attributes] => Array
                                                            (
                                                                [wp_user.display_name] => Array
                                                                    (
                                                                        [name] => Display Name
                                                                    )
    
                                                                [wp_user.login] => Array
                                                                    (
                                                                        [name] => Username
                                                                    )
    
                                                                [wp_user.first_name] => Array
                                                                    (
                                                                        [name] => First Name
                                                                    )
    
                                                                [wp_user.last_name] => Array
                                                                    (
                                                                        [name] => Last Name
                                                                    )
    
                                                            )
    
                                                    )
    
                                            )
    
                                    )
    
                                [name-prefix] => Array
                                    (
                                        [values] => Array
                                            (
                                            )
    
                                    )
    
                                [name-first-name] => Array
                                    (
                                        [values] => Array
                                            (
                                                [wp_user] => Array
                                                    (
                                                        [name] => WordPress User
                                                        [attributes] => Array
                                                            (
                                                                [wp_user.first_name] => Array
                                                                    (
                                                                        [name] => First Name
                                                                    )
    
                                                                [wp_user.display_name] => Array
                                                                    (
                                                                        [name] => Display Name
                                                                    )
    
                                                                [wp_user.login] => Array
                                                                    (
                                                                        [name] => Username
                                                                    )
    
                                                            )
    
                                                    )
    
                                            )
    
                                    )
    
                                [name-middle-name] => Array
                                    (
                                        [values] => Array
                                            (
                                            )
    
                                    )
    
                                [name-last-name] => Array
                                    (
                                        [values] => Array
                                            (
                                                [wp_user] => Array
                                                    (
                                                        [name] => WordPress User
                                                        [attributes] => Array
                                                            (
                                                                [wp_user.last_name] => Array
                                                                    (
                                                                        [name] => Last Name
                                                                    )
    
                                                                [wp_user.login] => Array
                                                                    (
                                                                        [name] => Username
                                                                    )
    
                                                            )
    
                                                    )
    
                                            )
    
                                    )
    
                            )
    
                        [defaults] => Array
                            (
                                [field_label] => Name
                                [placeholder] => E.g. John Doe
                                [prefix_label] => Prefix
                                [fname_label] => First Name
                                [fname_placeholder] => E.g. John
                                [mname_label] => Middle Name
                                [mname_placeholder] => E.g. Smith
                                [lname_label] => Last Name
                                [lname_placeholder] => E.g. Doe
                                [prefix] => true
                                [fname] => true
                                [mname] => true
                                [lname] => true
                                [required_message] => Name is required.
                                [prefix_required_message] => Prefix is required.
                                [fname_required_message] => First Name is required.
                                [mname_required_message] => Middle Name is required.
                                [lname_required_message] => Last Name is required.
                                [layout_columns] => 2
                            )
    
                        [hide_advanced] => 
                        [position] => 1
                        [is_input] => 
                        [has_counter] => 
                        [is_valid] => 1
                        [validation_message] => Array
                            (
                            )
    
                        [activated_autofill_providers:protected] => Array
                            (
                            )
    
                        [icon] => sui-icon-profile-male
                        [is_calculable] => 
                    )
    
            )
    
    )
    

    But it is easier if you target the field name instead, e.g text-1 then you can the $val[‘name’] == ‘something’ without field_array key.

    Note, that this is just a small guide we can’t provide the final code as it is outside our support scope.

    But to fight spam is better to think about website security in general example using Cloudflare, blocking some countries if you provide support to a specific country audience only.

    You can find more https://wpmudev.com/blog/prevent-form-comment-spam-forminator/

    Best Regards
    Patrick Freitas

    Thread Starter danidada

    (@danidada)

    Sorry for the late reply.

    Based on your suggestion I edited the code with:
    if( $val['field_array']['placeholder'] == 'antibot' ) {
    and it didn’t work, nothing changed.


    So I added in config.php

    define( 'WP_DEBUG', true ); define( 'WP_DEBUG_LOG', true );
    and changed the filter to:

    add_filter( ‘forminator_custom_form_submit_errors’, ‘check_form_data’, 99, 3 );
    function check_form_data( $submit_errors, $form_id, $field_data_array ) {
    ? ? error_log( print_r( $field_data_array, true ) );
    ? ? return $submit_errors;
    }

    but testing the form I see the validations error (leaving all fields blank) but no debug.log into wp-content folder. So my guess is that the filter is not firing.
    It’s an ajax form if I missed to say that before. The filter is added in a mu-plugin visible from wp backend.

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.