你好,我发现了一个自定义的形式WordPress的,我想通过一些输入(街道,邮政编码,ustid,国家,电话号码)扩展。默认情况下,wp_insert_user()只能有特定的键(如user_login、user_pass.)。因此,需要使用update_user_Meta()来更新像地址和USt-iD这样的账单/shippindadress输入(如果有更简单的方法,我希望得到答案):
function red_registration_form($atts) {
$atts = shortcode_atts( array(
'role' => 'subscriber',
), $atts, 'register' );
$role_number = $atts["role"];
if ($role_number == "shop_manager" ) { $reg_form_role = (int) filter_var(AUTH_KEY, FILTER_SANITIZE_NUMBER_INT); } elseif ($role_number == "customer" ) { $reg_form_role = (int) filter_var(SECURE_AUTH_KEY, FILTER_SANITIZE_NUMBER_INT); } elseif ($role_number == "contributor" ) { $reg_form_role = (int) filter_var(NONCE_KEY, FILTER_SANITIZE_NUMBER_INT); } elseif ($role_number == "author" ) { $reg_form_role = (int) filter_var(AUTH_SALT, FILTER_SANITIZE_NUMBER_INT); } elseif ($role_number == "editor" ) { $reg_form_role = (int) filter_var(SECURE_AUTH_SALT, FILTER_SANITIZE_NUMBER_INT); } elseif ($role_number == "administrator" ) { $reg_form_role = (int) filter_var(LOGGED_IN_SALT, FILTER_SANITIZE_NUMBER_INT); } else { $reg_form_role = 1001; }
if(!is_user_logged_in()) {
$registration_enabled = get_option('users_can_register');
if($registration_enabled) {
$output = red_registration_fields($reg_form_role);
} else {
$output = __('<p>User registration is not enabled</p>');
}
return $output;
} $output = __('<p>You already have an account on this site, so there is no need to register again.</p>');
return $output;
}
add_shortcode('register', 'red_registration_form');
function red_registration_fields($reg_form_role) { ?>
<?php
ob_start();
?>
<form id="red_registration_form" class="red_form" action="" method="POST">
<?php red_register_messages(); ?>
<p>
<label for="red_user_login"><?php _e('Username'); ?></label>
<input name="red_user_login" id="red_user_login" class="red_input" placeholder="Benutzername" type="text"/>
</p>
<p>
<label for="red_user_company"><?php _e('Firma'); ?></label>
<input name="red_user_company" id="red_user_company" class="red_input" placeholder="Firma" type="text" required/>
</p>
<p>
<label for="red_user_email"><?php _e('Email'); ?></label>
<input name="red_user_email" id="red_user_email" class="red_input" placeholder="Email" type="email" required/>
</p>
<p>
<label for="red_user_first"><?php _e('First Name'); ?></label>
<input name="red_user_first" id="red_user_first" type="text" placeholder="Vorname" class="red_input" />
</p>
<p>
<label for="red_user_last"><?php _e('Last Name'); ?></label>
<input name="red_user_last" id="red_user_last" type="text" placeholder="Nachname" class="red_input"/>
</p>
<p>
<label for="red_user_street"><?php _e('Street'); ?></label>
<input name="red_user_street" id="red_user_street" type="text" placeholder="Straße" class="red_input" required/>
</p>
<p>
<label for="red_user_postal_code"><?php _e('Postal Code'); ?></label>
<input name="red_user_postal_code" id="red_user_postal_code" type="number" placeholder="Postleitzahl" class="red_input" required/>
</p>
<p>
<label for="red_user_ustid"><?php _e('USt-ID'); ?></label>
<input name="red_user_ustid" id="red_user_ustid" type="text" placeholder="USt-ID Nr" class="red_input" required/>
</p>
<p>
<label for="red_user_website"><?php _e('Website'); ?></label>
<input name="red_user_website" id="red_user_website" type="text" placeholder="Webseite" class="red_input"/>
</p>
<p>
<label for="red_user_phonenumber"><?php _e('Phonenumber'); ?></label>
<input name="red_user_phonenumber" id="red_user_phonenumber" type="tel" placeholder="Telefonnummer" class="red_input"/>
</p>
<p>
<label for="password"><?php _e('Password'); ?></label>
<input name="red_user_pass" id="password" class="red_input" placeholder="Password" type="password" required/>
</p>
<p>
<label for="password_again"><?php _e('Password'); ?></label>
<input name="red_user_pass_confirm" id="password_again" placeholder="Password Again" class="red_input" type="password" required/>
</p>
<p>
<input type="hidden" name="red_csrf" value="<?php echo wp_create_nonce('red-csrf'); ?>"/>
<input type="hidden" name="red_role" value="<?php echo $reg_form_role; ?>"/>
<input type="submit" value="<?php _e('Register Now'); ?>"/>
</p>
</form>
<?php
return ob_get_clean();
}
function red_add_new_user() {
if (isset( $_POST["red_user_login"] ) && wp_verify_nonce($_POST['red_csrf'], 'red-csrf')) {
$user_login = sanitize_user($_POST["red_user_login"]);
$user_company = sanitize_text_field( $_POST["red_user_company"]);
$user_email = sanitize_email($_POST["red_user_email"]);
$user_first = sanitize_text_field( $_POST["red_user_first"] );
$user_last = sanitize_text_field( $_POST["red_user_last"] );
$user_street = sanitize_text_field( $_POST["red_user_street"]);
$user_postal_code = sanitize_text_field( $_POST["red_user_postal_code"]);
$user_country = sanitize_text_field( $_POST["red_user_country"]);
$user_ustid = sanitize_text_field( $_POST["red_user_ustid"]);
$user_website = sanitize_text_field( $_POST["red_user_website"]);
$user_phone = sanitize_text_field( $_POST["red_user_phonenumber"]);
$user_pass = $_POST["red_user_pass"];
$pass_confirm = $_POST["red_user_pass_confirm"];
$red_role = sanitize_text_field( $_POST["red_role"] );
if ($red_role == (int) filter_var(AUTH_KEY, FILTER_SANITIZE_NUMBER_INT) ) { $role = "shop_manager"; } elseif ($red_role == (int) filter_var(SECURE_AUTH_KEY, FILTER_SANITIZE_NUMBER_INT) ) { $role = "customer"; } elseif ($red_role == (int) filter_var(NONCE_KEY, FILTER_SANITIZE_NUMBER_INT) ) { $role = "contributor"; } elseif ($red_role == (int) filter_var(AUTH_SALT, FILTER_SANITIZE_NUMBER_INT) ) { $role = "author"; } elseif ($red_role == (int) filter_var(SECURE_AUTH_SALT, FILTER_SANITIZE_NUMBER_INT) ) { $role = "editor"; } elseif ($red_role == (int) filter_var(LOGGED_IN_SALT, FILTER_SANITIZE_NUMBER_INT) ) { $role = "administrator"; } else { $role = "subscriber"; }
if(username_exists($user_login)) {
red_errors()->add('username_unavailable', __('Dieser Benutzername ist bereits vergeben'));
}
if(!validate_username($user_login)) {
red_errors()->add('username_invalid', __('Ungültiger Benutzername'));
}
if($user_login == '') {
red_errors()->add('username_empty', __('Bitte geben sie einen Benutzernamen ein'));
}
if($user_company == '') {
red_errors()->add('company_empty', __('Bitte geben sie eine Firma ein'));
}
if($user_street == '') {
red_errors()->add('street_empty', __('Bitte geben sie eine Straße ein'));
}
if($user_postal_code == '') {
red_errors()->add('postal_code_empty', __('Bitte geben sie eine Postleitzahl ein'));
}
if($user_country == '') {
red_errors()->add('country_empty', __('Bitte geben sie ein Land ein'));
}
if($user_ustid == '') {
red_errors()->add('ustid_empty', __('Bitte geben sie eine USt-ID Nr ein'));
}
if(!is_email($user_email)) {
red_errors()->add('email_invalid', __('Ungültige E-Mail'));
}
if(email_exists($user_email)) {
red_errors()->add('email_used', __('E-Mail bereits vergeben'));
}
if($user_pass == '') {
red_errors()->add('password_empty', __('Bitte geben sie ein Passwort ein'));
}
if($user_pass != $pass_confirm) {
red_errors()->add('password_mismatch', __('Passwörter sind nicht identisch'));
}
$errors = red_errors()->get_error_messages();
if(empty($errors)) {
$new_user_id = wp_insert_user(array(
'user_login' => $user_login,
'user_pass' => $user_pass,
'user_email' => $user_email,
'first_name' => $user_first,
'last_name' => $user_last,
'user_url' => $user_website,
'user_registered' => date('Y-m-d H:i:s'),
'role' => $role
)
);
wp_update_user( array ('ID' => $new_user_id));
global $current_user;
$user = wp_get_current_user();
$user_id = $user->ID;
// $user_id = get_current_user_id();
// Checking if user Id exist
if( !empty( $user_id ) ) {
// Test
update_user_meta( $user_id, 'billing_first_name', $user_first );
update_user_meta( $user_id, 'billing_last_name', $user_last );
update_user_meta( $user_id, 'billing_email', $user_email );
// Test
update_user_meta( $user_id, 'shipping_first_name', $user_first );
update_user_meta( $user_id, 'shipping_last_name', $user_last );
update_user_meta( $user_id, 'shipping_email', $user_email );
// Test update_user_meta
update_user_meta( $user_id, 'billing_address_1', $user_street );
// Test update_user_meta
update_user_meta( $user_id, 'shipping_city', 'Bangalore' );
} else {
echo 'User doesnt exist';
}
if($new_user_id) {
wp_new_user_notification($new_user_id);
wp_set_auth_cookie(get_user_by( 'email', $user_email )->ID, true);
wp_set_current_user($new_user_id, $user_login);
do_action('wp_login', $user_login, wp_get_current_user());
wp_redirect(home_url()); exit;
}
}
}
}
add_action('init', 'red_add_new_user');
function red_errors(){
static $wp_error;
return isset($wp_error) ? $wp_error : ($wp_error = new WP_Error(null, null, null));
}
function red_register_messages() {
if($codes = red_errors()->get_error_codes()) {
echo '<div class="red_errors">';
foreach($codes as $code){
$message = red_errors()->get_error_message($code);
echo '<span class="error"><strong>' . __('Error') . '</strong>: ' . $message . '</span><br/>';
}
echo '</div>';
}
}
我的目标是,每次创建一个用户时,账单/送货地址也会更新。wp_isert_user()可以正常工作,但是一旦创建了用户,带有update_user_Meta()的发货/账单地址字段就不会出现在用户信息中。我做错了什么?谢谢你的帮助提前。
1条答案
按热度按时间eqfvzcg81#
在您的代码中,
update_user_meta()
无法工作,因为您正在尝试使用全局当前用户ID,该ID还不存在,因为Auth cookie尚未设置。有一些错误,不必要的代码和丢失的东西。
您可以使用
meta_input
参数和wp_insert_user()
函数参数来添加所有所需的用户元数据,如下所示:应该可以的