admin管理员组文章数量:1122832
I don't know how to add username field in registration page. There is no settings about this also. Used to be there is a setting about adding a username field but not now how could i add username field in woocommerce registration i already tried to code but it doesn't work. I tried to add the code like this;
add_action( 'woocommerce_register_form_start', 'custom_woocommerce_register_username' );
function custom_woocommerce_register_username() {
?>
<p class="form-row form-row-wide">
<label for="reg_username"><?php _e( 'Username', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="input-text" name="username" id="reg_username" value="<?php if ( ! empty( $_POST['username'] ) ) echo esc_attr( $_POST['username'] ); ?>" />
</p>
<?php
}
// Kullanıcı adı alanının zorunlu olmasını sağlama
add_filter( 'woocommerce_registration_errors', 'custom_woocommerce_registration_errors_username', 10, 3 );
function custom_woocommerce_registration_errors_username( $errors, $username, $email ) {
if ( isset( $_POST['username'] ) && empty( $_POST['username'] ) ) {
$errors->add( 'username_error', __( 'Username is required!', 'woocommerce' ) );
}
return $errors;
}
// Kayıt sırasında kullanıcı adını kaydetme
add_action( 'woocommerce_created_customer', 'custom_save_registration_fields_username' );
function custom_save_registration_fields_username( $customer_id ) {
if ( isset( $_POST['username'] ) ) {
wp_update_user( array( 'ID' => $customer_id, 'user_login' => sanitize_text_field( $_POST['username'] ) ) );
}
}
I don't know how to add username field in registration page. There is no settings about this also. Used to be there is a setting about adding a username field but not now how could i add username field in woocommerce registration i already tried to code but it doesn't work. I tried to add the code like this;
add_action( 'woocommerce_register_form_start', 'custom_woocommerce_register_username' );
function custom_woocommerce_register_username() {
?>
<p class="form-row form-row-wide">
<label for="reg_username"><?php _e( 'Username', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="input-text" name="username" id="reg_username" value="<?php if ( ! empty( $_POST['username'] ) ) echo esc_attr( $_POST['username'] ); ?>" />
</p>
<?php
}
// Kullanıcı adı alanının zorunlu olmasını sağlama
add_filter( 'woocommerce_registration_errors', 'custom_woocommerce_registration_errors_username', 10, 3 );
function custom_woocommerce_registration_errors_username( $errors, $username, $email ) {
if ( isset( $_POST['username'] ) && empty( $_POST['username'] ) ) {
$errors->add( 'username_error', __( 'Username is required!', 'woocommerce' ) );
}
return $errors;
}
// Kayıt sırasında kullanıcı adını kaydetme
add_action( 'woocommerce_created_customer', 'custom_save_registration_fields_username' );
function custom_save_registration_fields_username( $customer_id ) {
if ( isset( $_POST['username'] ) ) {
wp_update_user( array( 'ID' => $customer_id, 'user_login' => sanitize_text_field( $_POST['username'] ) ) );
}
}
Share
Improve this question
edited Oct 7, 2024 at 4:07
rudtek
6,3535 gold badges30 silver badges52 bronze badges
asked Oct 7, 2024 at 1:38
obalotaobalota
1
1 Answer
Reset to default 0// Add Username Field to Registration Form
add_action( 'woocommerce_register_form_start', 'custom_woocommerce_register_username' );
function custom_woocommerce_register_username() {
?>
<p class="form-row form-row-wide">
<label for="reg_username"><?php _e( 'Username', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="input-text" name="username" id="reg_username" value="<?php echo ( ! empty( $_POST['username'] ) ) ? esc_attr( wp_unslash( $_POST['username'] ) ) : ''; ?>" />
</p>
<?php
}
// Validate Username Field
add_filter( 'woocommerce_registration_errors', 'custom_woocommerce_registration_errors_username', 10, 3 );
function custom_woocommerce_registration_errors_username( $errors, $username, $email ) {
if ( empty( $_POST['username'] ) ) {
$errors->add( 'username_error', __( 'Username is required!', 'woocommerce' ) );
} elseif ( username_exists( $_POST['username'] ) ) {
$errors->add( 'username_error', __( 'This username is already taken. Please choose another one.', 'woocommerce' ) );
}
return $errors;
}
// Save Username Field
add_action( 'woocommerce_created_customer', 'custom_save_registration_fields_username' );
function custom_save_registration_fields_username( $customer_id ) {
if ( isset( $_POST['username'] ) && ! empty( $_POST['username'] ) ) {
wp_update_user( array(
'ID' => $customer_id,
'user_login' => sanitize_user( $_POST['username'] ),
) );
}
}
// Prevent Auto-Generation of Username from Email
add_filter( 'woocommerce_new_customer_data', 'custom_override_auto_generated_username', 10, 1 );
function custom_override_auto_generated_username( $data ) {
if ( isset( $_POST['username'] ) && ! empty( $_POST['username'] ) ) {
$data['user_login'] = sanitize_user( $_POST['username'] );
}
return $data;
}
本文标签: wp queryHow could i add username field in WooCommerce
版权声明:本文标题:wp query - How could i add username field in WooCommerce 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736284195a1927196.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论