Forked from ipokkel/sequential-numeric-member-number.php
Last active
October 30, 2024 13:30
-
-
Save michaelbeil/37d03aca5c21a33c6b0bb505fd5d4f29 to your computer and use it in GitHub Desktop.
Create sequential membership numbers. Display on user account, edit profile, and memberslist pages and membership card and include in memberslist csv export
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
<?php | |
/** | |
* Sequential Member Numbers | |
* Member numbers are automatically increased from the value set for $starting_member_number after each member registration | |
* | |
* Add this code to your PMPro Customizations Plugin | |
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
* | |
* Requirements: | |
* Membership Card Add On - https://www.paidmembershipspro.com/add-ons/pmpro-membership-card/ | |
* Custom Membership Card template - https://gist.github.com/ipokkel/c3a93a5718aa5646b80adff814ed76d4 | |
*/ | |
/** | |
* Generate member_number when a user is registered. | |
* | |
* Existing members already in the system will not receive a membership number | |
* This function can be, and is, called elsewhere to generate a membership number | |
* for existing members that doesn't have a members number | |
*/ | |
function generate_member_number( $user_id ) { | |
$starting_member_number = 3418; // set starting number here | |
$current_member_number = get_option( 'pmpro_seq_current_member_num' ); | |
if ( empty( $current_member_number ) ) { | |
$current_member_number = $starting_member_number; | |
} | |
update_user_meta( $user_id, 'member_number', $current_member_number ); | |
update_option( 'pmpro_seq_current_member_num', $current_member_number + 1 ); | |
return $current_member_number; | |
} | |
add_action( 'user_register', 'generate_member_number' ); | |
//Show it on the membership account page. | |
add_action( 'pmpro_account_bullets_bottom', 'pmpromn_pmpro_account_bullets_bottom' ); | |
add_action( 'pmpro_invoice_bullets_bottom', 'pmpromn_pmpro_account_bullets_bottom' ); | |
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 ); | |
// create on the fly | |
if ( empty( $member_number ) ) { | |
// lets generate it on the fly. | |
$member_number = generate_member_number( $current_user->ID ); | |
// fetch fido | |
$member_number = get_user_meta( $current_user->ID, 'member_number', true ); | |
} | |
//show it | |
if ( ! empty( $member_number ) ) { | |
?> | |
<li><strong><?php _e( 'Member Number', 'paid-memberships-pro' ); ?>:</strong> <?php echo $member_number; ?></li> | |
<?php | |
} | |
} | |
} | |
//show member_number in confirmation emails | |
add_filter( 'pmpro_email_filter', 'pmpromn_pmpro_email_filter', 30, 2 ); | |
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; | |
} | |
//Show member number in Members List and Members List CSV export | |
add_filter( 'pmpro_members_list_csv_extra_columns', 'my_pmpro_members_list_csv_extra_columns', 10 ); | |
function my_pmpro_members_list_csv_extra_columns( $columns ) { | |
$columns['member_number'] = 'my_extra_column_member_number'; | |
return $columns; | |
} | |
function my_extra_column_member_number( $user ) { | |
if ( ! empty( $user->metavalues->member_number ) ) { | |
return $user->metavalues->member_number; | |
} else { | |
generate_member_number( $user->ID ); | |
return $user->metavalues->member_number; | |
} | |
} | |
//Show member number in Members List | |
add_action( 'pmpro_memberslist_extra_cols_header', 'my_pmpro_memberslist_extra_cols_header' ); | |
function my_pmpro_memberslist_extra_cols_header() { | |
?> | |
<th><?php _e( 'Member Number', 'paid-memberships-pro' ); ?></th> | |
<?php | |
} | |
//columns | |
add_action( 'pmpro_memberslist_extra_cols_body', 'my_pmpro_memberslist_extra_cols_body' ); | |
function my_pmpro_memberslist_extra_cols_body( $theuser ) { | |
if ( empty( $theuser->member_number ) ) { | |
// lets generate it on the fly. | |
generate_member_number( $theuser->ID ); | |
} | |
?> | |
<td> | |
<?php | |
if ( ! empty( $theuser->member_number ) ) { | |
echo $theuser->member_number; | |
} | |
?> | |
</td> | |
<?php | |
} | |
// Create register helper field to show member number on profile edit screen for admins | |
add_action( 'init', 'my_pmprorh_init_member_number' ); | |
function my_pmprorh_init_member_number() { | |
//don't break if Register Helper is not loaded | |
if ( ! function_exists( 'pmprorh_add_registration_field' ) ) { | |
return false; | |
} | |
//define the fields | |
$fields = array(); | |
$fields[] = new PMProRH_Field( | |
'member_number', // input name, will also be used as meta key | |
'text', // type of field | |
array( | |
'label' => 'Member Number', // custom field label | |
'size' => 20, // input size | |
'profile' => 'only_admin', // show in user profile for only admins | |
) | |
); | |
foreach ( $fields as $field ) { | |
pmprorh_add_registration_field( | |
'just_profile', // location on checkout page | |
$field // PMProRH_Field object | |
); | |
} | |
//that's it. see the PMPro Register Helper readme for more information and examples. | |
} | |
/** | |
* https://gist.github.com/andrewlimaza/a89d4ab1790d573dd0419731c066dce3 | |
* This will show Register Helper fields or any user meta on the user's profile only in a read-only format. | |
* (Admins won't see these fields). | |
* If Register Helper is used, you may set the field's "profile" attribute | |
* to be "only_admin" to make the fields editable by admins only. | |
*/ | |
add_action( 'show_user_profile', 'my_pmpro_show_read_only_register_helper_field', 15, 1 ); | |
function my_pmpro_show_read_only_register_helper_field( $user ) { | |
$meta = get_user_meta( $user->ID, 'member_number', true ); //match this to your Register Helper field name. | |
if ( ! empty( $meta ) ) { | |
?> | |
<table class="form-table"> | |
<tr> | |
<th><?php _e( 'Member Number: ' ); ?></th> | |
<td><?php echo $meta; ?></td> | |
</tr> | |
</table> | |
<?php | |
} | |
} | |
/** | |
* Inspired by: https://gist.github.com/andrewlimaza/46a3e543e5d6d069e4715c6fed679e81 | |
* | |
* This will show a member number on the membership card for Paid Memberships Pro. Please read all comments below! | |
* | |
* Requires custom membership card template with hooks | |
* (a) pmpro_membership_card_additional_fields_top | |
* (b) pmpro_membership_card_additional_fields_bottom | |
* https://gist.github.com/ipokkel/c3a93a5718aa5646b80adff814ed76d4 | |
* | |
* Add this code to your PMPro Customizations Plugin | |
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
* | |
*/ | |
// to display membership number at the bottom of the card hook into 'pmpro_membership_card_additional_fields_bottom' | |
add_action( 'pmpro_membership_card_additional_fields_top', 'my_show_member_number_on_card' ); | |
function my_show_member_number_on_card( $user ) { | |
if ( ! pmpro_hasMembershipLevel() ) { | |
return; | |
} | |
$member_id = get_user_meta( $user->ID, 'member_number', true ); | |
if ( empty( $member_id ) ) { | |
// lets generate it on the fly. | |
$member_id = generate_member_number( $user->ID ); | |
} | |
echo '<p><strong>Membership Number:</strong> ' . $member_id . '</p>'; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment