Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save andrewlimaza/67dd56bb284adc4687d891d2a69e7922 to your computer and use it in GitHub Desktop.
Save andrewlimaza/67dd56bb284adc4687d891d2a69e7922 to your computer and use it in GitHub Desktop.
Auto-populate Contact Form 7 select field with active member names [Paid Memberships Pro]
<?php
/**
* Auto-populate user names for active members in a field drop-down.
* This will assume you are using a select field named "membership-list"
* The value of the select is the user's email and the label is set to the user's login name.
* Data Example: <option value="[email protected]">Test User</option>
* Add this code to your PMPro Customizations Plugin - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_pmpro_auto_populate_members ( $tag, $unused ) {
global $wpdb;
// Only load it for a field named "membership-list"
if ( $tag['name'] != 'membership-list' ) {
return $tag;
}
// Get a list of all active members for all membership levels.
$sql = "SELECT users.user_email, users.user_login FROM $wpdb->pmpro_memberships_users members INNER JOIN $wpdb->users users ON users.ID = members.user_id WHERE `status` = 'active'";
$member_data = $wpdb->get_results( $sql );
if ( ! $member_data ) {
return $tag;
}
//Let's get all members.
foreach ( $member_data as $members ) {
$tag['values'][] = $members->user_email; // use the user's email as the value.
$tag['labels'][] = $members->user_login; // Show the username as the label.
}
return $tag;
}
add_filter( 'wpcf7_form_tag', 'my_pmpro_auto_populate_members', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment