gform_shortcode_ACTION

Description

The gform_shortcode_$action filter in Gravity Forms is used to implement custom shortcode actions or filter output of existing shortcode actions. The action is specified after gform_shortcode_.

Usage

Specify the action name after the gform_shortcode_ hook name, e.g.:

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

Parameters

ParameterTypeDescription
$shortcode_stringstringThe full shortcode string.
$attributesarrayArray of the shortcode attributes.
$contentstringThe content text of the shortcode if it exists.

The $attributes array contains the shortcode attributes, for example:

array (
	'id'       => '22',
	'name'     => 'Poll',
	'action'   => 'form_property',
	'property' => 'title'
);

Examples

Adding a custom action

The example below has the following shortcode setup on a page:

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

…and uses the following hook setup with the action “form_property”. The “property” is retrieved and used to display the title of the form.

add_filter( 'gform_shortcode_form_property', 'custom_action', 10, 3 );
function custom_action( $string, $attributes, $content ) {
	//get the shortcode attributes into variables, default values set for some
	//the "property" attribute set in the shortcode has been added to the shortcode array
	extract( shortcode_atts( array(
		'title'        => true,
		'description'  => true,
		'id'           => 0,
		'name'         => *,
		'field_values' => "",
		'ajax'         => false,
		'tabindex'     => 1,
		'action'       => 'form',
		'property'     => *
	), $attributes ) );
	$form = RGFormsModel::get_form_meta( $id ); //get the form object
	$property_value = $form[ $property ]; //retrieve the "property" from the form object
	$info = "The property to retrieve is {$property} with value {$property_value}.";
	return $info;
}

Output hidden comment for inactive forms

This example shows how a HTML comment can be included 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 );

Placement

This code can be used in the functions.php file of the active theme, a custom functions plugin, a custom add-on, or with a code snippets plugin.

See also the PHP section in this article: Where Do I Put This Code?

Source Code

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