Home / Archive / MemberPress: Export a CSV File for All Corporate Accounts with Subaccounts in MemberPress
Duplicate Snippet

Embed Snippet on Your Site

MemberPress: Export a CSV File for All Corporate Accounts with Subaccounts in MemberPress

This code snippet creates an export tool for MemberPress Corporate Accounts, allowing administrators to generate a comprehensive CSV file containing all corporate accounts and their associated sub-accounts.

After adding the code to your site, you can trigger the export process by:

The user must be logged in as an administrator.
Visit the following URL in the browser: https://yourwebsite.com/wp-admin/?export_subaccounts=1 (The "yourwebsite.com" needs to be replaced with the actual website domain).
The export will process automatically and download a CSV file named "subaccounts-[timestamp].csv" to a local computer.

Code Preview
php
<?php
add_action('admin_init', function() {
	global $wpdb;
	
	if (! isset( $_GET['export_subaccounts'] ) ) {
		return;
	}
	$corp_ids = $wpdb->get_results("SELECT id FROM {$wpdb->prefix}mepr_corporate_accounts");
	
	$csv_results = array();
	
	foreach ( $corp_ids as $corp ) {
		// Get corp object
		$ca = new MPCA_Corporate_Account( $corp->id );
		
		// Get corp owner
		$ca_owner = $ca->user();
		// Get sub users
		$sub_accounts = $ca->sub_users();
		
		foreach($sub_accounts as $sub_account) {
		  $csv_results[] = array(
			'Owner Name'         => $ca_owner->full_name(),
			'Owner Email'        => $ca_owner->user_email,			  
			'Subacc. Email'      => $sub_account->user_email,
			'Subacc. Username'   => $sub_account->user_login,
			'Subacc. First Name' => $sub_account->first_name,
			'Subacc. Last Name'  => $sub_account->last_name,
		  );
		}
	}
	
	$filename = 'subaccounts-'.time();
	MeprUtils::render_csv( $csv_results, $filename );
});

Comments

Add a Comment