• Resolved Tegomass

    (@tegomass)


    Hey there,

    I tried to have the username as optional for my end users. To do so, I’ve hide the username field on the frontend.
    On my child theme functions.php file, I’ve added the wpmem_pre_register_data hook to generate a random username.
    I’ve logged the output using print_r(), username looks good (something like user_qsdfmlkjm) in the hook.

    add_filter( 'wpmem_pre_register_data', 'custom_set_username_field' );
    function custom_set_username_field( $fields ) {
        $random_username = 'user_' . wp_generate_password(8, false);
        $fields['username'] = $random_username;
        echo "<pre>"; print_r( $fields ); echo "</pre>";
        return $fields;
    }

    But when the user is save in the database, the username remains the one the user typed in the register form.

    Did I do something wrong? I’ve carefully read the documentation (really well done btw), from my understanding, this is the correct hook to use.

    Could you please give me some help?

    Thank you!

    • This topic was modified 1 year ago by Tegomass.
    • This topic was modified 1 year ago by Tegomass.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Tegomass

    (@tegomass)

    Found the solution myself!

    I’ve just replaced wpmem_pre_register_data by wpmem_pre_validate_form.

    I leave this topic here but mark it as resolved.

    Plugin Author Chad Butler

    (@cbutlerjr)

    There is an api function that will generate a username from the user’s email address. The function is based off of what WooCommerce does, but takes it a step further (and better, IMO). If WC is active, the function will use WC’s process, but if it’s not, it will split the email address, take the first part of it (before the “@”) as the email. But first, it will check if that’s a unique value (usernames must be unique in WP). If it’s not a unique value, it will add a number to it and recheck until it does find a unique value.

    Pass the email to it, and it will return the processed result:

    $username = wpmem_create_username_from_email( $email );

    api-users.php in wp-members/trunk/includes/api – WordPress Plugin Repository

    Thread Starter Tegomass

    (@tegomass)

    Hey Chad,

    I appreciate the time you took to share your knowledge of WordPress.

    Thank you!
    Regards

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Register Hook seems to be not working as expected (at least in my case)’ is closed to new replies.