Introduction
Would you like to display the total number of entries for your forms? When you add a form to your page, you can, with a small PHP snippet, create a custom shortcode that would show the total number of submissions for this form as well. In this tutorial, we’ll walk you through how to do this.
For our example, we want to show our newsletter form and directly under the form, we also want to show how many of our users have signed up for this newsletter as well.
Adding the code snippet to your site
In order to create this custom shortcode, first, need to create it.
Start by simply copying and pasting the code snippet to your site so that you can use the shortcode in the next step.
To learn how to add snippets like this to your site, please review this tutorial.
/** * Custom shortcode to display the total number of entries for a form. * * Basic usage: [wpf_entries_count id="FORMID" type="TYPE"] * * Realtime counts could be delayed due to any caching setup on the site * * @link https://wpforms.com/developers/how-to-display-the-total-number-of-entries/ * */ function wpf_entries_count( $atts ) { // Pull ID shortcode attributes such as the type of entries to count $atts = shortcode_atts( [ 'id' => '', 'type' => 'all', // all, unread, read, or starred. ], $atts ); if ( empty( $atts['id'] ) ) { return; } $args = [ 'form_id' => absint( $atts['id'] ), ]; // What types of entries do you want to show? The read, unread, starred or all? if ( $atts['type'] === 'unread' ) { $args['viewed'] = '0'; } elseif( $atts['type'] === 'read' ) { $args['viewed'] = '1'; } elseif ( $atts['type'] === 'starred' ) { $args['starred'] = '1'; } return wpforms()->entry->get_entries( $args, true ); } add_shortcode( 'wpf_entries_count', 'wpf_entries_count' );
So let’s break down the code above, the
$atts = shortcode_atts(['id' => '','type' => 'all', ],
The id
is going to be expecting the form ID. This is the only way for the shortcode to know which form that it should be counting the entries for.
If you’re not sure what your form ID is, take a look at this tutorial.
The next attribute of the shortcode is type
. By default, using this shortcode will automatically count all
entries and display the total number of entries.
If you choose to not include this attribute at all when you using the shortcode, it will automatically include every entry for this form.
However, if you’d like to specify which type of entry you want to count and display the total for, the shortcode will accept one of the following attributes:
all
– this will display all the entries for the form.unread
– only the entries that haven’t been viewed yet from your Entries screen will be counted and displayed when using this attribute.read
– this attribute will only count and display the entries that have already been viewed from your Entries screen.starred
– and finally, this attribute will only count and display the entries that have been Starred from your Entries screen.
Using the shortcode
For our example, we’ve already created a simple newsletter sign up form that has been added to a page in WordPress.
Before we add the shortcode to our page, however, we want to make sure our visitors understand what this number will represent, so we’re going to add some text before the shortcode to explain what the number is that they are seeing.
Now it’s time to add our shortcode to the page. Using a Shortcode block, we’ve added our custom shortcode to display a total number of entries for our newsletter form (form ID 25). [wpf_entries_count id="25" type="all"]
However, if we wanted to have only the total number of entries shown for only the starred entries, our shortcode would’ve looked like this. [wpf_entries_count id="25" type="starred"]
And now when we visit the page, we can see the total number of entries below the form.
Please note that realtime updating to counts could be delayed due to any caching set up on the site or server.
And that’s it! You’ve successfully added a custom shortcode to your site that you can reuse as many times as you want to display the total number of entries for each form on your site!
Would you like to also create a custom shortcode to display all of your forms on the frontend of your site? Take a look at our tutorial on How to Display a List of WPForms Using a Shortcode.