aa

How to Display WordPress Form Entries on Your Site (2 Ways)

10 hours ago, WordPress Tutorials, Views
How to Display WordPress form entries on your site

Introduction: Showing Form Data to the World

WordPress forms are powerful tools for collecting information from your website visitors. Whether it’s contact details, survey responses, or application submissions, forms are essential for interaction and data gathering. But what happens after someone submits a form? Often, you need to display this collected data on your website, either for internal review, public access (with proper privacy considerations), or to confirm successful submissions to users. This article will guide you through two primary methods of displaying WordPress form entries directly on your site. We’ll cover using plugins specifically designed for this purpose, as well as a more advanced, code-based approach using custom post types and templates.

Method 1: Using WordPress Form Display Plugins

The easiest and often most efficient way to display form entries is by utilizing dedicated WordPress plugins. Numerous plugins are available that simplify the process, allowing you to showcase data without writing any code. Here’s a breakdown of the general process and some popular plugin options.

General Steps for Using a Form Display Plugin

Regardless of the specific plugin you choose, the general workflow typically involves these steps:

  • Install and activate the plugin.
  • Connect the plugin to your form plugin (e.g., Gravity Forms, Contact Form 7, WPForms). Most plugins will automatically detect installed form plugins.
  • Configure the display settings. This includes selecting which form entries to show, which fields to display, and how to format the output.
  • Embed the form entries on a page or post using a shortcode or block.

Popular Form Display Plugins

Here are a few well-regarded plugins that can help you display your form entries:

  • Formidable Forms: While primarily a form builder, Formidable Forms offers excellent features for displaying entries. It provides views, which allow you to create customized displays of your form data. These views can be inserted into pages, posts, or even custom templates. Features include conditional logic, dynamic content, and advanced filtering options.
  • GravityView: Specifically designed for Gravity Forms, GravityView is a powerful plugin for creating data tables, lists, and maps from your form entries. It offers a drag-and-drop interface for designing your entry displays and provides a wide range of customization options. You can control which fields are displayed, how they are formatted, and even allow users to edit their own entries.
  • Ninja Tables: This versatile table plugin can be used to display data from various sources, including form entries. While not specifically designed for form display, its flexibility and extensive table customization options make it a viable choice. You’ll likely need to manually export your form data to a CSV file and then import it into Ninja Tables.
  • Posts Table Pro: Although primarily used for creating sortable and searchable tables of WordPress posts, Posts Table Pro can be adapted to display custom post types (which we’ll discuss in Method 2). If you’re already using custom post types to store form entries, this plugin provides a powerful way to present that data in a user-friendly table format.

Example: Displaying Form Entries with Formidable Forms Views

Let’s look at a simplified example of how to use Formidable Forms to display form entries:

  1. Install and activate Formidable Forms.
  2. Create your form. Design the form with the fields you want to collect. For instance, a simple contact form might include fields like “Name,” “Email,” and “Message.”
  3. Create a View. In the Formidable Forms admin area, navigate to “Formidable” -> “Views” and click “Add New.”
  4. Select a View Template. Choose a template that suits your needs, such as a table, list, or single entry view.
  5. Configure the View. Map the form fields to the corresponding elements in the View template. This tells Formidable Forms which field data to display in which part of the View. You can customize the appearance and formatting of the displayed data.
  6. Embed the View. Formidable Forms will provide a shortcode for your View. Copy this shortcode and paste it into the page or post where you want to display the form entries.

Pros and Cons of Using Plugins

Pros:

  • Ease of use: Plugins typically offer user-friendly interfaces and require minimal coding.
  • Time-saving: They significantly reduce the development time compared to custom coding.
  • Feature-rich: Plugins often provide advanced features like filtering, sorting, pagination, and user editing.
  • Support and updates: Plugin developers usually provide support and updates to ensure compatibility and security.

Cons:

  • Cost: Many powerful plugins are premium and require a purchase.
  • Potential conflicts: Plugins can sometimes conflict with other plugins or themes.
  • Bloat: Some plugins can add unnecessary code and slow down your website.
  • Limited customization: While many plugins offer customization options, they might not always meet your specific requirements.

Method 2: Using Custom Post Types and Templates

For more advanced users or those requiring highly customized displays, creating a custom post type and template is a powerful option. This approach provides complete control over how form entries are stored and displayed. However, it requires coding knowledge and a good understanding of WordPress development.

Overview of the Custom Post Type Approach

The core idea is to:

  1. Create a custom post type to store your form entries. Think of this as creating a new type of content specifically for form submissions (e.g., “Form Submissions”).
  2. Programmatically create a new post of this custom post type whenever a form is submitted. The form data is then stored as custom fields (metadata) associated with that post.
  3. Create a custom template to display the form entries (i.e., the custom posts) in the desired format. This template will retrieve the data from the custom fields and present it on the page.

Step-by-Step Implementation

1. Create a Custom Post Type.

You can register a custom post type using the `register_post_type()` function in your theme’s `functions.php` file or a custom plugin.

