gform_display_add_form_button

Description

By default, the “Add Form” button and legacy shortcode UI will only be available on the Post or Page edit screens using the classic editor. The gform_display_add_form_button filter can be used to enable it for other pages or scenarios.

Note: The “Add Form” button requires a TinyMCE input, so in order to display it, you must first include a TinyMCE input to your page.

Usage

add_filter( 'gform_display_add_form_button', 'display_form_button_on_custom_page' );

Parameters

  • $display_add_form_button bool
    Indicates if the Add Form button and shortcode UI should be included for the current admin page.
$display_add_form_button = ( ! class_exists( 'GF_Block' ) || class_exists( 'Classic_Editor' ) ) && in_array( RG_CURRENT_PAGE, array(
	'post.php',
	'page.php',
	'page-new.php',
	'post-new.php',
	'customize.php',
) );

Examples

Generic Examples

Enable based on query string

This example enables the “Add Form” button on a custom page whose URL contains the following query string: “page=my_page”.

add_filter( 'gform_display_add_form_button', 'display_form_button_on_custom_page' );
function display_form_button_on_custom_page( $display_add_form_button ) {
    if ( isset( $_GET['page'] ) && $_GET['page'] == 'my_page' ) {
        return true;
    }

    return $display_add_form_button;
}

Disable everywhere

Simple example, to disable the “Add Form” button everywhere.

add_filter( 'gform_display_add_form_button', '__return_false' );

Enable everywhere

Simple example, to enable the “Add Form” button everywhere.

add_filter( 'gform_display_add_form_button', '__return_true' );

Enable based on WP_Screen base

This example enables the “Add Form” button if the page is not a post page.

add_filter( 'gform_display_add_form_button', function ( $display_add_form_button ) {
    global $current_screen;

    return $current_screen instanceof WP_Screen && $current_screen->base != 'post' ? true : $display_add_form_button;
} );

Theme Examples

Enable for Divi

This example enables the “Add Form” button for the page and post editor when the Divi theme is active.

add_filter( 'gform_display_add_form_button', function ( $display_add_form_button ) {
	if ( $display_add_form_button || ! class_exists( 'ET_Builder_Plugin_Compat_Gravityforms' ) ) {
		return $display_add_form_button;
	}

	$enable_classic_editor = apply_filters( 'et_builder_enable_classic_editor', isset( $_GET['et_classic_editor'] ) );

	return $enable_classic_editor && in_array( RG_CURRENT_PAGE, array(
		'post.php',
		'page.php',
		'page-new.php',
		'post-new.php',
	) );
} );

Enable for Enfold

This example enables the “Add Form” button for some Content Elements in the Advanced Layout Editor of the Enfold theme.

add_filter( 'gform_display_add_form_button', function ( $display_add_form_button ) {
	if (
		$display_add_form_button || in_array( RG_CURRENT_PAGE, array(
			'post.php',
			'page.php',
			'page-new.php',
			'post-new.php',
		) )
	) {
		return true;
	}

	if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || rgpost( 'ajax_fetch' ) !== 'true' ) {
		return false;
	}

	$supported_actions = array(
		'avia_ajax_av_textblock',
		'avia_ajax_av_toggle',
		'avia_ajax_av_tab',
		'avia_ajax_av_promobox',
	);

	return in_array( rgpost( 'action' ), $supported_actions );
} );

Plugin Examples

Enable for Advanced Custom Fields

This example enables the “Add Form” button for the page and post editor when the ACF plugin is active.

add_filter( 'gform_display_add_form_button', function ( $display_add_form_button ) {
	if ( $display_add_form_button || ! function_exists( 'acf' ) ) {
		return $display_add_form_button;
	}

	return in_array( RG_CURRENT_PAGE, array(
		'post.php',
		'page.php',
		'page-new.php',
		'post-new.php',
	) );
} );

Enable for The Events Calendar

This example enables the “Add Form” button for the post editor when The Events Calendar plugin is active.

add_filter( 'gform_display_add_form_button', function ( $display_add_form_button ) {
	if ( $display_add_form_button || ! class_exists( 'Tribe__Events__Main' ) ) {
		return $display_add_form_button;
	}

	return in_array( RG_CURRENT_PAGE, array(
		'post.php',
		'post-new.php',
	) );
} );

Enable for multiple plugins

This example enables the “Add Form” button for the page/post editor when the ACF or The Events Calendar plugins are active.

add_filter( 'gform_display_add_form_button', function ( $display_add_form_button ) {
	if ( $display_add_form_button ) {
		return $display_add_form_button;
	}

	$class_exists = false;
	$classes      = array(
		'ACF',
		'Tribe__Events__Main',
	);

	foreach ( $classes as $class ) {
		if ( class_exists( $class ) ) {
			$class_exists = true;
			break;
		}
	}

	return $class_exists && in_array( RG_CURRENT_PAGE, array(
		'post.php',
		'page.php',
		'page-new.php',
		'post-new.php',
	) );
} );

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::page_supports_add_form_button() in gravityforms.php.