Wordpress插入新用户而不是在后端添加

时间:2018-01-25 04:36:18

标签: php ajax wordpress

在我的开发网站上注册时,我似乎无法弄清楚这里有一个错误。我可以在注册时通过验证。但是当我使用提交按钮创建帐户时,似乎不想将任何信息记录到数据库中。我收到了登录消息,但是,我可以重新创建 同样超过广告。

add_action( 'wp_ajax_nopriv_ajaxregister', 'ajax_register' );
add_action( 'wp_ajax_ajaxregister', 'ajax_register' );

function ajax_register() {

    $title          = $_POST['titles'];
    $firstname      = $_POST['firstname'];
    $lastname       = $_POST['lastname'];
    $email          = $_POST['user_email'];
    $user_pass      = $_POST['user_pass'];
    $confirm_pass   = $_POST['confirm_pass'];
    $agree_tc       = $_POST['agree_tc'];
    $nationality    = $_POST['nationality'];
    $username       = $_POST['user_email'];//strtolower($firstname) . '-' . strtolower($lastname);
    $phone          = '+' . $_POST['country_code'] . $_POST['phone'];
    //$address        = $_POST['address'];

    if ($user_pass != $confirm_pass || !is_email($email) || $agree_tc != 'true') {
        echo json_encode(array('loggedin' => false, 'message' => __('There are some errors')));
    } else {

        $user = array(
            'user_login'    => $username,
            'user_pass'     => $user_pass,
            'first_name'    => $firstname,
            'last_name'     => $lastname,
            'user_email'    => $email,
            'role'          => 'client'
        );

        $user_id = wp_insert_user($user);

        if (is_wp_error($user_id)) {

            $error_string = $user_id->get_error_message();
            echo json_encode(array('loggedin' => false, 'message' => $error_string));

        } else {

            update_user_meta($user_id, 'user_num', $phone);
            update_user_meta($user_id, 'user_nat', $nationality);
            update_user_meta($user_id, 'display_titles',$title);
            //update_user_meta($user_id, 'user_address', $address);

            $creds = array(
                'user_login'    => $username,
                'user_password' => $user_pass
            );

            $user_signon = wp_signon($creds, false);
            echo json_encode(array('loggedin' => true, 'message' => __('Login successful, redirecting...')));

        }
    }

    die();
}

编辑。

标记完全正常,但是经过我的回购更改后。显示标题未被解析。

function ajax_register() {
    $title          = $_POST['titles'];
    $firstname      = $_POST['firstname'];
    $lastname       = $_POST['lastname'];
    $email          = $_POST['user_email'];
    $confirm_email  = $_POST['confirm_email'];
    $user_pass      = $_POST['user_pass'];
    $confirm_pass   = $_POST['confirm_pass'];
    $agree_tc       = $_POST['agree_tc'];
    $nationality    = $_POST['nationality'];
    $username       = $_POST['user_email'];
    $phone          = '+' . $_POST['country_code'] . $_POST['phone'];
    if ($user_pass != $confirm_pass || !is_email($email) || !is_numeric($_POST['phone']) || $agree_tc != 'true') {
        echo json_encode(array('loggedin' => false, 'message' => __('There are some errors')));
    } else {
        $user = array(
            'user_login'    => $username,
            'user_pass'     => $user_pass,
            'first_name'    => $firstname,
            'last_name'     => $lastname,
            'user_email'    => $email,
            'display_titles'=>$title, // THIS WAS MISSING 
            'role'          => 'client'
        );
        $user_id = wp_insert_user($user);
        if (is_wp_error($user_id)) {
            $error_string = $user_id->get_error_message();
            echo json_encode(array('loggedin' => false, 'message' => $error_string));
        } else {
            update_user_meta($user_id, 'user_num', $phone);
            update_user_meta($user_id, 'user_nat', $nationality);
            update_user_meta($user_id, 'display_titles',$title);
            //update_user_meta($user_id, 'user_address', $address);
            $creds = array(
                'user_login'    => $username,
                'user_password' => $user_pass
            );
            $user_signon = wp_signon($creds, false);
            echo json_encode(array('loggedin' => true, 'message' => __('Login successful, redirecting...')));
        }
    }
    die();
}

0 个答案:

没有答案