Skip to content

Instantly share code, notes, and snippets.

@raamdev
Created September 9, 2014 23:48
Show Gist options
  • Select an option

  • Save raamdev/7cd2d1bcfc70e42a2721 to your computer and use it in GitHub Desktop.

Select an option

Save raamdev/7cd2d1bcfc70e42a2721 to your computer and use it in GitHub Desktop.
s2Member Pro-Form customizations that hide the username field and then auto-generate a username based on the email address. This was used for the Paid Journal subscription I ran on raamdev.com between 2011 and 2014.
<?php
add_action ("login_head", "s2_customize_login", 1000);
function s2_customize_login ()
{
?>
<script type = "text/javascript">
function getParameterByName(name)
{
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if(results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
(function ($) /* Wraps this `$ = jQuery` routine. */
{
$.fn.swapWith = function (to) /* Utility extension for jQuery. */
{
return this.each (function ()
{
var $to = $ (to).clone (true), $from = $ (this).clone (true);
$(to).replaceWith ($from), $ (this).replaceWith ($to);
});
};
/**/
$(document).ready (function () /* Handles email-to-username on keyup. */
{
/* If this is the register page, customize it */
if(getParameterByName('action') == 'register'){
/* Generate random number to append to username,
hopefully making it unique (yes, this isn't perfect!) */
var randomNum = Math.ceil(Math.random()*999);
var email = 'input#user_email';
var login = 'input#user_login';
var firstname = 'input#ws-plugin--s2member-custom-reg-field-first-name';
var lastname = 'input#ws-plugin--s2member-custom-reg-field-last-name';
$('p.message').text("Thank you for your payment! Please complete your subscription below:");
$('div.ws-plugin--s2member-custom-reg-field-divider-section').hide();
$('#reg_passmail').hide();
$('#nav').hide();
$('#backtoblog').hide();
$('#wp-submit').val('Submit');
$(login).closest ('p').hide();
$(email).closest ('p').swapWith ($ (firstname).closest ('p'));
$(email).closest ('p').swapWith ($ (lastname).closest ('p'));
$(firstname).focus();
$(email).attr('tabindex', 50);
/* Fill hidden username field with first part of email address
and append randomNum to hopefully make it unique. */
$ (email).keyup (function ()
{
$(login).val ($.trim ($ (email).val ().split (/@/)[0].replace (/[^\w]/gi, '')) + randomNum.toString());
});
}
});
}) (jQuery);
</script>
<?php
}
add_action ("ws_plugin__s2member_pro_before_sc_paypal_form", "s2_customize_pro_form", 1000);
function s2_customize_pro_form ($vars = array())
{
/* Don't pre populate when/if the Pro Form has just been POSTd, and/or if it's a Modification Form
with modify="1", because s2Member already pre populates Pro Forms in that scenario, with the
User's existing Email and Username. */
if(empty ($_POST["s2member_pro_paypal_checkout"]) && empty($vars["attr"]["modify"])) {
?>
<script type = "text/javascript">
function getParameterByName(name)
{
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if(results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
(function ($) /* Wraps this `$ = jQuery` routine. */
{
$.fn.swapWith = function (to) /* Utility extension for jQuery. */
{
return this.each (function ()
{
var $to = $ (to).clone (true), $from = $ (this).clone (true);
$(to).replaceWith ($from), $ (this).replaceWith ($to);
});
};
/**/
$(document).ready (function () /* Handles email-to-username on keyup. */
{
/* Generate random number to append to username,
hopefully making it unique (yes, this isn't perfect!) */
var randomNum = Math.ceil(Math.random()*999);
if($('input#s2member-pro-paypal-checkout-email').length) {
var email = 'input#s2member-pro-paypal-checkout-email';
var login = 'input#s2member-pro-paypal-checkout-username';
$('#s2member-pro-paypal-checkout-form-username-div').hide();
$('#s2member-pro-paypal-checkout-form-description-section').hide();
//$('div.s2member-pro-paypal-checkout-form-registration-section-title').text("Subscribe to Raam's Journal ($7/month)");
if ( $('#pro-form-title').length ){ pro_form_title = $('#pro-form-title').html(); }
//else { pro_form_title = "Raam's Journal Subscription"; }
$('#s2member-pro-paypal-checkout-form-registration-section-title').text(pro_form_title);
} else {
var email = 'input#s2member-pro-paypal-registration-email';
var login = 'input#s2member-pro-paypal-registration-username';
$('#s2member-pro-paypal-registration-form-username-div').hide();
$('#s2member-pro-paypal-registration-form-description-section').hide();
$('div.s2member-pro-paypal-registration-form-registration-section-title').text("Complimentary Journal Subscription");
}
/* Fill hidden username field with first part of email address
and append randomNum to hopefully make it unique. */
$ (email).keyup (function ()
{
$(login).val ($.trim ($ (email).val ().split (/@/)[0].replace (/[^\w]/gi, '')) + randomNum.toString());
});
});
}) (jQuery);
</script>
<?php
}
}
add_action ("ws_plugin__s2member_pro_before_sc_paypal_form", "s2_customize_modify_pro_form", 1000);
function s2_customize_modify_pro_form ($vars = array())
{
/* Only apply changes if it's a Modification Form with modify="1" */
if(!empty($vars["attr"]["modify"])) {
?>
<script type = "text/javascript">
(function ($) /* Wraps this `$ = jQuery` routine. */
{
$(document).ready (function () /* Handles email-to-username on keyup. */
{
$('#s2member-pro-paypal-checkout-form-username-div').hide();
$('#s2member-pro-paypal-checkout-form-description-section').hide();
if ( $('#pro-form-title').length ){ pro_form_title = $('#pro-form-title').html(); }
$('#s2member-pro-paypal-checkout-form-registration-section-title').text(pro_form_title);
});
}) (jQuery);
</script>
<?php
}
}
add_action ("ws_plugin__s2member_pro_before_sc_paypal_form", "s2_customize_sp_pro_form", 1000);
function s2_customize_sp_pro_form ($vars = array())
{
?>
<script type = "text/javascript">
(function ($) /* Wraps this `$ = jQuery` routine. */
{
$(document).ready (function () /* Handles email-to-username on keyup. */
{
$('#s2member-pro-paypal-sp-checkout-form-description-div').hide();
if ( $('#pro-form-title').length ){ pro_form_title = $('#pro-form-title').html(); }
$('#s2member-pro-paypal-sp-checkout-form-registration-section-title').text(pro_form_title);
});
}) (jQuery);
</script>
<?php
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment