Forked from greathmaster/pmpro-assign-username-at-checkout.php
Created
August 29, 2017 07:14
-
-
Save andrewlimaza/2b1bb82b753ebc3b5e5d8306a028157e to your computer and use it in GitHub Desktop.
Assigns a random 6 digit number for a username automatically. Two variations...the first will show the username field with number already filled, but editing is disabled. The second variations (uncomment the lines) will not show the username field at all, but will simply generate the numerical username and display it on the confirmation 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
/* | |
Assigns a random 6 digit number for a username automatically. | |
Two variations... | |
the first will show the username field with number already filled, but editing is disabled. | |
The second variations (uncomment the lines) will not show the username field at all, but will simply generate the numerical username and display it on the confirmation page. | |
*/ | |
function my_pmpro_checkout_after_user_fields() | |
{ | |
echo "<script>"; | |
// echo "jQuery('#username').hide();"; | |
// echo "jQuery('label[for=username]').hide();"; | |
if(empty($_REQUEST['submit-checkout'])) | |
{ | |
echo | |
"//Taken from: https://stackoverflow.com/questions/1527803/generating-random-whole-numbers-in-javascript-in-a-specific-range | |
function getRandomInt(min, max) { | |
return Math.floor(Math.random() * (max - min + 1)) + min; | |
} | |
var rand_num_username; | |
rand_num_username = getRandomInt(100000, 999999); | |
jQuery('#username').val(rand_num_username); | |
jQuery('#username').prop('readonly', true);"; | |
} | |
echo "</script>"; | |
} | |
add_action('pmpro_checkout_after_user_fields', 'my_pmpro_checkout_after_user_fields'); | |
function my_pmpro_required_user_fields($pmpro_required_user_fields) | |
{ | |
unset($pmpro_required_user_fields['username']); | |
return $pmpro_required_user_fields; | |
} | |
//add_filter('pmpro_required_user_fields', 'my_pmpro_required_user_fields'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can I use a variation of this to assign the sponsoring user's email to the sponsored user? (Note: I've already used Allow Multiple Plugins to let multiple users use the same email address).
function parent_email_add()
{
global $wpdb, $pmpro_level, $discount_code;
if($pmpro_level->id == 5 && !empty($discount_code)) // 5 is the sponsoring level
{
$code_id = $wpdb->get_var( "SELECT * FROM $wpdb->pmpro_discount_codes WHERE code = '" . esc_sql( $discount_code ) . "' LIMIT 1" ); //finds ID for sponsored code
$code_user_ids = get_option("pmpro_code_user_ids"); //searches database of discount codes
$parent_id = $code_user_ids[$code_id]; // finds user ID associated with sponsored code
$parent = get_user_by('id', $parent_id);
$parent_email = $parent->user_email; // get parent email
if(empty($_REQUEST['submit-checkout']))
{
echo " <script> jQuery('#bemail').val($parent_email); </script>";
}
}
}
add_action ("pmpro_checkout_after_user_fields", "parent_email_add", 10, 4);
function my_pmpro_required_user_fields($pmpro_required_user_fields)
{
unset($pmpro_required_user_fields['bemail']);
return $pmpro_required_user_fields;
}