Using Gravity Forms ‘gform_shortcode_$action’ PHP filter

The gform_shortcode_ACTION filter in Gravity Forms is used to implement custom shortcode actions.

Usage

To use the filter, specify the action name after the gform_shortcode_ hook name:

add_filter('gform_shortcode_form_property', 'custom_action', 10, 3);

Parameters

  • $shortcode_string (string): The full shortcode string.
  • $attributes (array): Array of the shortcode attributes.
  • $content (string): The content text of the shortcode if it exists.

More information

See Gravity Forms Docs: gform_shortcode_ACTION

Placement: This code may be placed in the functions.php file of your active theme OR in your plugin code.

Source Code: This filter is located in GFForms::parse_shortcode() in gravityforms.php.

Examples

The examples below assume the following shortcode setup on a page:

[gravityform id="22" name="Poll" action="form_property" property="title"]
my content
[/gravityform]

Adding a custom action

This example demonstrates how to use the gform_shortcode_form_property filter to display the title of the form:

add_filter('gform_shortcode_form_property', 'custom_action', 10, 3);

function custom_action($string, $attributes, $content) {
    // Extract the shortcode attributes into variables
    extract(shortcode_atts(array(
        'title' => true,
        'description' => true,
        'id' => 0,
        'name' => '*',
        'field_values' => "",
        'ajax' => false,
        'tabindex' => 1,
        'action' => 'form',
        'property' => '*'
    ), $attributes));

    // Get the form object
    $form = RGFormsModel::get_form_meta($id);

    // Retrieve the "property" from the form object
    $property_value = $form[$property];

    $info = "The property to retrieve is {$property} with value {$property_value}.";
    return $info;
}

Output hidden comment for inactive forms

This example shows how to include an HTML comment in the page source code when a form is set to inactive:

add_filter('gform_shortcode_form', function ($shortcode_string, $attributes) {
    if (empty($shortcode_string)) {
        $shortcode_string = sprintf('<!--Form (%s) is inactive.-->', rgar($attributes, 'name', rgar($attributes, 'id')));
    }
    return $shortcode_string;
}, 10, 2);