Contact me if you need to build a complex form with complex functions by using Caldera Forms (Create new user, send SMS to user, email validation, delete user, delete post...)In this tutorial I will guide you how to make a custom email validation processor for Caldera Forms which could prevent users from entering some email providers. I will build a tiny plugin for this purpose.

Two email providers I will use in this demo is @abc.com and @xyz.com.

I/ Making a demo form for testing

In order to demonstrate the function of this plugin, I will make a form as below, you can try to enter example@abc.com to check.

  • Email field with Slug = client_email, this field is required.
  • Submit button

II/ Coding the plugin

There are only 2 main files: cf-email-validator.php and config.php. Following this tutorial, this is the code:

  • Change line 108, 109 for your email providers which you want to block
  • Change line 81 for error message ‘We dont accept ABC.com – XYZ.com’…
  1. cf-email-validator.php
<?php

/**
 * The plugin bootstrap file
 *
 * This file is read by WordPress to generate the plugin information in the plugin
 * admin area. This file also includes all of the dependencies used by the plugin,
 * registers the activation and deactivation functions, and defines a function
 * that starts the plugin.
 *
 * @link              https://giangle.dev/
 * @since             1.0.0
 * @package           Cf_Email_Validator
 *
 * @wordpress-plugin
 * Plugin Name:       Caldera Forms Email Validator
 * Plugin URI:        https://giangle.dev/creating-a-custom-validation-processor-for-caldera-forms/
 * Description:       This tiny plugin adds 1 processor for Caldera Forms which could block some email providers.
 * Version:           1.0.0
 * Author:            GiangLe.DEV
 * Author URI:        https://giangle.dev/
 * License:           GPL-2.0+
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 * Text Domain:       cf-email-validator
 * Domain Path:       /languages
 */
add_filter('caldera_forms_get_form_processors', 'block_email_cf_validator_processor');

/**
 * Add a custom processor for field validation
 *
 * @uses 'block_email_cf_validator_processor'
 *
 * @param array $processors Processor configs
 *
 * @return array
 */
function block_email_cf_validator_processor($processors){
    $processors['block_email_cf_validator'] = array(
        'name' => __('Block Email Providers', 'GiangLe.DEV'),
        'description' => 'Block some unwanted email providers - GiangLe.DEV',
        'pre_processor' => 'block_email_validator',
        'template' => dirname(__FILE__) . '/config.php'

    );

    return $processors;
}

/**
 * Run field validation
 *
 * @param array $config Processor config
 * @param array $form Form config
 *
 * @return array|void Error array if needed, else void.
 */
function block_email_validator( array $config, array $form ){

    //Processor data object
    $data = new Caldera_Forms_Processor_Get_Data( $config, $form, block_email_cf_validator_fields() );

    //Value of field to be validated
    $value = $data->get_value( 'client_email' );

    //if not valid, return an error
    if( false == block_email_cf_validator_is_valid( $value ) ){

        //get ID of field to put error on
        $fields = $data->get_fields();
        $field_id = $fields[ 'client_email' ][ 'config_field' ];

        //Get label of field to use in error message above form
        $field = $form[ 'fields' ][ $field_id ];
        $label = $field[ 'label' ];

        //this is error data to send back
        return array(
            'type' => 'error',
            //this message will be shown above form
            'note' => sprintf( 'Please Re-Correct %s. We dont accept ABC.com - XYZ.com', $label),
            //Add error messages for any form field
            'fields' => array(
                //This error message will be shown below the field that we are validating
                $field_id => __( 'This field is invalid', 'client_email' )
            )
        );
    }

    //If everything is good, don't return anything!

}


/**
 * Check if value is valid
 *
 * UPDATE THIS! Use your array of values, or query the database here.
 *
 * @return bool
 */

function block_email_cf_validator_is_valid( $value ){
  $email_extract = explode('@',$value);
  $email_check = $email_extract[1];
  
    return !in_array( $email_check, array(
        'abc.com',
        'xyz.com'
    ) );
}


/**
 * Processor fields
 *
 * @return array
 */
function block_email_cf_validator_fields(){
    return array(
        array(
            'id' => 'client_email',
            'type' => 'email',
            'required' => true,
            'magic' => true,
            'label' => __( 'Magic tag for field to validate.', 'client_email' )
        ),
    );
}

2. config.php

<?php
echo Caldera_Forms_Processor_UI::config_fields( block_email_cf_validator_fields() );

III/ Plugin Download

Download Plugin and activate: Caldera Forms Email Validator v1.0

IV/ Adding Form Processor

How to Block Email Providers Caldera Forms 1

After downloading and activating the plugin, there is one more processor in your form processors. Pick that processor and set magic tag to %client_email%. The processor will check that field as validating.

How to Block Email Providers Caldera Forms 2

That’s it! Good luck! 🙂

Credit:

GiangLe.DEV

My name is Giang, a Vietnamese guy living in Thanh Hoa City, Vietnam. I am a WordPress Developer, learning to master WordPress and Laravel PHP Framework.

Leave a Reply

avatar
  Subscribe  
Notify of
Close Menu