Page 1 of 1

Email as Username

PostPosted: September 1st, 2011, 10:00 am
by bsowards
Searched the web for a few days on this one, no luck. You guys are awesome, so I figured I'd ask. :)

Is there a recommended way to make the username field for registration accept an email address?

Rather not deal with users trying to guess what username they should pick.

Hooks/function.php works for me as an approach.

Re: Email as Username

PostPosted: September 1st, 2011, 10:08 pm
by Cristián Lávaque
I imagine it should be possible, although I haven't done it myself. Have you searched the hooks related to registration? It won't just be a matter of populating the username with the email field, but also making it validate as a username. Here's a good reference for WP hooks: http://adambrown.info/p/wp_hooks

I hope that helps. :)

Re: Email as Username

PostPosted: September 2nd, 2011, 7:28 pm
by Jason Caldwell
Thanks for the heads up on this thread Cristián.

@bsowards: I would advise against trying to modify the WordPress core in this regard, because many areas of WordPress, and almost all plugins rely on a strict Username validation and sanitation, which would not expect nor allow characters found in email addresses, such as the @ symbol. This becomes even more of an issue in a Multisite Network installation of WordPress.

Instead, you might try pre-populating the Username field, via JavaScript. Perhaps using jQuery to fill the Username field with only the first part of the email address being typed in ( i.e. johndoe22@example.com ), would automatically populate the Username field with: johndoe22, and then the User would have the ability to change it if they like something better. This would need to be custom coded ( just a few lines ), and then connected via the Hook login_head for the WordPress core.

Re: Email as Username

PostPosted: September 2nd, 2011, 8:53 pm
by Jason Caldwell
I went ahead and finished this up for anyone that wants to use it.

Create this directory and file:
/wp-content/mu-plugins/email-to-username.php
Code: Select all
<?php
add_action 
("login_head", "s2_email_to_username", 1000);
function s2_email_to_username ()
    {
?>
        <script type = "text/javascript">
            (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. */
                        {
                            var email = 'input#user_email', login = 'input#user_login';
                            $(email).closest ('p').swapWith ($ (login).closest ('p')), $ (email).keyup (function ()
                                {
                                    $(login).val ($.trim ($ (email).val ().split (/@/)[0].replace (/[^\w]/gi, '')));
                                });
                        });
                }) (jQuery);
        </script>
<?php
    
}
?>
* Please make sure there are no extra spaces/tabs/line breaks before or after the <?php ?> tags.

Re: Email as Username

PostPosted: September 2nd, 2011, 8:58 pm
by bsowards
Thanks Jason, very cool, appreciate the code!

Re: Email as Username

PostPosted: September 2nd, 2011, 11:00 pm
by Cristián Lávaque
Very cool! :)

Re: Email as Username

PostPosted: September 4th, 2011, 1:54 am
by antseo
Hi Jason. I built out that dir and page but when I try to sign up as a member, I don't see the username as the email. What am I doing wrong?

Re: Email as Username

PostPosted: September 4th, 2011, 9:41 am
by bsowards
This doesn't make the email address the username. Per Jason's earlier comment, that isn't a good idea since so many plugins rely on the username to validate properly - and having an "@" in the username would violate it.

So... this code snippet is for auto-generating usernames instead. As of right now it doesn't appear to validate that the username is available. If we added that then you could have this script auto-generate a username, set username to "display:none," and then use the "Email as Login" plugin.

Effecctively, this would make the email the username as far as the user is concerned, without hacking Wordpress.

I think this will be my next plugin...

Re: Email as Username

PostPosted: September 4th, 2011, 10:46 am
by antseo
Thanks bsowards. so what are the steps then to making that script work using the s2member profile shortcode?

Re: Email as Username

PostPosted: September 4th, 2011, 11:12 am
by bsowards
You upload this into wp-content/mu-plugins/s2hacks.php but put it outside the <?php ?> tags.

Once i have this plugin built I'll upload the notification here, probably will take a few weeks.

Re: Email as Username

PostPosted: September 4th, 2011, 2:56 pm
by antseo
I didn't see an attachment. Are you referring to the script above?

So with regard to the username, I guess I don't why the email can't be used since it's a unique identifier?

Re: Email as Username

PostPosted: September 6th, 2011, 12:22 pm
by bsowards
Per Jason's comment, wordpress core doesn't allow it.

There's no attachment yet, I haven't made the plugin yet. :)

Re: Email as Username

PostPosted: September 6th, 2011, 12:32 pm
by antseo
okay, no worries. Thanks!

Re: Email as Username

PostPosted: September 14th, 2011, 10:40 am
by bsowards

Re: Email as Username

PostPosted: October 7th, 2011, 1:51 am
by sonora
I'm using a plugin in conjunction with s2member Pro to allow users to log in with either their email address OR their username:

http://wordpress.org/extend/plugins/wp-email-login/

Re: Email as Username

PostPosted: October 7th, 2011, 3:56 am
by Cristián Lávaque
Thanks for sharing that! :)

Re: Email as Username

PostPosted: October 8th, 2011, 4:03 pm
by antseo
nice! thanks!

Re: Email as Username

PostPosted: October 28th, 2011, 1:10 pm
by Raam Dev
I've modified Jason's 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.

You can see an example and grab my modified version of the script over here: viewtopic.php?f=4&t=15672&p=49082#p49082

Re: Email as Username

PostPosted: October 28th, 2011, 1:18 pm
by antseo
okay cool. I did notice when I tried to implement this plugin ... http://wordpress.org/extend/plugins/si- ... wordpress/ ... that the captcha isn't recognized on Submit action. Any idea why that would be?

Re: Email as Username

PostPosted: October 28th, 2011, 2:47 pm
by Jason Caldwell
@seozones @bsowards
Forum rank updated to Experienced User