Skip to content

Instantly share code, notes, and snippets.

@ipokkel
Created January 20, 2021 10:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ipokkel/f62037b6877f47531fc69ea060e4670b to your computer and use it in GitHub Desktop.
Save ipokkel/f62037b6877f47531fc69ea060e4670b to your computer and use it in GitHub Desktop.
Custom signup shortcode where the email fields are before the password fields.
<?php
/**
* This recipe creates a custom signup shortcode where the email fields are
* before the password fields.
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_pmpro_signup_shortcode( $atts, $content = null, $code = '' ) {
global $current_user, $pmpro_level, $username, $email;
// $atts ::= array of attributes
// $content ::= text within enclosing form of shortcode element
// $code ::= the shortcode found, when == callback name
// examples: [pmpro_signup level="3" short="1" intro="0" submit_button="Signup Now"]
//make sure PMPro is activated
if ( ! function_exists( 'pmpro_getLevel' ) ) {
return 'Paid Memberships Pro must be installed to use the pmpro_signup shortcode.';
}
//set defaults
extract(
shortcode_atts(
array(
'intro' => '0',
'hidelabels' => null,
'level' => null,
'login' => true,
'redirect' => null,
'short' => null,
'submit_button' => __( 'Sign Up Now', 'pmprosus' ),
'title' => null,
'custom_fields' => true,
),
$atts
)
);
// If there is a current level in global, save it to a backup variable.
$pmpro_level_backup = $pmpro_level;
// try to get the Terms of Service page settings
$tospage = pmpro_getOption( 'tospage' );
// set title
if ( $title === '1' || $title === 'true' || $title === 'yes' ) {
$title_display = true;
}
if ( isset( $title_display ) ) {
if ( ! empty( $level ) ) {
$title = 'Register For ' . pmpro_getLevel( $level )->name;
} else {
$title = 'Register For ' . get_option( 'blogname' );
}
}
//turn 0's into falses
if ( $login === '0' || $login === 'false' || $login === 'no' ) {
$login = false;
} else {
$login = true;
}
//turn 0's into falses
if ( $custom_fields === '0' || $custom_fields === 'false' || $custom_fields === 'no' ) {
$custom_fields = false;
}
//the default checkout boxes location is loaded only if custom_fields is specifically "1" or "true"
if ( $custom_fields === '1' || $custom_fields === 'true' ) {
$checkout_boxes = true;
// Set the level for this signup shortcode so level-specific checkout fields appear.
if ( ! empty( $level ) ) {
$pmpro_level = pmpro_getLevel( $level );
}
} else {
$checkout_boxes = false;
}
//check which form format is specified
if ( ! empty( $hidelabels ) ) {
$hidelabels = true;
}
//turn 0's into falses
if ( $intro === '0' || $intro === 'false' || $intro === 'no' ) {
$intro = false;
}
//turn 1's and 'yes' into true
if ( $short === '1' || $short === 'true' || $short === 'yes' ) {
$short = true;
} elseif ( $short === 'emailonly' ) {
$short = 'emailonly';
} else {
$short = false;
}
// Get field values from URL or user.
if ( isset( $_REQUEST['username'] ) ) {
$username = sanitize_user( stripslashes( $_REQUEST['username'] ) );
} elseif ( is_user_logged_in() ) {
$username = $current_user->user_login;
} else {
$username = '';
}
if ( isset( $_REQUEST['email'] ) ) {
$bemail = sanitize_email( stripslashes( $_REQUEST['email'] ) );
} elseif ( is_user_logged_in() ) {
$bemail = $current_user->user_email;
} else {
$bemail = '';
}
// treat this page load as a checkout
add_filter( 'pmpro_is_checkout', '__return_true' );
// load recaptcha if needed
if ( ! function_exists( 'pmpro_recaptcha_get_html' ) ) {
pmpro_init_recaptcha();
}
global $current_user, $membership_levels, $pmpro_pages;
ob_start();
?>
<?php if ( ! empty( $current_user->ID ) && pmpro_hasMembershipLevel( $level, $current_user->ID ) ) { ?>
<?php
if ( current_user_can( 'manage_options' ) ) {
?>
<div class="pmpro_message pmpro_alert"><?php _e( '&#91;pmpro_signup&#93; Admin Only Shortcode Alert: You are logged in as an administrator and already have the membership level specified.', 'pmprosus' ); ?></div>
<?php
}
?>
<?php } else { ?>
<style>
.pmpro_signup_form-hidelabels .pmpro_checkout-field label:first-child {
clip: rect(1px, 1px, 1px, 1px);
position: absolute;
height: 1px;
width: 1px;
overflow: hidden
}
</style>
<form id="pmpro_form" class="pmpro_form pmpro_signup_form
<?php
if ( ! empty( $hidelabels ) ) {
?>
pmpro_signup_form-hidelabels<?php } ?>" action="<?php echo pmpro_url( 'checkout' ); ?>" method="post">
<?php
if ( ! empty( $title ) ) {
echo '<h2>' . $title . '</h2>';
}
?>
<?php
if ( ! empty( $intro ) ) {
echo wpautop( $intro );
}
?>
<div class="pmpro_checkout">
<div class="pmpro_checkout-fields">
<input type="hidden" id="level" name="level" value="<?php echo $level; ?>" />
<input type="hidden" id="pmpro_signup_shortcode" name="pmpro_signup_shortcode" value="1" />
<?php do_action( 'pmpro_signup_form_before_fields' ); ?>
<?php if ( ! empty( $current_user->ID ) ) { ?>
<p id="pmpro_account_loggedin">
<?php printf( __( 'You are logged in as <strong>%1$s</strong>. If you would like to use a different account for this membership, <a href="%2$s">log out now</a>.', 'pmprosus' ), $current_user->user_login, wp_logout_url( $_SERVER['REQUEST_URI'] ) ); ?>
</p>
<?php
} else {
?>
<?php if ( $short !== 'emailonly' ) { ?>
<div class="pmpro_checkout-field pmpro_checkout-field-username">
<label for="username"><?php _e( 'Username', 'pmprosus' ); ?></label>
<input id="username" name="username" type="text" class="input" size="30" value="<?php echo esc_attr( $username ); ?>"
<?php
if ( ! empty( $hidelabels ) ) {
?>
placeholder="<?php _e( 'Username', 'pmprosus' ); ?>"<?php } ?> />
</div>
<?php } ?>
<?php
if ( ! empty( $custom_fields ) ) {
do_action( 'pmpro_checkout_after_username' ); }
?>
<div class="pmpro_checkout-field pmpro_checkout-field-bemail">
<label for="bemail"><?php _e( 'E-mail Address', 'pmprosus' ); ?></label>
<input id="bemail" name="bemail" type="email" class="input" size="30" value="<?php echo esc_attr( $bemail ); ?>"
<?php
if ( ! empty( $hidelabels ) ) {
?>
placeholder="<?php _e( 'E-mail Address', 'pmprosus' ); ?>"<?php } ?> />
</div>
<?php if ( ! empty( $short ) ) { ?>
<input type="hidden" name="bconfirmemail_copy" value="1" />
<?php } else { ?>
<div class="pmpro_checkout-field pmpro_checkout-field-bconfirmemail">
<label for="bconfirmemail"><?php _e( 'Confirm E-mail', 'pmprosus' ); ?></label>
<input id="bconfirmemail" name="bconfirmemail" type="email" class="input" size="30" value=""
<?php
if ( ! empty( $hidelabels ) ) {
?>
placeholder="<?php _e( 'Confirm E-mail', 'pmprosus' ); ?>"<?php } ?> />
</div>
<?php } ?>
<?php if ( $short !== 'emailonly' ) { ?>
<div class="pmpro_checkout-field pmpro_checkout-field-password">
<label for="password"><?php _e( 'Password', 'pmprosus' ); ?></label>
<input id="password" name="password" type="password" class="input" size="30" value=""
<?php
if ( ! empty( $hidelabels ) ) {
?>
placeholder="<?php _e( 'Password', 'pmprosus' ); ?>"<?php } ?> />
</div>
<?php } ?>
<?php if ( ! empty( $short ) ) { ?>
<input type="hidden" name="password2_copy" value="1" />
<?php } else { ?>
<div class="pmpro_checkout-field pmpro_checkout-field-password2">
<label for="password2"><?php _e( 'Confirm Password', 'pmprosus' ); ?></label>
<input id="password2" name="password2" type="password" class="input" size="30" value=""
<?php
if ( ! empty( $hidelabels ) ) {
?>
placeholder="<?php _e( 'Confirm Password', 'pmprosus' ); ?>"<?php } ?> />
</div>
<?php } ?>
<?php
if ( ! empty( $custom_fields ) ) {
do_action( 'pmpro_checkout_after_password' ); }
?>
<input type="hidden" name="pmprosus_referrer" value="<?php echo esc_attr( $_SERVER['REQUEST_URI'] ); ?>" />
<?php
if ( $redirect == 'referrer' ) {
$redirect_to = $_SERVER['REQUEST_URI'];
} elseif ( $redirect == 'account' ) {
$redirect_to = get_permalink( $pmpro_pages['account'] );
} elseif ( empty( $redirect ) ) {
$redirect_to = '';
} else {
$redirect_to = $redirect;
}
?>
<input type="hidden" name="redirect_to" value="<?php echo esc_attr( $redirect_to ); ?>" />
<?php
if ( ! empty( $custom_fields ) ) {
do_action( 'pmpro_checkout_after_email' ); }
?>
<div class="pmpro_hidden">
<label for="fullname"><?php _e( 'Full Name', 'pmprosus' ); ?></label>
<input id="fullname" name="fullname" type="text" class="input" size="30" value="" /> <strong><?php _e( 'LEAVE THIS BLANK', 'pmprosus' ); ?></strong>
</div>
<?php
global $recaptcha, $recaptcha_publickey;
if ( $recaptcha == 2 || ( ! empty( $level ) && $recaptcha == 1 && pmpro_isLevelFree( pmpro_getLevel( $level ) ) ) ) {
?>
<div class="pmpro_checkout-field pmpro_captcha">
<?php echo pmpro_recaptcha_get_html( $recaptcha_publickey, null, true ); ?>
</div> <!-- end pmpro_captcha -->
<?php } ?>
<?php } ?>
<?php do_action( 'pmpro_checkout_after_user_fields' ); ?>
<?php
if ( $checkout_boxes && function_exists( 'pmprorh_pmpro_checkout_boxes' ) ) {
pmprorh_pmpro_checkout_boxes(); }
?>
<?php
if ( ! empty( $tospage ) ) {
$tospage = get_post( $tospage );
?>
<input type="checkbox" name="tos" value="1" id="tos" /> <label class="pmpro_label-inline pmpro_clickable" for="tos"><?php printf( __( 'I agree to the %s', 'paid-memberships-pro' ), $tospage->post_title ); ?></label>
<?php
}
?>
<?php
if ( ! empty( $custom_fields ) ) {
do_action( 'pmpro_signup_form_before_submit' ); }
?>
<div class="pmpro_submit">
<span id="pmpro_submit_span">
<input type="hidden" name="submit-checkout" value="1" />
<input id="pmpro_btn-submit" type="submit" class="pmpro_btn pmpro_btn-submit-checkout" value="<?php echo $submit_button; ?>" />
</span>
</div>
<?php do_action( 'pmpro_signup_form_after_submit' ); ?>
<?php if ( ! empty( $login ) && empty( $current_user->ID ) ) { ?>
<div class="login-link" style="text-align:center;">
<a href="<?php echo wp_login_url( get_permalink() ); ?>"><?php _e( 'Log In', 'pmprosus' ); ?></a>
</div>
<?php } ?>
</div> <!-- end pmpro_checkout-fields -->
</div> <!-- end pmpro_checkout -->
</form>
<?php do_action( 'pmpro_signup_form_after_form' ); ?>
<?php } ?>
<?php
$temp_content = ob_get_contents();
ob_end_clean();
// Set the global level back to the correct object.
$pmpro_level = $pmpro_level_backup;
return $temp_content;
}
add_shortcode( 'my_pmpro_signup', 'my_pmpro_signup_shortcode' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment