How to Display Form Entries

Overview

Would you like to display the form entries on your site so that your visitors can easily see them? WPForms is set up with security in mind, so by default, only WordPress admin users will be able to see entry data. This tutorial will walk you through how to use PHP to create a shortcode to display your form entries on a page for non-admin users.

Setup

This code doesn’t require any changes, and is ready to be added to your site.

1) Creating the shortcode

First, you’ll need to create the shortcode so that it can be used on any page, post or widget area on your site.

/**
 * Custom shortcode to display form entries in a table.
 *
 * Usage [wpforms_entries_table id="FORMID"]
 *
 * @link https://wpforms.com/developers/how-to-display-form-entries/
 *
 * @param array $atts
 */
function wpf_dev_entries_table( $atts ) {
	$atts = shortcode_atts( array(
		'id' => ''
	), $atts );
	if ( empty( $atts['id'] ) || !function_exists( 'wpforms' ) ) {
		return;
	}
	$form = wpforms()->form->get( absint( $atts['id'] ) );
	if ( empty( $form ) ) {
		return;
	}
	$form_data = !empty( $form->post_content ) ? wpforms_decode( $form->post_content ) : '';
	$entries   = wpforms()->entry->get_entries( array( 'form_id' => absint( $atts['id'] ), 'number' => -1 ) );
	$disallow  = apply_filters( 'wpforms_frontend_entries_table_disallow', array( 'divider', 'html', 'pagebreak', 'captcha' ) );
	$ids       = array();
	ob_start();
	echo '<table class="wpforms-frontend-entries">';
		echo '<thead><tr>';
			foreach( $form_data['fields'] as $field ) {
				if ( !in_array( $field['type'], $disallow ) ) {
					$ids[] = $field['id'];
					echo '<th>' . sanitize_text_field( $field['label'] ) . '</th>';
				}
			}
		echo '</tr></thead>';
		echo '<tbody>';
			foreach( $entries as $entry ) {
				echo '<tr>';
				$fields = wpforms_decode( $entry->fields );
				foreach( $fields as $field ) {
					if ( in_array( $field['id'], $ids ) ) {
						echo '<td>' . apply_filters( 'wpforms_html_field_value', wp_strip_all_tags( $field['value'] ), $field, $form_data, 'entry-frontend-table' ) . '</td>';
					}
				}
				echo '</tr>';
			}
		echo '</tbody>';
	echo '</table>';
	$output = ob_get_clean();
	return $output;
}
add_shortcode( 'wpforms_entries_table', 'wpf_dev_entries_table' );

2) Using the shortcode

To display entries in a page or post, you’ll need to use the following shortcode, where FORMID is replaced with the form’s ID number:

[wpforms_entries_table id="FORMID"]

A form’s ID number can be found by going to WPForms » All Forms and looking to the number in the shortcode for each form.

How to locate your form ID

For this example, we’ll modify the shortcode to use the form with an ID of 51:

[wpforms_entries_table id="51"]

The styling of your entry table will vary depending on the default styling your theme may or may not have for tables.

And that’s it, you’ve now created a shortcode that can be added in various places on your site so that you can display your form entries to non-admin users. Would you also like to display how many entries are left on your form when using the Form Locker addon? Try out our code snippet for How to Display Remaining Entry Limit Number.

FAQ

Q: Is there any way to make the table look better?

A: By default, the styling will come from your theme. You can add some additional CSS if you’d like to make the form look a little nicer.

table {
    border-collapse: collapse;
}

thead tr {
    height: 60px;
}

table, th, td {
    border: 1px solid #000000;
}

td {
    white-space: normal;
    max-width: 33%;
    width: 33%;
    word-break: break-all;
    height: 60px;
    padding: 10px;
}
tr:nth-child(even) {
    background: #ccc
}

tr:nth-child(odd) {
    background: #fff
}

Add stying to your form entries table

Q: What if I only wanted to show starred entries?

A: You can easily achieve this by using the following PHP snippet.

/**
 * Custom shortcode to display form entries in a table.
 *
 * Usage [wpforms_entries_table id="FORMID"]
 *
 * @param array $atts
 */
function wpf_dev_entries_table( $atts ) {
	$atts = shortcode_atts( array(
		'id' => ''
	), $atts );
	if ( empty( $atts['id'] ) || !function_exists( 'wpforms' ) ) {
		return;
	}
	$form = wpforms()->form->get( absint( $atts['id'] ) );
	if ( empty( $form ) ) {
		return;
	}
	$form_data = !empty( $form->post_content ) ? wpforms_decode( $form->post_content ) : '';
	$entries   = wpforms()->entry->get_entries( 
		array( 
			'form_id' => absint( $atts['id'] ), 
			'number'  => -1,
			'starred' => 1,
		)
	);
	$disallow  = apply_filters( 'wpforms_frontend_entries_table_disallow', array( 'divider', 'html', 'pagebreak', 'captcha' ) );
	$ids       = array();
	ob_start();
	echo '<table class="wpforms-frontend-entries">';
		echo '<thead><tr>';
			foreach( $form_data['fields'] as $field ) {
				if ( !in_array( $field['type'], $disallow ) ) {
					$ids[] = $field['id'];
					echo '<th>' . sanitize_text_field( $field['label'] ) . '</th>';
				}
			}
		echo '</tr></thead>';
		echo '<tbody>';
			foreach( $entries as $entry ) {
				echo '<tr>';
				$fields = wpforms_decode( $entry->fields );
				foreach( $fields as $field ) {
					if ( in_array( $field['id'], $ids ) ) {
						echo '<td>' . apply_filters( 'wpforms_html_field_value', wp_strip_all_tags( $field['value'] ), $field, $form_data, 'entry-frontend-table' );
					}
				}
				echo '</tr>';
			}
		echo '</tbody>';
	echo '</table>';
	$output = ob_get_clean();
	return $output;
}
add_shortcode( 'wpforms_entries_table', 'wpf_dev_entries_table' );