I am looking for any built in function that when I add a user or a user wants to register, the username will be automatically prepended with a predefined text.

For example, if the user registers with abul, the username will be saved as tk_abul.

  • how do you save the user? – Vishwa May 9 at 7:06
  • I want to add user from WordPress dashboard – Akhtarujjaman Shuvo May 9 at 7:09

You can use pre_user_login filter to customize username when registered. Example.

add_filter( 'pre_user_login', 'wpse_customize_user' );

function wpse_customize_user( $username ) {
    return 'tk_' . $username;
  • This is working but it is returning double tk after register. ex: tk_tk_username – Akhtarujjaman Shuvo May 9 at 8:28

Try user_register hook. The following code wasn't tested.

add_action( 'user_register', 'my_nickname_prepend_function' );

function my_nickname_prepend_function( $user_id )
    if ( isset( $_POST['user_login'] ) )
        update_user_meta( $user_id, 'nickname', 'tk_' . $_POST['user_login'] );

Remember about user input validation.

Action Reference is at your service.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.