Skip to content

Instantly share code, notes, and snippets.

@trey8611
Last active June 14, 2023 09:12
Show Gist options
  • Save trey8611/6a2caa8c3cc3ccd56ad02886e2ec49fb to your computer and use it in GitHub Desktop.
Save trey8611/6a2caa8c3cc3ccd56ad02886e2ec49fb to your computer and use it in GitHub Desktop.
WP All Export - export data from custom database tables

Sometimes you might need to export extra data from a custom database table. This is possible by using a custom PHP function in the export along with the WPDB class.

For example, let’s say that we are exporting Users from a BuddyPress installation and we need to get the value of the “Name” field from the BuddyPress “Extended Profile” section: https://d.pr/6kZfA8. This information is stored inside the ‘bp_xprofile_data’ table: https://d.pr/CTdjb2. So, to export this, we’ll want to:

  • Click “Add Field” in our export, keep “ID” in the top drop-down
  • Name the field, for example “BuddyPress Name”
  • Click “Export the value returned by a PHP function"
  • Type in our function name, add the code to the function editor, and click “Done”.

Example video: https://drive.google.com/file/d/0BxSOi52PDCsAZ1JXazB0RzFFTnc/view?usp=sharing&resourcekey=0-OOnGF0Zes40Qqn6iEvFtIQ

Function code:

function soflyy_get_bp_name( $user_id ) {
	global $wpdb;
	$table = $wpdb->prefix . 'bp_xprofile_data';
	
	$results = $wpdb->get_row( $wpdb->prepare( "SELECT `value` FROM `" . $table . "` WHERE `user_id` = '%d' AND `field_id` = '1' LIMIT 1", $user_id ) );
	
	if ( !empty( $results->value ) ) {
		return $results->value;
	}
	return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment