На сайте есть плагин со своей системой регистрации, я хочу добавить мета данные при регистрации (телефон и дату рождения). При регистрации данные валидацию проходят, но в БД их нет. В JS файл так же добавил эти переменные. Пробовал разные варианты:
1)
add_user_meta( get_current_user_id(), 'user_birthday', $user_birthday);
add_user_meta( get_current_user_id(), 'user_phone', $user_phone);
2)
add_user_meta( $user['ID'], 'user_birthday', $user_birthday);
add_user_meta( $user['ID'], 'user_phone', $user_phone);
3) Через экшены personal_options_update и edit_user_profile_update, ну и 4й который в коде
Ни один из вышеперечисленных вариантов не подошёл. Вот кусок кода с методами отвечающими за регистрацию:
public static function stm_lms_register_custom()
{
check_ajax_referer('stm_lms_register_custom', 'nonce');
$r = array(
'message' => '',
'status' => 'error'
);
$recaptcha_passed = STM_LMS_Helpers::check_recaptcha();
if (!$recaptcha_passed) {
$r['message'] = esc_html__('CAPTCHA verification failed.', 'masterstudy-lms-learning-management-system');
wp_send_json($r);
die;
}
$fields = array(
'user_login' => array(
'label' => esc_html__('Login', 'masterstudy-lms-learning-management-system'),
'type' => 'text'
),
'user_email' => array(
'label' => esc_html__('E-mail', 'masterstudy-lms-learning-management-system'),
'type' => 'email'
),
'user_password' => array(
'label' => esc_html__('Password', 'masterstudy-lms-learning-management-system'),
'type' => 'text'
),
'user_password_re' => array(
'label' => esc_html__('Password confirm', 'masterstudy-lms-learning-management-system'),
'type' => 'text'
),
'user_birthday' => array(
'label' => esc_html__('Date of Birth', 'masterstudy-lms-learning-management-system'),
'type' => 'date'
),
'user_phone' => array(
'label' => esc_html__('Phone', 'masterstudy-lms-learning-management-system'),
'type' => 'tel'
),
'privacy_policy' => array(
'label' => esc_html__('Privacy Policy', 'masterstudy-lms-learning-management-system'),
'type' => 'text'
),
);
$request_body = file_get_contents('php://input');
$data = json_decode($request_body, true);
foreach ($fields as $field_key => $field) {
if (empty($data[$field_key])) {
$r['message'] = sprintf(esc_html__('%s field is required', 'masterstudy-lms-learning-management-system'), $field['label']);
wp_send_json($r);
die;
} else {
$data[$field_key] = STM_LMS_Helpers::sanitize_fields($data[$field_key], $field['type']);
if (empty($data[$field_key])) {
$r['message'] = sprintf(esc_html__('Please enter valid %s field', 'masterstudy-lms-learning-management-system'), $field['label']);
wp_send_json($r);
die;
}
}
}
extract($data);
/**
* @var $user_login ;
* @var $user_email ;
* @var $user_password ;
* @var $user_password_re ;
* @var $user_birthday ;
* @var $user_phone ;
*/
if (empty($user_phone)) {
$r['message'] = esc_html__('Phone', 'masterstudy-lms-learning-management-system');
wp_send_json($r);
die;
}
/*If password is equal*/
if ($user_password !== $user_password_re) {
$r['message'] = esc_html__('Passwords do not match', 'masterstudy-lms-learning-management-system');
wp_send_json($r);
die;
}
$premoderation = STM_LMS_Options::get_option('user_premoderation', false);
/*Now we have valid data*/
$userdata = array(
'user_login' => $user_login,
'user_email' => $user_email,
'user_pass' => $user_password,
);
$user = wp_insert_user($userdata);
add_user_meta( $user[get_current_user_id()], 'user_birthday', $user_birthday);
add_user_meta( $user[get_current_user_id()], 'user_phone', $user_phone);
if (is_wp_error($user)) {
$r['message'] = $user->get_error_message();
} else {
if ($premoderation) {
self::_handle_premoderation_custom($user, $data, $user_email);
$r['status'] = 'success';
$r['message'] = esc_html__('Please follow the instructions sent to your email address.', 'masterstudy-lms-learning-management-system');
} else {
self::_register_user_custom($user, $data, $user_email);
$r['status'] = 'success';
$r['user_page'] = STM_LMS_User::user_page_url($user, true);
$r['message'] = esc_html__('Registration completed successfully.', 'masterstudy-lms-learning-management-system');
}
}
wp_send_json($r);
}
static function _handle_premoderation_custom($user, $data, $user_email)
{
$token = bin2hex(openssl_random_pseudo_bytes(16));
/*Setting link for 3 days*/
set_transient($token, $data, 3 * 24 * 60 * 60);
/*Delete User first and save his data to transient*/
require_once ABSPATH . 'wp-admin/includes/ms.php';
wp_delete_user($user);
wpmu_delete_user($user);
$subject = sprintf(esc_html__('Activate your account on site %s', 'masterstudy-lms-learning-management-system'), get_bloginfo('name'));
$message = sprintf(
esc_html__(
'Please activate your account via this link - %s',
'masterstudy-lms-learning-management-system'
),
STM_LMS_User::login_page_url() . '?user_token=' . $token
);
STM_LMS_Helpers::send_email($user_email, $subject, $message);
}
static function verify_user_custom() {
$token = sanitize_text_field($_GET['user_token']);
$data = get_transient($token);
if(!empty($data)) {
extract($data);
/**
* @var $user_login ;
* @var $user_email ;
* @var $user_password ;
* @var $user_birthday ;
* @var $user_phone ;
*/
$userdata = array(
'user_login' => $user_login,
'user_email' => $user_email,
'user_pass' => $user_password,
);
$user = wp_insert_user($userdata);
add_user_meta( $user[get_current_user_id()], 'user_birthday', $user_birthday);
add_user_meta( $user[get_current_user_id()], 'user_phone', $user_phone);
if(!is_wp_error($user)) {
self::_register_user_custom($user, $data, $user_email);
}
}
wp_redirect(STM_LMS_User::login_page_url());
}
static function _register_user_custom($user, $data, $user_email)
{
wp_signon($data, is_ssl());
/*If everything is right, check for Instructor application*/
STM_LMS_Instructor::become_instructor($data, $user);
do_action('stm_lms_user_registered', $user, $data);
$subject = esc_html__('You successfully register on site.', 'masterstudy-lms-learning-management-system');
$message = sprintf(
esc_html__(
'Now you active user on site - %s. Add information and start chatting with other users - free and fast.',
'masterstudy-lms-learning-management-system'
), get_bloginfo('name')
);
STM_LMS_Helpers::send_email($user_email, $subject, $message);
wp_new_user_notification($user);
}}