Skip to content

Instantly share code, notes, and snippets.

@jaredatch
Last active October 16, 2023 11:51
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaredatch/934515afe3a047559e9d092923a9320c to your computer and use it in GitHub Desktop.
Save jaredatch/934515afe3a047559e9d092923a9320c to your computer and use it in GitHub Desktop.
WPForms display form entries with a custom shortcode
<?php
/**
* Custom shortcode to display WPForms form entries.
*
* Basic usage: [wpforms_entries_table id="FORMID"].
*
* Possible shortcode attributes:
* id (required) Form ID of which to show entries.
* user User ID, or "current" to default to current logged in user.
* fields Comma seperated list of form field IDs.
* number Number of entries to show, defaults to 30.
*
* @link https://wpforms.com/developers/how-to-display-form-entries/
*
* @param array $atts Shortcode attributes.
*
* @return string
*/
function wpf_entries_table( $atts ) {
// Pull ID shortcode attributes.
$atts = shortcode_atts(
[
'id' => '',
'user' => '',
'fields' => '',
'number' => '',
],
$atts
);
// Check for an ID attribute (required) and that WPForms is in fact
// installed and activated.
if ( empty( $atts['id'] ) || ! function_exists( 'wpforms' ) ) {
return;
}
// Get the form, from the ID provided in the shortcode.
$form = wpforms()->form->get( absint( $atts['id'] ) );
// If the form doesn't exists, abort.
if ( empty( $form ) ) {
return;
}
// Pull and format the form data out of the form object.
$form_data = ! empty( $form->post_content ) ? wpforms_decode( $form->post_content ) : '';
// Check to see if we are showing all allowed fields, or only specific ones.
$form_field_ids = isset( $atts['fields'] ) && $atts['fields'] !== '' ? explode( ',', str_replace( ' ', '', $atts['fields'] ) ) : [];
// Setup the form fields.
if ( empty( $form_field_ids ) ) {
$form_fields = $form_data['fields'];
} else {
$form_fields = [];
foreach ( $form_field_ids as $field_id ) {
if ( isset( $form_data['fields'][ $field_id ] ) ) {
$form_fields[ $field_id ] = $form_data['fields'][ $field_id ];
}
}
}
if ( empty( $form_fields ) ) {
return;
}
// Here we define what the types of form fields we do NOT want to include,
// instead they should be ignored entirely.
$form_fields_disallow = apply_filters( 'wpforms_frontend_entries_table_disallow', [ 'divider', 'html', 'pagebreak', 'captcha' ] );
// Loop through all form fields and remove any field types not allowed.
foreach ( $form_fields as $field_id => $form_field ) {
if ( in_array( $form_field['type'], $form_fields_disallow, true ) ) {
unset( $form_fields[ $field_id ] );
}
}
$entries_args = [
'form_id' => absint( $atts['id'] ),
];
// Narrow entries by user if user_id shortcode attribute was used.
if ( ! empty( $atts['user'] ) ) {
if ( $atts['user'] === 'current' && is_user_logged_in() ) {
$entries_args['user_id'] = get_current_user_id();
} else {
$entries_args['user_id'] = absint( $atts['user'] );
}
}
// Number of entries to show. If empty, defaults to 30.
if ( ! empty( $atts['number'] ) ) {
$entries_args['number'] = absint( $atts['number'] );
}
// Get all entries for the form, according to arguments defined.
// There are many options available to query entries. To see more, check out
// the get_entries() function inside class-entry.php (https://a.cl.ly/bLuGnkGx).
$entries = wpforms()->entry->get_entries( $entries_args );
if ( empty( $entries ) ) {
return '<p>No entries found.</p>';
}
ob_start();
echo '<table class="wpforms-frontend-entries">';
echo '<thead><tr>';
// Loop through the form data so we can output form field names in
// the table header.
foreach ( $form_fields as $form_field ) {
// Output the form field name/label.
echo '<th>';
echo esc_html( sanitize_text_field( $form_field['label'] ) );
echo '</th>';
}
echo '</tr></thead>';
echo '<tbody>';
// Now, loop through all the form entries.
foreach ( $entries as $entry ) {
echo '<tr>';
// Entry field values are in JSON, so we need to decode.
$entry_fields = json_decode( $entry->fields, true );
foreach ( $form_fields as $form_field ) {
echo '<td>';
foreach ( $entry_fields as $entry_field ) {
if ( absint( $entry_field['id'] ) === absint( $form_field['id'] ) ) {
echo apply_filters( 'wpforms_html_field_value', wp_strip_all_tags( $entry_field['value'] ), $entry_field, $form_data, 'entry-frontend-table' );
break;
}
}
echo '</td>';
}
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
$output = ob_get_clean();
return $output;
}
add_shortcode( 'wpforms_entries_table', 'wpf_entries_table' );
@gene158
Copy link

gene158 commented Jun 19, 2019

Does anyone know how to modify this code to only show entries for the currently logged in user? The form entries would contain the email address which could be used to compare against the currently logged in user's email. Any help would be much appreciated, thanks!

@aloescobar
Copy link

Does anyone now how to twitch this code to show only the last entry in the form? It's a way of setting up a confirmation page.

@omBew0k
Copy link

omBew0k commented Jan 31, 2020

hallo friend, This plugin really helped me in building the web. is it possible to display data only input by the user? because I use to make a support ticket, so the user can see the status of the ticket inputted.

@jaredatch
Copy link
Author

I've updated the snippet substantially, which hopefully should help those who find this :)

First, I've rewritten and restructured everything to be more readable, and added comments everywhere to provide context of what is going on for those who may what to customize the code.

Secondly, I added a user shortcode attribute, which can be used to narrow entries down to a specific user ID or the currently logged in user.

Lastly, I added a fields shortcode attribute, which allows total control over what form fields are displayed in the table and in what order.

Hope that helps :)

