WordPress.org

Ready to get started?Download WordPress

Forums

Theme My Login
[resolved] Email as username (11 posts)

  1. dhunink
    Member
    Posted 1 year ago #

    TML has a beautiful option where one can login with e-mailadress instead of a username. It would be logical though, to extend it to the registration form: when the module is activated, don't aks for a username but just use the e-mailadress as username.

    I did it myself by using jQuery to copy the value of the e-mail field to a hidden field called user_login. But that's just a dirty workaround and not really how it should be.

    Any thoughts, is it something that could be easily done by plugin users themselves, or will it be part of the plugin in the future?

    http://wordpress.org/extend/plugins/theme-my-login/

  2. Jeff Farthing
    Member
    Plugin Author

    Posted 1 year ago #

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

  3. dhunink
    Member
    Posted 1 year ago #

    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;
    }

  4. Jeff Farthing
    Member
    Plugin Author

    Posted 1 year ago #

    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' );
  5. dhunink
    Member
    Posted 1 year ago #

    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);

  6. Jeff Farthing
    Member
    Plugin Author

    Posted 1 year ago #

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

  7. ChrisJDean
    Member
    Posted 1 year ago #

    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');
  8. uzegonemad
    Member
    Posted 8 months ago #

    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.

  9. andreylima
    Member
    Posted 7 months ago #

    What's the real solution for this?

  10. esmi
    Forum Moderator
    Posted 7 months ago #

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

  11. Spitfirehd
    Member
    Posted 5 months ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic