How to Display the Total Number of Entries

Introduction

Do you want to showcase the total number of entries for your forms? You can easily achieve this by creating a custom shortcode with a small PHP snippet. In this tutorial, we’ll guide you on how to set it up.

It’s important to note that real-time updates for the counts may experience delays due to any caching configurations on your site or server.

Creating the form

Now, let’s move on to the next step. In our example, we aim to display the newsletter form along with the count of users who have signed up right underneath it.

If you’re unfamiliar with the form creation process, you can find detailed instructions in this article.

create your form and add your fields

Adding the code snippet to your site

After copying the provided code snippet, you can easily add it to your site to enable the shortcode. If you’re unsure about adding snippets, you can find guidance in 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' );

Let’s delve into the code explanation. The shortcode attributes are set using $atts = shortcode_atts(['id' => '','type' => 'all', ],.

The id attribute is crucial as it expects the form ID. This is how the shortcode determines which form it should count the entries for. If you’re unsure about your form ID, you can refer to this tutorial for guidance.

The second attribute, type, is optional. By default, the shortcode counts all entries and displays the total number. If you don’t include this attribute, it automatically includes every entry for the form. However, if you want to specify which type of entry to count and display the total for, you can use one of the following attributes:

  • all: Displays all entries for the form.
  • unread: Counts and displays only the entries that haven’t been viewed from your Entries screen.
  • read: Counts and displays only the entries that have been viewed from your Entries screen.
  • starred: Counts and displays only the entries that have been starred from your Entries screen.

Using the shortcode

In our demonstration, we’ve already crafted a basic newsletter subscription form and integrated it into a WordPress page.

Add a form to your page

Prior to incorporating the shortcode into our page, we aim to provide some context to our visitors regarding the significance of the number they’ll encounter. Therefore, we’ll include explanatory text before the shortcode.

Before we add the shortcode we're going to add some text so our users know what this number is for

Now, let’s integrate our shortcode into the page. Utilizing a Shortcode block, we’ve incorporated our custom shortcode to exhibit the total number of entries for our newsletter form (form ID 25). The shortcode is: [wpf_entries_count id="25" type="all"].

Add the shortcode to your page to display the total number of entries for this form

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.

Now you can display the total number of entries for each form you have using a shortcode

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.