Simple Membership - Export Members Data (and Meta Data) to Excel or CSV
scriptwriterph technical support - free consultation

Simple Membership - Export Members Data (and Meta Data) to Excel or CSV

Last updated on February 19th, 2021

Here in this tutorial, we'll share you a way to export Simple Membership members data (and meta data) to a file such as Excel or CSV for FREE.

This might be helpful for WordPress developers who uses Simple Membership plugin as their membership solution; and for those who use Form Builder Addon (to add custom field/s in Registration form), user meta will be included also in the exported file.

One of the solution (and probably the easiest way) to export Simple Membership's user data is to install their Member Data Exporter Addon. Having this paid addon, you will be able to export all member's profile data and payments data to Excel or CSV easily. But if you don't want to spend a dime, if you want to save $39.95, you may try our FREE solution here below.

So How We Can Export Simple Membership's Members Data?

Simple Membership plugin stores users primary data in swpm_members_tbl table; while users meta data (data captured from Registration form's custom field/s -- created using Form Builder Addon) stored in swpm_form_builder_custom table.

Here's what we're going to do. We're going to create 2 custom functions -- custom AJAX endpoints in functions.php file.

  • 1st function will be responsible for exporting members primary data only;
  • While the 2nd function will be responsible for exporting both member's data and meta data. This function might be helpful for those developers who use Form Builder Addon.

After adding custom functions in functions.php, you have to access a particular AJAX endpoint from your browser to trigger the function. The function will do its magic -- Excel/CSV file will be downloaded from your computer automatically.

Here's what will happen in background when you access a particular AJAX endpoint on your browser:

  1. The function will execute a SQL -- querying Simple Membership's table/s.
  2. Member's data will be processed.
  3. Processed data will be exported to either Excel or CSV (depends on which one you'll enable).

Simple Membership - Export Members Data (and Meta Data) to Excel or CSV

Before Anything Else

Before we jump in, we expect that:

  • You have your WordPress website already set up and running on your server plus you have Simple Membership plugin already installed.
  • You have a child theme already created. If you don't have a child theme, create one first, otherwise, your updates in functions.php will be overwritten once you update your theme.
  • You have the latest backup of your WordPress website. If you don't have one, perform a backup first. You may use backup plugins such as All in one WP migration or Duplicator.

PHP Code to Export Simple Membership's Members Data to Excel/CSV

Open your child theme's functions.php file and paste the following code below.

function get_simple_membership_members_data() {

	global $wpdb; // Instantiate global $wpdb object (we'll need it later to access our WordPress database).

	$swpm_members_tbl = 'swpm_members_tbl'; // Define Simple Membership's main table.

	$file_name = 'file'; // Set the file name of your Excel or CSV file.

	// Execute SQL -- SELECT -- query members data from Simple Membership's main table -- swpm_members_tbl; it'll return a PHP object. Adjust this SQL according to your needs.
	$wp_swpm_members = $wpdb->get_results("SELECT member_id, user_name, first_name, last_name, member_since, membership_level, account_state, email, phone, company_name FROM $wpdb->prefix$swpm_members_tbl ORDER BY member_id DESC LIMIT 10", ARRAY_A );

	if( count($wp_swpm_members) > 0 ) {
	  foreach( $wp_swpm_members as $wp_swpm_member ) {

	  	// Prepare members data.
		  $member_id = $wp_swpm_member['member_id'];
		  $user_name = $wp_swpm_member['user_name'];
		  $first_name = $wp_swpm_member['first_name'];
		  $last_name = $wp_swpm_member['last_name'];
		  $member_since = $wp_swpm_member['member_since'];
		  $membership_level = $wp_swpm_member['membership_level'];
		  $account_state = $wp_swpm_member['account_state'];
		  $email = $wp_swpm_member['email'];
		  $phone = $wp_swpm_member['phone'];
		  $company_name = $wp_swpm_member['company_name'];

		  $result_arr = array();

		  // Push members data to array -- $result_arr.
		  array_push($result_arr, $member_id, $user_name, $first_name, $last_name, $member_since, $membership_level, $account_state, $email, $phone, $company_name);

		  // Members data imploded with "\t" (tab); this will be the data we'll print when we export to Excel file.
		  $result_implode_tab = implode("\t", $result_arr);
		  $xls_output .= $result_implode_tab . "\n";

		  // Members data imploded with ", " (comma); and this one we'll print when we're exporting to CSV file).
		  $result_implode_comma = implode(", ", $result_arr);
		  $csv_output .= $result_implode_comma . "\n";
		}
	}

	$file_name = $file_name . "_" . date( "Y-m-d_H-i", time() ); // Append date and time to your file name.

	// Comment these 6 lines of code if you want to export file as CSV instead of Excel.
	header( "Content-type: application/xls" ); // Set the content type for Excel files.
	header( "Content-Disposition: attachment; filename = " . $file_name. ".xls" ); // Set the content-disposition for Excel files.
	header( "Pragma: no-cache" );
	header( "Expires: 0" );
	print $xls_output; // Lastly, print the ouput.
	exit;

	// Uncomment these 5 lines of code if you want to export file as CSV instead of Excel.
	// header( "Content-type: application/vnd.ms-excel" );
	// header( "Content-disposition: csv" . date("Y-m-d" ) . ".csv" );
	// header( "Content-disposition: filename = " . $file_name. ".csv" );
	// print $csv_output;
	// exit;
}
add_action('wp_ajax_swpm_members_excel_csv', 'get_simple_membership_members_data');
// The Excel/CSV file (containing members data) will be downloaded to your computer automatically when you access the following URL in your browser: http://your-domain/wp-admin/admin-ajax.php?action=swpm_members_excel_csv.

Source code explanation:

Line 1: Create a function named get_simple_membership_members_data. This function will be responsible for: 1. Getting member's primary data from Simple Membership's main table -- swpm_members_tbl. 2. Processing and formatting each member's data. 3. Lastly, exporting members data to either Excel (.xls) or CSV (.csv) file.

Line 3: Instantiate global $wpdb object (we'll need it later to access our WordPress database).

Line 5: Define Simple Membership's main table.

Line 7: Set the file name of your Excel or CSV file.

Line 10: Execute SQL -- SELECT -- query members data from Simple Membership's main table -- swpm_members_tbl; it'll return a PHP object. Adjust this SQL according to your needs.

Line 15: Prepare members data. Here's all members data available that you may wish to include: member_id, user_name, first_name, last_name, password, member_since, membership_level, more_membership_levels, account_state, last_accessed, last_accessed_from_ip, email, phone, address_street, address_city, address_state, address_zipcode, home_page, country, gender, referrer, extra_info, reg_code, subscription_starts, initial_membership_level, txn_id, subscr_id, company_name, notes, flags, profile_image.

Line 29: Push members data to array -- $result_arr.

Line 32: Members data imploded with "\t" (tab); this will be the data we'll print when we export to Excel file.

Line 36: Members data imploded with ", " (comma); this one we'll print when we're exporting to CSV file).

Line 42: Append date and time to your file name.

Line 45-49: Set the content type for Excel file; set the content-disposition for Excel file; Lastly, print the output.

Line 45-50: Comment these 6 lines of code if you want to export file as CSV instead of Excel.

Line 53-57: Uncomment these 5 lines of code if you want to export file as CSV instead of Excel.

The Excel/CSV file (containing members data) will be downloaded to your computer automatically when you access the following URL in your browser: http://your-domain/wp-admin/admin-ajax.php?action=swpm_members_excel_csv.

PHP Code to Export Simple Membership's Members Data and Meta Data to Excel/CSV

This function is specifically for those developers who use Simple Membership – Form Builder Addon.

Open your child theme's functions.php file once again and paste the following code below.

function get_simple_membership_members_data_and_meta_data () { 

	global $wpdb; // Instantiate global $wpdb object (we'll need it later to access our WordPress database).

	$swpm_members_tbl = 'swpm_members_tbl'; // Define Simple Membership's main table.
	$swpm_form_builder_custom = 'swpm_form_builder_custom'; // Define Simple Membership's - Form Builder Addon table

	$file_name = 'file'; // Set the file name of your Excel or CSV file.

	// Execute SQL -- SELECT -- query members data from Simple Membership's main table -- swpm_members_tbl; it'll return a PHP object. Adjust this SQL according to your needs.
	$wp_swpm_members = $wpdb->get_results("SELECT member_id, user_name, first_name, last_name, member_since, membership_level, account_state, email, phone, company_name FROM $wpdb->prefix$swpm_members_tbl ORDER BY member_id DESC LIMIT 10", ARRAY_A );

	if( count($wp_swpm_members) > 0 ) {
	  foreach( $wp_swpm_members as $wp_swpm_member ) {

	  	// Prepare members data.
		  $member_id = $wp_swpm_member['member_id'];
		  $user_name = $wp_swpm_member['user_name'];
		  $first_name = $wp_swpm_member['first_name'];
		  $last_name = $wp_swpm_member['last_name'];
		  $member_since = $wp_swpm_member['member_since'];
		  $membership_level = $wp_swpm_member['membership_level'];
		  $account_state = $wp_swpm_member['account_state'];
		  $email = $wp_swpm_member['email'];
		  $phone = $wp_swpm_member['phone'];
		  $company_name = $wp_swpm_member['company_name'];

		  $result_arr = array();

		  // Push members data to array -- $result_arr.
		  array_push($result_arr, $member_id, $user_name, $first_name, $last_name, $member_since, $membership_level, $account_state, $email, $phone, $company_name);

		  // Execute another SQL -- SELECT -- now we're querying member meta data from Simple Membership - Form Builder Addon table -- swpm_form_builder_custom; PHP object will be returned.
		  $wp_swpm_member_meta = $wpdb->get_results( "SELECT field_id, user_id, value FROM $wpdb->prefix$swpm_form_builder_custom WHERE user_id = $member_id" );

		  foreach ($wp_swpm_member_meta as $obj_wp_swpm_member_meta) {
		  	$custom_field = ( !empty($obj_wp_swpm_member_meta->value) ) ? $obj_wp_swpm_member_meta->value : 'n/a';

		  	// Also push member's meta data to array -- $result_arr.
				array_push($result_arr, $custom_field);
		  }

		  // Members data imploded with "\t" (tab); this will be the data we'll print when we export to Excel file.
		  $result_implode_tab = implode("\t", $result_arr);
		  $xls_output .= $result_implode_tab . "\n";

		  // Members data imploded with ", " (comma); and this one we'll print when we're exporting to CSV file).
		  $result_implode_comma = implode(", ", $result_arr);
		  $csv_output .= $result_implode_comma . "\n";
		}
	}

	$file_name = $file_name . "_" . date( "Y-m-d_H-i", time() ); // Append date and time to your file name.

	// Comment these 6 lines of code if you want to export file as CSV instead of Excel.
	header( "Content-type: application/xls" ); // Set the content type for Excel files.
	header( "Content-Disposition: attachment; filename = " . $file_name. ".xls" ); // Set the content-disposition for Excel files.
	header( "Pragma: no-cache" );
	header( "Expires: 0" );
	print $xls_output; // Lastly, print the output.
	exit;

	// Uncomment these 5 lines of code if you want to export file as CSV instead of Excel.
	// header( "Content-type: application/vnd.ms-excel" );
	// header( "Content-disposition: csv" . date("Y-m-d" ) . ".csv" );
	// header( "Content-disposition: filename = " . $file_name. ".csv" );
	// print $csv_output;
	// exit;
}
add_action('wp_ajax_swpm_members_w_meta_excel_csv', 'get_simple_membership_members_data_and_meta_data');
// The Excel/CSV file (containing members data and meta data) will be downloaded to your computer automatically when you access the following URL in your browser: http://your-domain/wp-admin/admin-ajax.php?action=wp_ajax_swpm_members_excel_csv.

Source code explanation (source code's much like the first one, the only difference is we have to add additional query for getting members meta data here):

Line 33: Execute another SQL -- SELECT -- now we're querying member's meta data from Form Builder Addon table -- swpm_form_builder_custom; PHP object will be returned.

Line 40: Also push member's meta data to array -- $result_arr.

The Excel/CSV file (containing members data and meta data) will be downloaded to your computer automatically when you access the following URL in your browser: http://your-domain/wp-admin/admin-ajax.php?action=wp_ajax_swpm_members_excel_csv.

As you can see in GIF here above, right after we access our custom AJAX endpoint, the Excel/CSV file exported almost immediately. But if you have thousand/s of members registered on your website, the function may take longer time to finish (maybe 30 seconds or more).

Does it work on your end? Let us know in the comments below.

Hopefully, this tutorial helped you to export your Simple Membership's members data (and meta data) to Excel and CSV.

2 replies
  1. Renan Barreto
    Renan Barreto says:

    Thanks for the script.

    Just one thing. When I export i prints out only 5 members to the file, but I have more than that.
    What can be happening?

    Reply

Leave a Reply

You have any questions or suggestions? Experiencing technical issues?

Please drop a comment, we're willing to resolve any issues and improve our solutions. Let's have a personal and meaningful conversation. 😀

Leave a Reply

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

Web Developer • Technical Writer
I'm a web developer with a flair for creating elegant software solutions. I love to create and share content primarily about web development, programming, and other IT related topics.