Support » Plugin: Age Gate » How to add a custom field?

  • i can’t figure out how to add a custom field.

    I’ve read all the Guides but I cannot understand how to add a custom field (in italy we need more field like “codice fiscale”).

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Phil

    (@philsbury)

    Hi @logaen,

    Do you have some code that’s not working that I can have a look at and see if we can resolve?

    Thanks
    Phil

    Thread Starter logaen

    (@logaen)

    I can’t figure out where to put the code to have an additional field.
    I would like to insert a custom field that may require an alphanumeric field of exact 16 digits.

    • This reply was modified 4 months, 1 week ago by .
    Plugin Author Phil

    (@philsbury)

    Hi @logaen,

    It would go in your functions.php in your theme or child theme, or a custom plugin file.

    Here’s a fully example with what you’ve said above:

    /* Add the field */
    add_filter('post_age_gate_custom_fields', 'my_custom_fields', 10, 1);
    
    function my_custom_fields($fields)
    {
        $fields .= sprintf('<label>codice fiscale</label><br /> <input type="text" name="ag_field_codice_fiscale" value="%s" />', age_gate_set_value('ag_field_codice_fiscale'));
        $fields .= age_gate_error('ag_field_codice_fiscale');
    
        return $fields;
    }
    
    /* validate the field */
    add_filter('age_gate_validation', 'my_validation_rules', 10, 1);
    
    function my_validation_rules($rules)
    {
        return array_merge($rules, [
        'ag_field_codice_fiscale' => 'required|min_len,16,max_len,16|alpha_numeric'
      ]);
    }
    
    /* Set the field display name */
    add_filter('age_gate_field_names', 'my_field_names', 10, 1);
    
    function my_field_names($names)
    {
        return array_merge($names, [
        'ag_field_codice_fiscale' => "codice fiscale"
      ]);
    }
    
    /* We need to set a message for the alpha_numeric rule */
    add_filter('age_gate_validation_messages', 'my_error_messages');
    function my_error_messages($messages)
    {
        return array_merge($messages, [
            'validate_alpha_numeric' => 'The {field} field can only contain alpha numeric characters',
        ]);
    }

    Thanks
    Phil

    Thread Starter logaen

    (@logaen)

    Perfect. It works! Where does this data go?

    Plugin Author Phil

    (@philsbury)

    Hi @logaen,

    After the form is submitted, that data is gone. What do you want to do with it?

    There are hooks to allow you to store it somewhere should you want to.

    Thanks
    Phil

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