Advanced Form Modification & Creation

Caldera Forms Banner - A different kind of WordPress form builder.

Caldera Forms thrives to be a tool that is as useful to non-programmers and programers a like. In version 1.2 we added two new filters that radically expand its usefulness to PHP developers. These two new filters are:

  • caldera_forms_get_form
  • caldera_forms_get_form-{form_name}

These two filters can be used to modify a form’s settings before it is outputted or create an entirely new form not present in the database, created entirely in PHP. The ability to create forms using PHP code is not for everyone, but for advanced developers, it is a great way to make editing forms easy, and ease the process of moving between local, staging and production environments.

Modify A Form Before Output

You can use the caldera_forms_get_form filter to modify a form before it is outputted. The example below shows how to remove a processor if a user does not have a certain capability.

Since the full form settings are exposed by this filter, you can modify any part of the form, including fields, processors, and form settings.

<?php
add_filter( 'caldera_forms_get_form', function( $form, $id  )  {
    if ( 'YOUR_FORM_ID' == $id && isset( $form[ 'processors' ] ) && ! current_user_can( 'edit_posts' ) ) {
        $geo = array_search( 'geo_location_search', array_column( $form[ 'processors' ], 'type' ) );
        $indexes = array_keys( $form[ 'processors' ]  );
        $key = $indexes[ $geo ];
        unset( $form[ 'processors' ][ $key ] );
    }
    
    return $form;

}, 10, 2);
  • Note: This filter was added in Caldera Forms 1.2.0
  • Note: The function array_column() was added in PHP 5.5. In old versions of PHP, you will need to use a foreach loop.

Create A Form With PHP

Using the caldera_forms_get_form-{form-name} filter, added in Caldera Forms 1.2, you can now define an entire form using PHP.

This example below shows one function to create an entirely new form, not saved in the database, and then output it. Using this method can allow advanced manipulation of the form settings and what processors are used via PHP.

<?php
function slug_contact_form() {

    add_action( 'caldera_forms_get_form-contact', function( $form )  {
        $form = array (
            'ID' => 'contact',
            'name' => 'Contact',
            'description' => '',
            'db_support' => 1,
            'pinned' => 0,
            'hide_form' => 1,
            'check_honey' => 1,
            'success' => 'Form has been successfully submitted. Thank you.',
            'form_ajax' => 1,
            'custom_callback' => '',
            'layout_grid' => array (
                'fields' => array (
                    'name' => '1:1',
                    'email' => '1:2',
                    'message' => '2:1',
                    'send' => '3:1',
                ),
                'structure' => '6:6|12|12',
            ),
            'fields' => array (
                'send' => array (
                    'ID' => 'send',
                    'type' => 'button',
                    'label' => 'Send',
                    'slug' => 'send',
                    'caption' => '',
                    'config' => array (
                        'custom_class' => '',
                        'type' => 'submit',
                        'class' => 'btn btn-default',
                    ),
                    'conditions' => array (
                        'type' => '',
                    ),
                ),
                'email' => array (
                    'ID' => 'email',
                    'type' => 'email',
                    'label' => 'Email',
                    'slug' => 'email',
                    'caption' => '',
                    'required' => 1,
                    'config' => array (
                        'custom_class' => '',
                        'placeholder' => '',
                        'default' => '',
                    ),
                    'conditions' => array (
                        'type' => '',
                    ),
                ),
                'name' => array (
                    'ID' => 'name',
                    'type' => 'text',
                    'label' => 'Name',
                    'slug' => 'name',
                    'required' => 1,
                    'caption' => '',
                    'config' => array (
                        'custom_class' => '',
                        'placeholder' => '',
                        'default' => '',
                        'mask' => '',
                    ),
                    'conditions' => array (
                        'type' => '',
                    ),
                ),
                'message' => array (
                    'ID' => 'message',
                    'type' => 'paragraph',
                    'label' => 'Message',
                    'slug' => 'message',
                    'required' => 1,
                    'caption' => '',
                    'config' => array (
                        'custom_class' => '',
                        'placeholder' => '',
                        'rows' => 4,
                        'default' => '',
                    ),
                    'conditions' => array (
                        'type' => '',
                    ),
                ),
            ),
            'page_names' => array (
                0 => 'Page 1',
            ),
            'settings' => array (
                'responsive' => array (
                    'break_point' => 'sm',
                ),
            ),
            'mailer' => array (
                'enable_mailer' => 1,
                'sender_name' => 'Caldera Forms Notification',
                'sender_email' => 'josh@calderawp.com',
                'reply_to' => 'josh@calderawp.com',
                'email_type' => 'html',
                'recipients' => '%email%',
                'bcc_to' => '',
                'email_subject' => 'New message from %name%',
                'email_message' => '{summary}',
            ),
        );


        return $form;

    });

    return Caldera_Forms::render_form( 'contact' );
    
}
  • Note: This filter was added in Caldera Forms 1.2.0
  • This is an overly-simplistic, but functional example. In reality, an object-oriented approach would be recommended.