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.
// Don't show "Edit Entry" title on the Edit Entry form.
add_filter( 'gravityview_edit_entry_title', '__return_empty_string' );
/**
* Display a Gravity Forms form when no entry exists for [gventry] shortcode.
*
* @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 show form if there's no entry and the feature is enabled.
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 Gravity Forms form (same form attached to the View)
$form_output = 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, // field_values - set to an array of field values to pre-populate the form.
false, // Set to true to use AJAX to submit the form.
0, // Set the starting `tabindex` for the form.
false // Do not modify.
);
return $form_output;
}, 10, 4 );
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" secret="example1234"]
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_form
attribute 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)