Last active
February 23, 2022 16:09
-
-
Save strangerstudios/3d2824b8caf56734a54a to your computer and use it in GitHub Desktop.
Generate a "Member Number" for WordPress users and show it on the PMPro account page.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Member Numbers | |
* Change the generate_member_number function if your member number needs to be in a certain format. | |
* Member numbers are generated when users are registered or when the membership account page is accessed for the first time. | |
*/ | |
//Generate member_number when a user is registered. | |
function generate_member_number($user_id) | |
{ | |
$member_number = get_user_meta($user_id, "member_number", true); | |
//if no member number, create one | |
if(empty($member_number)) | |
{ | |
global $wpdb; | |
//this code generates a string 10 characters long of numbers and letters | |
while(empty($member_number)) | |
{ | |
$scramble = md5(AUTH_KEY . current_time('timestamp') . $user_id . SECURE_AUTH_KEY); | |
$member_number = substr($scramble, 0, 10); | |
$check = $wpdb->get_var("SELECT meta_value FROM $wpdb->usermeta WHERE meta_value = '" . esc_sql($member_number) . "' LIMIT 1"); | |
if($check || is_numeric($member_number)) | |
$member_number = NULL; | |
} | |
//save to user meta | |
update_user_meta($user_id, "member_number", $member_number); | |
return $member_number; | |
} | |
} | |
add_action('user_register', 'generate_member_number'); | |
//Show it on the membership account page. | |
function pmpromn_pmpro_account_bullets_bottom() | |
{ | |
if(is_user_logged_in()) | |
{ | |
global $current_user; | |
//get member number | |
$member_number = get_user_meta($current_user->ID, "member_number", true); | |
//if no number, generate one | |
if(empty($member_number)) | |
$member_number = generate_member_number($current_user->ID); | |
//show it | |
if(!empty($member_number)) | |
{ | |
?> | |
<li><strong><?php _e("Member Number", "pmpro");?>:</strong> <?php echo $member_number?></li> | |
<?php | |
} | |
} | |
} | |
add_action('pmpro_account_bullets_bottom', 'pmpromn_pmpro_account_bullets_bottom'); | |
add_action('pmpro_invoice_bullets_bottom', 'pmpromn_pmpro_account_bullets_bottom'); | |
//show member_number in confirmation emails | |
function pmpromn_pmpro_email_filter($email) | |
{ | |
global $wpdb; | |
//only update admin confirmation emails | |
if(strpos($email->template, "checkout") !== false) | |
{ | |
if(!empty($email->data) && !empty($email->data['user_login'])) | |
{ | |
$user = get_user_by("login", $email->data['user_login']); | |
if(!empty($user) && !empty($user->ID)) | |
{ | |
$member_number = get_user_meta($user->ID, "member_number", true); | |
if(!empty($member_number)) | |
$email->body = str_replace("<p>Membership Level", "<p>Member Number:" . $member_number . "</p><p>Membership Level", $email->body); | |
} | |
} | |
} | |
return $email; | |
} | |
add_filter("pmpro_email_filter", "pmpromn_pmpro_email_filter", 30, 2); |
This recipe is included in the blog post on "Generate a Unique Member Number for Display on Membership Account, Confirmation Email and More" at Paid Memberships Pro here: https://www.paidmembershipspro.com/generate-a-unique-member-number-for-display-on-membership-account-confirmation-email-and-more/
How can I display the member number on the directory and profile pages?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice work, please let me know where do I add this into WordPress?