Description
The “gform_shortcode_$action” filter in Gravity Forms is used to implement custom 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
- $shortcode_string string
The full shortcode string.
-
$attributes array
Array of the shortcode attributes.
array ( 'id' => '22', 'name' => 'Poll', 'action' => 'form_property', 'property' => 'title' ); -
$content string
The content text of the shortcode if it exists.
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 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.