“`php
function create_form_submission_post_type() {
$labels = array(
‘name’ => ‘Form Submissions’,
‘singular_name’ => ‘Form Submission’,
‘menu_name’ => ‘Form Submissions’,
‘name_admin_bar’ => ‘Form Submission’,
‘add_new’ => ‘Add New’,
‘add_new_item’ => ‘Add New Form Submission’,
‘new_item’ => ‘New Form Submission’,
‘edit_item’ => ‘Edit Form Submission’,
‘view_item’ => ‘View Form Submission’,
‘all_items’ => ‘All Form Submissions’,
‘search_items’ => ‘Search Form Submissions’,
‘parent_item_colon’ => ‘Parent Form Submissions:’,
‘not_found’ => ‘No form submissions found.’,
‘not_found_in_trash’ => ‘No form submissions found in Trash.’,
);

$args = array(
‘labels’ => $labels,
‘public’ => false, // Make it private if needed.
‘publicly_queryable’ => false,
‘show_ui’ => true,
‘show_in_menu’ => true,
‘query_var’ => true,
‘rewrite’ => array( ‘slug’ => ‘form-submission’ ),
‘capability_type’ => ‘post’,
‘has_archive’ => false,
‘hierarchical’ => false,
‘menu_position’ => null,
‘supports’ => array( ‘title’ ), // You can add ‘editor’ if you want a content editor.
);

register_post_type( ‘form_submission’, $args );
}
add_action( ‘init’, ‘create_form_submission_post_type’ );
“`

This code registers a custom post type called `form_submission`. Note that `public` and `publicly_queryable` are set to `false` as this example assumes you want to display the entries through a custom template, not directly through standard WordPress archives. Set them to true if you want entries to behave like standard posts.

2. Capture Form Data and Create a Custom Post.

You need to hook into the form submission process of your chosen form plugin and create a new custom post type entry with the submitted data. The specifics of this depend on the form plugin you are using.

For example, using Gravity Forms, you could use the `gform_after_submission` hook:

“`php
add_action( ‘gform_after_submission’, ‘create_form_submission_post’, 10, 2 );

function create_form_submission_post( $entry, $form ) {
$post_title = ‘Form Submission – ‘ . date(‘Y-m-d H:i:s’); // Or a more meaningful title

$post_id = wp_insert_post( array(
‘post_title’ => $post_title,
‘post_type’ => ‘form_submission’,
‘post_status’ => ‘publish’,
) );

if ( ! is_wp_error( $post_id ) ) {
// Loop through the form fields and save them as custom fields.
foreach ( $form[‘fields’] as $field ) {
$field_id = $field->id;
$field_value = rgar( $entry, $field_id ); // Gravity Forms function to get the value.

update_post_meta( $post_id, ‘form_field_’ . $field_id, $field_value );
}
}
}
“`

This code snippet listens for Gravity Forms submissions and creates a new `form_submission` post for each submission. It then iterates through the submitted form fields and saves each field’s value as post meta (custom fields) associated with the newly created post. The meta keys are prefixed with `form_field_` followed by the field ID.

Important: Replace `rgar` with the appropriate function to retrieve the form data based on the specific form plugin you are using. Refer to your form plugin’s documentation. Also, sanitize and validate the form data before saving it to the database to prevent security vulnerabilities.

3. Create a Custom Template.

Create a template file (e.g., `single-form_submission.php`) in your theme’s directory. This template will be used to display individual form submissions. If you are just creating a page to show the entries, you can create a custom template for a page, and just list the entries rather than show one in detail.

“`php

Name: ‘ . esc_html( $name ) . ‘

‘;
}
if ( $email ) {
echo ‘

Email: ‘ . esc_html( $email ) . ‘

‘;
}
if ( $message ) {
echo ‘

Message: ‘ . esc_html( $message ) . ‘

‘;
}

?>

Remember to replace the `’form_field_1’`, `’form_field_2’`, and `’form_field_3’` keys with the actual meta keys you used when saving the form data. Also, always escape the output using `esc_html()` to prevent cross-site scripting (XSS) vulnerabilities.

4. Displaying a List of Entries.

To show a list of all form submissions on a page, you can create a custom page template or modify an existing one. Use the `WP_Query` class to retrieve the `form_submission` posts:

“`php

Form Submissions

‘form_submission’,
‘posts_per_page’ => -1, // Show all submissions
);

$the_query = new WP_Query( $args );

if ( $the_query->have_posts() ) {
echo ‘

    ‘;
    while ( $the_query->have_posts() ) {
    $the_query->the_post();
    $post_id = get_the_ID();
    $name = get_post_meta( $post_id, ‘form_field_1’, true );

    echo ‘

  • ‘ . get_the_title() . ‘ – ‘ . esc_html( $name ) . ‘
  • ‘;
    }
    echo ‘

‘;
wp_reset_postdata();
} else {
echo ‘

No form submissions found.

‘;
}
?>

Pros and Cons of Using Custom Post Types and Templates

Pros:

  • Complete control: You have complete control over how the data is stored and displayed.
  • Customization: You can customize the display to meet your exact requirements.
  • Scalability: This approach can be more scalable for complex data and display requirements.
  • No plugin dependencies: You don’t rely on third-party plugins.

Cons:

  • Requires coding knowledge: This approach requires coding skills in PHP, HTML, and CSS.
  • Development time: It takes significantly longer to develop compared to using a plugin.
  • Maintenance: You are responsible for maintaining and updating the code.
  • Complexity: This approach can be more complex, especially for beginners.