5

I want to create a user via a standalone script. I want an activation email to be sent out, in the same way that it is when a user is created from the Add User screen. An email is set out with a link to create the password. There is no password in the email.

There seem to be various functions for creating new users.

There's also

  • wp-admin/user-new.php

Below is the code I have. It creates a new user, but it isn't the notification email. I've checked that wp-mail() and php mail() are working correctly.

I'm not sure that this is the right direction. I feel there might be an easier way to do it. If this is the right direction, any pointers on why the notification is not being sent?

Thanks.

<?php
define( 'SHORTINIT', true );
require_once( '/var/www/html/mysite/wp-load.php' );
require_once ('/var/www/html/mysite/wp-includes/user.php');
require_once ('/var/www/html/mysite/wp-includes/formatting.php');
require_once ('/var/www/html/mysite/wp-includes/capabilities.php');
require_once ('/var/www/html/mysite/wp-includes/pluggable.php');
require_once ('/var/www/html/mysite/wp-includes/kses.php');
require_once ('/var/www/html/mysite/wp-includes/meta.php');
function __() {}
wp_create_user ( 'testuser8', 'apsswd', '[email protected]' );
wp_new_user_notification ( testuser8,null,'both' );
?>

2 Answers 2

7

You should read the codex page re wp_create_user.

You don't describe the context in which your code runs. You shouldn't need all those require_once calls.

Anyhow, in this line wp_new_user_notification ( testuser8,null,'both' ); what is testuser8 ? It's not a variable, it's not a string, it's just some text that probably throws an error.

Try:

$user_id = wp_create_user ( 'testuser8', 'apsswd', '[email protected]' );
if( is_wp_error( $user_id ) ) 
    var_dump( $user_id );
else
    wp_new_user_notification ( $user_id, null,'both' );
2
  • Thanks for the help. I did have the username in quotes in my tests, but as you point out I was passing in the username, not the id. I would like not to have all of the require_once calls, and in fact I've had to add some more to get it working. The script is run outside of the WP environment. It's a standalone script on the server. I haven't seen any description of how to load the entire environment, so I added them one-by-one as the script was failing with undefined function calls. Oct 24, 2015 at 21:24
  • I've got rid of the require_once calls by using this, instead define('WP_USE_THEMES', false); require('/var/www/html/andaluciamia/wp-blog-header.php'); Oct 24, 2015 at 21:54
2

Use WP-CLI's User Create and put in a script.

wp user create - Create a user.

$ wp user create testuser8 [email protected] --user_pass=apsswd--role=author --send-email
Success: Created user 3.

Your Answer

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

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