WordPress.org

Support

Support » Plugins and Hacks » [Resolved] Email as username

[Resolved] Email as username

Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Author Jeff Farthing

    @jfarthing84

    It can be done pretty easily with a filter. But I may consider putting it in a future version.

    Would be nice if implemented!

    For now, I assume you refer to a hook like user_registration_email. Which is actually badly documented; can’t find real examples to get this stuff working. The code below still results in the missing email error. Any thoughts?

    add_filter(‘user_registration_email’, ‘use_email_as_username’);
    function use_email_as_username($user_login){
    $user_email = $user_login;
    return $user_email;
    }

    Plugin Author Jeff Farthing

    @jfarthing84

    I think the simplest way would be to use the register_post action and manually set the username in the global POST array:

    function tml_register_post() {
    	if ( ! empty( $_POST['user_email'] ) ) {
    		$_POST['user_login'] = $_POST['user_email'];
    	} else {
    		/* Use something random to prevent the empty username error message
    		 * This won't be used anyway, since the email is empty
    		 */
    		$_POST['user_login'] = wp_generate_password( 16, false );
    	}
    }
    add_action( 'register_post', 'tml_register_post' );

    Hi Jeff,

    thanks a lot. I first came up with that hook, didn’t worked. So I thought I must had the wrong hook. But since you’re recommending it as well, I hang on to it and finally discovered what the problem is actually. Just to be precise, I pointed out my solution below. It works, but if you feel i’m wrong, please let me know.

    It turned out it works, when I just ad a hidden field to the registration form like this:
    <input type="hidden" name="user_login" id="user_login<?php $template->the_instance(); ?>" class="input" value="<?php echo rand();//WP needs some value ?>" size="20" />
    Without this input, or when the value is empty, the following happens:

    I’ve got this set-up as custom TML functions:

    add_action('register_form','show_this');
    add_action('register_post', 'tml_register_post');
    add_action('registration_errors','check_fields', 10, 3);
    add_action('user_register', 'register_extra_fields');

    On this time $errors is still empty:
    add_action( 'register_post', 'tml_register_post' );

    But when checked in this function, WP has gave the error alhough $_POST[‘user_login’] is perfectly set:
    add_action('registration_errors','check_fields', 10, 3);

    Plugin Author Jeff Farthing

    @jfarthing84

    A hidden field will work fine too. Just know that it is modifiable, though that shouldn’t be much of an issue.

    Just to expand a little on dhunink’s post

    The idea you have is right but this hook TAKES the email address not the user_login.

    So to make the filter work all you actually need to do is have a field named user_login in your form which you can label as email address and then put this in functions.php:

    function use_email_as_username($user_email)
    {
    	return $_POST['user_login'];
    }
    
    add_filter('user_registration_email', 'use_email_as_username');

    If you’re having issues with hooks as I was… You can throw this into the top of a plugin and just bypass WP hooks all together. You could probably put it it the top of your functions.php as well, but that’s untested.

    if(isset($_POST['user_login']) && isset($_POST['user_email']))
    {
        $_POST['user_login'] = $_POST['user_email'];
    }

    Note that WP will still attempt to validate the username using this hack. Since usernames don’t support + symbols, registration will fail if your email address is yourname+throwawayemail@example.com.

    Using a plugin to login with email address is probably safer.

    What’s the real solution for this?

    esmi

    @esmi

    Forum Moderator

    @andreylima: If you require assistance then, as per the Forum Welcome, please post your own topic.

    Thanks @uzegonemad and @dhunink. A combination of yours solved my problem.

    Just so that others now, I put uzegonemad suggestion into my functions.php file with the hidden field that dhunink wrote and Boom. It works nice like rice.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘[Resolved] Email as username’ is closed to new replies.