Page 1 of 1

User registration without username

PostPosted: October 26th, 2011, 6:42 pm
by Raam Dev
Hi All,

I use s2Member primarily for the Paypal -> AWeber integration. I'm creating a subscription-based email offering that requires my readers to subscribe via Paypal to receive premium email content. Once they pay through Paypal, s2Member automatically adds them to my AWeber email list so they receive my premium email content.

I'm trying to make this process as simple as possible for my readers. Since it's an email-based offering, they have no reason to login to my WordPress site. I don't mind the accounts being created for s2Member to handle Subscription Cancellations, etc., however when the registration form is presented to them they're required to enter a "Username", which adds confusion to the process.

I understand that WordPress requires a username to create their account, so I'm wondering if there's any way to automatically generate a username (perhaps based on their email address) and then hide that field on the registration form so that all they see is Email Address, First Name, and Last Name.

Any ideas?

Re: User registration without username

PostPosted: October 27th, 2011, 4:06 pm
by Cristián Lávaque
I'm afraid I don't know how to do this yet. I know there's a plugin for logging in with the email address, but am not sure if it affects registration too. http://wordpress.org/extend/plugins/wp-email-login/

Re: User registration without username

PostPosted: October 27th, 2011, 4:43 pm
by Jason Caldwell
Please see this thread. I built a custom hacks file that will accomplish this, and it seems to be working well for others. Goto: viewtopic.php?f=36&t=14806&p=33812#p33812

Re: User registration without username

PostPosted: October 28th, 2011, 1:08 pm
by Raam Dev
Jason Caldwell wrote:Please see this thread. I built a custom hacks file that will accomplish this, and it seems to be working well for others. Goto: viewtopic.php?f=36&t=14806&p=33812#p33812


Jason, that's brilliant -- thanks!

I've modified your script slightly to hide the username field and append a randomly generated number to the username (to reduce the chances of a user registering with a conflicting username; as noted in the code comments, this isn't a fool-proof solution.) I also hid some other elements that I didn't want to show on the registration page.

Image

For those looking to create a registration form with the username field hidden and the username automatically generated, here you go:

Create this directory and file:
/wp-content/mu-plugins/email-to-username.php
Code: Select all

<?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').hide();
                                $('div.ws-plugin--s2member-custom-reg-field-divider-section').hide();
                                $('#reg_passmail').hide();
                                $('#nav').hide();
                                $('#wp-submit').val('Subscribe');
                                $(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
    
}
?>



(This is based on Jason's version here: viewtopic.php?f=36&t=14806&p=33812#p33812)

Note that I also hid a few other elements on the page. You can delete those lines if you don't want to hide those elements.

Re: User registration without username

PostPosted: October 28th, 2011, 1:15 pm
by Jason Caldwell
Nice work! Thanks for sharing this.
~ Updated rank to Experienced User.

Re: User registration without username

PostPosted: October 30th, 2011, 12:46 am
by Cristián Lávaque
Very cool. :)