3

So, here is some code I came up with -- It works, but now I need it to send the username and password it generated to that email address automatically ?? Also the first and last name isn't recording ?? Thanks!!

require('wordpress/wp-blog-header.php');

$user_email = trim(isset($_POST['payer_email']) ? $_POST['payer_email'] : "");
$user_name = trim(isset($_POST['first_name']) ? $_POST['first_name'] : "");

$user_id = username_exists( $user_name );
if ( !$user_id ) {
    $random_password = wp_generate_password( 12, false );
    $user_id = wp_create_user( $user_name, $random_password, $user_email );
} else {
    $random_password = __('User already exists.  Password inherited.');
}

4 Answers 4

5

You can use this: wp_new_user_notification( $user_id, $random_password);

2
  • 2
    "This answer was automatically flagged as low-quality because of its length and content", which I am sure you don't want. Please explain your answer, rather than simply post code.
    – s_ha_dum
    Commented Jun 16, 2013 at 3:29
  • More information on this: codex.wordpress.org/Function_Reference/wp_new_user_notification Notify the blog admin of a new user, normally via email and also send an email with login/password to the new user.
    – bryceadams
    Commented Feb 8, 2015 at 10:39
3

There's the user_register hook, which runs at the end of user insertion and has $user_id as a parameter, you could leverage that to send the username and password, but you have to update_user_meta() before you can get first name and stuff, so it might be better to do it at the end of the user creation and usermeta insertion.

The only thing creating a user does is insert the user into the DB, you need to use update_user_meta() to add the user values. get_the_author_meta() has a list of the default usermeta values, if you insert something with the key of one of those, it will show up in profile.php by default, otherwise you will have to extend the user profile to display it.

Also, just a note, it seems as though you're setting $random_password to a non-random value and also, probably more importantly, setting it AFTER it's been used.

0

I would use wp_insert_user instead of wp_create_user so you can add as much user data as you like in the first instance. Check the Wordpress codex for all that you can insert. (wp_create_user simply calls this function anyway!) To send out the user login details you can call wp_mail($user_email, $subject, $message); Of course you will want to set $message to include the username and password in the body of the message text. Another thing, I think you can create the function wp_new_user_notification which is a pluggable function and is called on new user registration, you can put the mail function in that, I know plugins such as Register Plus Redux and Pie Register do it this way.

0

I Think this is you are looking for. This will add first name and last name to user meta and send a mail to user about first name and last name addition.

 add_action('user_register', 'add_user_custom_meta');

function add_user_custom_meta($user_id) {
    $user_info=get_userdata( $user_id );
if ( isset( $_POST['first_name'] ) )
    update_user_meta($user_id, 'first_name', $_POST['first_name']);

if ( isset( $_POST['last_name'] ) )
    update_user_meta($user_id, 'last_name', $_POST['last_name']);

wp_mail($user_info->user_email, 'User first and last name', sprintf('Hi we have added your first name :-% and last name:- % to our site.',$_POST['first_name'],$_POST['last_name']));
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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