Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use unique membership key as username with PMPro checkout.
<?php // Do not copy this tag
// Use unique membership key as username with PMPro checkout. You'll also have to hide the username fields at checkout using CSS or a custom checkout page template.
// Paste the code below into a PMPro Customization Plugin: https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
function salted_md5($encrypt_string) {
// If no string is passed create one from timestamp
if ( !$encrypt_string )
$encrypt_string = rand(10000,99999);
// Hash string using md5 encryption and salt it with current timestamp
$encrypted_string = md5(AUTH_KEY . current_time('timestamp') . $encrypt_string . SECURE_AUTH_KEY);
return $encrypted_string;
}
function my_init_unique_key_as_username()
{
// Check that user isn't logged in before continuing
if ( is_user_logged_in() )
return;
//check for level as well to make sure we're on checkout page
if(empty($_REQUEST['level']))
return;
if(!empty($_REQUEST['bemail']))
$_REQUEST['username'] = substr(salted_md5($_REQUEST['bemail']), 0, 10);
if(!empty($_POST['bemail']))
$_POST['username'] = substr(salted_md5($_POST['bemail']), 0, 10);
if(!empty($_GET['bemail']))
$_GET['username'] = substr(salted_md5($_POST['bemail']), 0, 10);
}
add_action('init', 'my_init_unique_key_as_username');
//Show it on the membership account page.
function pmprouku_pmpro_account_bullets_top()
{
if(is_user_logged_in())
{
global $current_user;
//get user login
$user = get_user_by( 'id', $current_user->ID );
$member_login = $user->user_login;
//show it
if(!empty($member_login))
{
?>
<li><strong><?php _e("Username", "pmpro");?>:</strong> <?php echo $member_login?></li>
<?php
}
}
}
add_action('pmpro_account_bullets_top', 'pmprouku_pmpro_account_bullets_top');
add_action('pmpro_invoice_bullets_top', 'pmprouku_pmpro_account_bullets_top');
//show unique username in confirmation emails
function pmprouku_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_login = $user->user_login;
if(!empty($member_login))
$email->body = str_replace("<p>Membership Level", "<p>Username: " . $member_login . "</p><p>Membership Level", $email->body);
}
}
}
return $email;
}
add_filter("pmpro_email_filter", "pmprouku_pmpro_email_filter", 30, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.