问题
I have searched everywhere for a plugin that would generate a unique sequential membership number upon registration for Wordpress without any joy. Hopefully, other people out there would like this same functionality so with a bit of research and copying bits of code that I have found on the web have put together the following solution.
It is almost there but I would like to extend the functionality a bit more and was wondering if anyone could point me in the right direction.
I would like to display the ID Number (Membership Number) on the WooCommerce my_account page as well as on some of the WooCommerce emails such as New Order and New Account emails.
I would also like to include a prefix to the 4 digit number that gets generated such as AU-0001 (AU can remain the same before each sequential number). Can I just add?...
<?php echo "AU-"$unique_number; ?>
The below code is working fine. It generates and shows the Membership Number in the User Profile list and User Profile Page after account creation.
If possible I would just like to add to the below code so that I am not modifying any core files that would get overridden when updating. The below code is in my functions.php file of my child theme.
Also, the //send email below sends the email with the text output but not the
strip_tags($user_id['my_unique_id'])
new Member Number. (This is not essential as I would rather not send a separate email but rather have the Member number show and send in the WooCommerce emails).
Thank you all in advance!
//create unique id on new user creation
add_action( 'user_register', 'my_on_user_register' );
function my_on_user_register( $user_id ) {
$unique_id = + 1000 + $user_id;
update_user_meta( $user_id, 'my_unique_id', $unique_id );
}
//display Membership Number on profile
add_action('show_user_profile', 'my_extra_user_profile_fields');
add_action('edit_user_profile', 'my_extra_user_profile_fields');
function my_extra_user_profile_fields($user){
$unique_number = get_the_author_meta('my_unique_id', $user->ID);
?>
<h3><?php _e('Membership Number'); ?></h3>
<table class="form-table">
<tr>
<th><label for="address"><?php _e('Member Number:'); ?></label></th>
<td><?php echo "AU-"$unique_number; ?></td>
</tr>
</table>
<?php
}
//send email with Membership Number to admin on new user registration
function registration_email_alert($user_id) {
$message = strip_tags($_POST['user_login']). ' - ' . strip_tags($_POST['user_email']) . ' SMWS Membership Number: ' . strip_tags($user_id['my_unique_id']) . ' Has registered to the SMWS';
wp_mail( 'your@email.com.au', 'New member has just registered', $message );
}
add_action('user_register', 'registration_email_alert');
//add Membership Number to user list
function new_modify_user_table( $column ) {
$column['my_unique_id'] = 'Membership Number';
return $column;
}
add_filter( 'manage_users_columns', 'new_modify_user_table' );
function new_modify_user_table_row( $val, $column_name, $user_id ) {
$user = get_userdata( $user_id );
switch ($column_name) {
case 'my_unique_id' :
return get_the_author_meta( 'my_unique_id', $user_id );
break;
default:
}
return $return;
}
add_filter( 'manage_users_custom_column', 'new_modify_user_table_row', 10, 3 );
来源:https://stackoverflow.com/questions/32418468/generate-sequential-membership-number-on-wordpress-signup