Wp中用户注册的自定义函数?

时间:2011-05-09 作者:Kathir

我有一个自定义的用户注册模板。我想使用函数插入用户。我可以直接调用wp_insert_user()函数吗?

1 个回复
SO网友:Bainternet

是的,你可以,但你需要包括registration.php 对于验证,例如:

//Need registration.php for data validation
require_once( ABSPATH . WPINC . \'/registration.php\');
$firstname = sanitize_text_field( $form_data[\'firstname\'] );
$lastname = sanitize_text_field( $form_data[\'lastname\'] );
$username = sanitize_text_field( $form_data[\'username\'] );
$email = sanitize_text_field( $form_data[\'email\'] );
//Add usernames we don\'t want used
$invalid_usernames = array( \'admin\' );
//Do username validation
$username = sanitize_user( $username );
if ( !validate_username( $username ) || in_array( $username, $invalid_usernames ) ) {
    echo \'Username is invalid.\';
}
if ( username_exists( $username ) ) {
    echo \'Username already exists.\';
}
//Do e-mail address validation
if ( !is_email( $email ) ) {
    echo \'E-mail address is invalid.\';
}
if (email_exists($email)) {
    echo \'E-mail address is already in use.\';
}
//Everything has been validated, proceed with creating the user
//Create the user
$user_pass = wp_generate_password();
$user = array(
    \'user_login\' => $username,
    \'user_pass\' => $user_pass,
    \'first_name\' => $firstname,
    \'last_name\' => $lastname,
    \'user_email\' => $email
    );
$user_id = wp_insert_user( $user );

/*Send e-mail to admin and new user - 
You could create your own e-mail instead of using this function*/
wp_new_user_notification( $user_id, $user_pass );

结束

相关推荐

获取在Functions.php中设置的变量,并在我的Custom Post模板中回显它们

在我的函数中设置了以下函数。php文件,以允许我的自定义帖子类型“Slideshow”工作。add_action( \'the_post\', \'paginate_slide\' ); function paginate_slide( $post ) { global $pages, $multipage, $numpages; if( is_single() && get_post_type() == \'lom_s