Design System is here - Read the release post

Learn GeneratePress

Developers

Registering form actions (Pro)

A form action is what a Form block does with a submission once it has been sanitized and validated, such as sending an email or posting to a webhook. GenerateBlocks Pro registers four actions: email, email-signup, webhook and confirmation-email. You can register your own with GenerateBlocks_Pro_Form_Action_Registry.

For the hooks around form processing, see Forms: processing and validation. To add an email marketing service to the form editor, see Registering form integrations.

Jump to

The registry

GB Pro — since GenerateBlocks 2.6.

Every method of GenerateBlocks_Pro_Form_Action_Registry is static.

MethodWhat it does
register( $name, $callback )Registers an action. $name is passed through sanitize_key(). The action is only registered if $callback is callable. Registering a name that already exists replaces it.
get( $name )Returns the callback for an action, or null.
unregister( $name )Removes an action.

Register your action while WordPress loads, after GenerateBlocks Pro is active, for example on init.

A form’s configuration, which is stored with the form, has an actions list of action names. A new form starts with [ 'email' ]. The names are passed through sanitize_key(), and the form runs them in order. A name with no registered callback counts as a failure and the submission is stored as failed. confirmation-email is the one action that is non-blocking: if it is the only action that fails, the submission still succeeds.

The form editor only offers the built-in actions. A registered action runs when its name is in the form’s actions list, so you have to add the name to the form’s saved configuration yourself.

Related: generateblocks_form_after_process Guide: Forms

Back to top

The action callback

GB Pro — since GenerateBlocks 2.6.

GenerateBlocks calls your callback with three arguments:

ArgumentDescription
$sanitized_dataArray of the sanitized fields. Each entry has the field’s value.
$form_settingsArray of the form’s settings.
$contextArray with the request context, such as post_id and page_url.

Return true when the action worked. Return a WP_Error when it didn’t. If an action returns a WP_Error, the submission is stored as failed and the visitor sees a generic error. To show your own message, add public_message to the error data.

Related: generateblocks_form_validate_submission Guide: Forms

Back to top

Example: log submissions

GB Pro

This action writes the submitted field values to the PHP error log.

add_action( 'init', function() {
    if ( ! class_exists( 'GenerateBlocks_Pro_Form_Action_Registry' ) ) {
        return;
    }

    GenerateBlocks_Pro_Form_Action_Registry::register(
        'log-submission',
        function( $sanitized_data, $form_settings, $context ) {
            foreach ( $sanitized_data as $name => $field ) {
                error_log( $name . ': ' . ( $field['value'] ?? '' ) );
            }

            return true;
        }
    );
} );

Related: Registering form integrations Guide: Forms

Back to top