Display a Gravity Forms form when no entry exists with [gventry]
When using the [gventry] shortcode to display a single entry, you may want to show a Gravity Forms form when the entry doesn’t exist. This is useful for allowing visitors to create an entry when one isn’t available.
The form displayed will be the same form attached to the View, making it seamless for users to submit new entries.
This guide shows you how to add a simple code snippet that enables this functionality.
How it will work: #
- When an entry exists for the View, the [gventry] shortcode will function as usual.
- When there is no entry visible for a View, the primary form connected to the View will be shown.
Before you begin #
Make sure you have:
- A View with a Gravity Forms form attached as the data source
- Access to add code snippets to your WordPress site (via theme functions or a code snippets plugin)
Add the code snippet #
Add the following code to your site. Not sure how? Read this doc.
// Blank the "Edit Entry" heading. A space, not an empty string.
add_filter( 'gk/gravityview/edit-entry/title', function () {
return ' ';
} );
/**
* Show a Gravity Forms form when no entry exists for [gventry].
*
* @param string $output The shortcode output.
* @param \GV\View|null $view The View object or null.
* @param \GV\Entry|null $entry The Entry object or null.
* @param array $atts Shortcode attributes.
*
* @return string Modified output.
*/
add_filter(
'gravityview/shortcodes/gventry/output',
function ( $output, $view, $entry, $atts ) {
// Only act when there is no entry and the attribute is set.
if ( $entry || empty( $atts['show_form_if_empty'] ) ) {
return $output;
}
// Make sure we have a View with a form attached.
if ( ! $view || ! $view->form || ! $view->form->ID ) {
return $output;
}
// Display the form attached to the View.
return gravity_form(
$view->form->ID,
false, // Set to true to show the form title.
false, // Set to true to show the form description.
false, // Set to true to show inactive forms. Do not modify.
null, // Pass field values here to pre-populate the form.
false, // Set to true to submit the form over AJAX.
0, // The starting tabindex for the form.
false // Do not modify.
);
},
10,
4
);The first filter returns a single space rather than an empty string on purpose. GravityView falls back to the words “Edit Entry” whenever the title comes back empty, so an empty string puts the heading back. A space passes through and leaves the heading blank. If you would rather keep a heading, set one under Settings > Edit Entry > Edit Entry Title in the View editor and drop that filter entirely.
Use the shortcode #
After adding the code snippet, you can use the new functionality by adding the show_form_if_empty="1" attribute to your [gventry] shortcode.
Basic usage
Display the View’s form when no entry exists:
[gventry entry="123" show_form_if_empty="1"]In this example:
- The shortcode will try to display entry 123
- If entry 123 doesn’t exist, the form attached to the View will be displayed instead
- The form ID is determined automatically from the View’s data source
How it works #
When the [gventry] shortcode runs:
- It checks if an entry exists
- If the entry exists, it displays normally
- If no entry is found and
show_form_if_empty="1"is set, it automatically displays the Gravity Forms form attached to the View
The form appears exactly where the entry would normally display, with all standard Gravity Forms styling and functionality. Since the form is the same one used as the View’s data source, new submissions will automatically appear in the View (subject to any View filters).
Common use cases #
This functionality is useful for:
- User profile forms: Display an existing profile entry or show a form to create one
- Application submissions: Show submitted applications or allow new submissions
- Registration systems: Display registration details or provide a registration form
- Member directories: Show member profiles or allow new member signups
Troubleshooting #
The form doesn’t appear
Make sure:
- The code snippet has been added and saved
- The
entry_formattribute contains a valid form ID - The form is active in Gravity Forms
- There is no entry matching the specified entry ID
The form appears even when an entry exists
Check that:
- The entry ID in the shortcode is correct
- The entry hasn’t been deleted or moved to the trash
- You’re using the correct View context (if applicable)