Posted on by

Adding a CSV Download Button

As of version 1.7.3.2, Participants Database allows you to add a CSV download form to your regular frontend list displays. It does require a little configuration and a custom template, but we’ll walk you through the process in this post.

First, the warning: be careful with this, it gives your users direct access to your data. You do have some control over what they can get, but it is up to you to control who has access to the download form. The form itself is as secure as we can reasonably make it, but the final and most important layer of security is up to you. Be careful who you give access to.

How to Set It Up

The CSV download form is generated by a template function that you must add to a custom template. If you’re not sure how custom templates in Participants Database work, read this article before you begin: Using Custom Templates You will be using a template for the list shortcode, it doesn’t matter which one, it will work with any list shortcode template. If you have a custom template for your list already, you can just modify it with these changes.

The reason we made it slightly inconvenient to add the feature is because we want to make sure it is only available if the site administrator really wants to allow it.

The change you need to make is very simple: we add the template function to the template just before the final closing tag of the outer wrapper. Here is the default list template with the download form added:

If you know what you are doing, you can put in in other locations, but it must be in a Participants Database list template.

Configuring the Form

The form has several configuration possibilities:

  • ‘title’ – the download form title
  • ‘helptext’ – the explanatory text shown under the download button
  • ‘button_text’ – the text shown on the download button
  • ‘filename’ –  the initial filename to use for the downloaded file
  • ‘allow_user_filename’ – if set to false, the user cannot set their own filename for the downloaded file
  • ‘export_fields’ – defines a list of fields to include in the download

To set any of these different from the default values, you need to put your preferred value into the $config array, for example, like this:

$config = array( 
     'title' => 'Search Result Download', 
     'helptext' => 'Click the "download" button to download your search results.', 
     'button_text' => 'download', 
     );

Setting Which Fields Are Included in the Download

By default, all the fields that are configured to be exported in a CSV will be included in the frontend CSV download, the same as it works on the backend. This is determined in the field definition on the Manage Database Fields page: all fields with the “CSV” checkbox checked will be downloaded.

If you want to determine which fields are included in the frontend CSV download, use the ‘export_fields’ configuration value. This value must be an array of valid fieldnames. Here is what that could look like:

$config = array( 
     'export_fields' => array( 'first_name', 'last_name', 'email' ) 
     );

Make sure you are using the ‘name’ of the fields and not the title.

One thought on “Adding a CSV Download Button

  1. This is just so wonderful! Have tried in vain for so long, to integrate a download CSV button into some of my sites, this means quite quite a lot. Thank you so much Mr. Roland for this priceless feature. Can’t thank you enough!!!

Leave a Reply

Your email address will not be published. Required fields are marked *

One thought on “Adding a CSV Download Button

  1. This is just so wonderful! Have tried in vain for so long, to integrate a download CSV button into some of my sites, this means quite quite a lot. Thank you so much Mr. Roland for this priceless feature. Can’t thank you enough!!!

Leave a Reply

Your email address will not be published. Required fields are marked *