@RicoYahoo
Copy link

How to properly install this code? Do I place it in my PHP snippet plugin ('XYX PHP Code') then in my page insert this...?
[wpforms id="1198"] //display the form
[xyz-ips snippet="createshortcode"] //create the shortcode
[wpforms_entries_table] //execute the shortcode

I am currently getting the [wpforms_entries_table] displayed as text and not the functionality that it provides.
Ideas?

@jaredatch
Copy link
Author

@RicoYahoo - there are a few different approaches available, depending on what you are comfortable with.

Check out https://www.wpbeginner.com/plugins/how-to-easily-add-custom-code-in-wordpress-without-breaking-your-site/

@Tarvo0
Copy link

Tarvo0 commented Mar 10, 2020

Thank you for the code, it is very helpful. Is it possible to display somehow {entry_id} as well?

@nine2uk
Copy link

nine2uk commented Apr 20, 2020

Great snippet! - Is there a way to display entry_id in the table of entries?

@dragonwebsite
Copy link

I'm very new at this and would like to get the Entries going down the page not across.
I've altered the table display part of the code and got the 'Field Label' running down the page but have lost the entered values.
I can't seem to figure out how to get the whole display to come up.
Any advice very much appreciated.

@Elagrody20
Copy link

How to display the date of each entry?

Thanks in advance!

@Rocco23
Copy link

Rocco23 commented Feb 27, 2021

I pasted the php code using snippets. If I understand correctly, wpforms should create a new form.
Unfortunately, for me this form does not appear in the list ....

What have I done wrong?

@ttattini
Copy link

Can you please consider adding an "edit" option?

@shrikant2311
Copy link

Is there a way to display entry_id in the table of entries? please tell its urgent

@ttattini
Copy link

ttattini commented Jul 7, 2021

Is there a way to display entry_id in the table of entries? please tell its urgent

https://wpforms.com/developers/how-to-create-a-unique-id-for-each-form-entry/

@shrikant2311
Copy link

shrikant2311 commented Jul 16, 2021

Is there a way to display entry_id in the table of entries? please tell its urgent

https://wpforms.com/developers/how-to-create-a-unique-id-for-each-form-entry/

I want to display my entry_id along with the whole data that are being filled in the form. How can I display that please tell?

@shrikant2311
Copy link

I want to display my entry_id along with the whole data that are being filled in the form. How can I display that please tell?

please its urgent

@ms7br
Copy link

ms7br commented Sep 7, 2021

Hello, Can you help me guys I need to hide the feilds if it's empty

for example if I'm displaying feilds 1,2,3,4 and in second entry feild 3 is emplty I want to be hide from front end

@zuriknet
Copy link

Thank you so much for this. I can now sort by entry_id, show the most recent entries, display or hide previously viewed entries. This is perfect for what I am working on!

@Pandagan-85
Copy link

Great snippet! - Is there a way to display entry_id in the table of entries?

Hi! did you find a solution to display entry_id?

@Nehaljeloka
Copy link

where to change what in this code. It is giving me a headache. Can someone please answer it??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment