Tham khảo: Make WordPress User Name the Email Address – Stack Overflow
//code xoa usename va dung email
add_action('login_head', function(){
?>
<style>
#registerform > p:first-child{
display:none;
}
</style>
<script type="text/javascript" src="<?php echo site_url('/wp-includes/js/jquery/jquery.js'); ?>"></script>
<script type="text/javascript">
jQuery(document).ready(function($){
$('#registerform > p:first-child').css('display', 'none');
});
</script>
<?php
});
//Remove error for username, only show error for email only.
add_filter('registration_errors', function($wp_error, $sanitized_user_login, $user_email){
if(isset($wp_error->errors['empty_username'])){
unset($wp_error->errors['empty_username']);
}
if(isset($wp_error->errors['username_exists'])){
unset($wp_error->errors['username_exists']);
}
return $wp_error;
}, 10, 3);
add_action('login_form_register', function(){
if(isset($_POST['user_login']) && isset($_POST['user_email']) && !empty($_POST['user_email'])){
$_POST['user_login'] = $_POST['user_email'];
}
});
// Source - https://stackoverflow.com/a
// Posted by Nishant Kumar, modified by community. See post 'Timeline' for change history
// Retrieved 2025-12-24, License - CC BY-SA 3.0
add_action('login_head', function(){
?>
<style>
#registerform > p:first-child{
display:none;
}
</style>
<script type="text/javascript" src="<?php echo site_url('/wp-includes/js/jquery/jquery.js'); ?>"></script>
<script type="text/javascript">
jQuery(document).ready(function($){
$('#registerform > p:first-child').css('display', 'none');
});
</script>
<?php
});
// Source - https://stackoverflow.com/a
// Posted by Nishant Kumar, modified by community. See post 'Timeline' for change history
// Retrieved 2025-12-24, License - CC BY-SA 3.0
//Remove error for username, only show error for email only.
add_filter('registration_errors', function($wp_error, $sanitized_user_login, $user_email){
if(isset($wp_error->errors['empty_username'])){
unset($wp_error->errors['empty_username']);
}
if(isset($wp_error->errors['username_exists'])){
unset($wp_error->errors['username_exists']);
}
return $wp_error;
}, 10, 3);
; Source - https://stackoverflow.com/a
; Posted by Nishant Kumar, modified by community. See post 'Timeline' for change history
; Retrieved 2025-12-24, License - CC BY-SA 3.0
add_action('login_form_register', function(){
if(isset($_POST['user_login']) && isset($_POST['user_email']) && !empty($_POST['user_email'])){
$_POST['user_login'] = $_POST['user_email'];
}
});
//Code xoa usename va dung email
//Step-1: Remove Username textfield
add_action('login_head', function(){
?>
<style>
#registerform > p:first-child{
display:none;
}
</style>
<script type="text/javascript" src="<?php echo site_url('/wp-includes/js/jquery/jquery.js'); ?>"></script>
<script type="text/javascript">
jQuery(document).ready(function($){
$('#registerform > p:first-child').css('display', 'none');
});
</script>
<?php
});
//Remove error for username, only show error for email only.
add_filter('registration_errors', function($wp_error, $sanitized_user_login, $user_email){
if(isset($wp_error->errors['empty_username'])){
unset($wp_error->errors['empty_username']);
}
if(isset($wp_error->errors['username_exists'])){
unset($wp_error->errors['username_exists']);
}
return $wp_error;
}, 10, 3);
add_action('login_form_register', function(){
if(isset($_POST['user_login']) && isset($_POST['user_email']) && !empty($_POST['user_email'])){
$_POST['user_login'] = $_POST['user_email'];
}
});
Sử dụng email làm tài khoản
add_action('login_form_register', function(){
if(isset($_POST['user_login']) && isset($_POST['user_email']) && ! empty($_POST['user_email'])){
$_POST['user_login'] = $_POST['user_email'];
}
});
Hoặc
add_action('login_form_register', function(){
if(isset($_POST['user_email']) && !empty($_POST['user_email'])){
$_POST['user_login'] = $_POST['user_email'];
}
});
Code ản Admin với user
function restrict_wp_admin_by_role() {
if ( is_admin() && !defined('DOING_AJAX') ) {
// Lấy user hiện tại
$current_user = wp_get_current_user();
// Các role được phép vào wp-admin
$allowed_roles = array('administrator', 'editor', 'author');
// Kiểm tra xem user có role hợp lệ không
if ( !array_intersect($allowed_roles, $current_user->roles) ) {
wp_redirect( home_url() );
exit;
}
}
}
add_action('admin_init', 'restrict_wp_admin_by_role');
add_filter('show_admin_bar', function ($show) {
if ( is_user_logged_in() && current_user_can('read') && !current_user_can('edit_posts') ) {
return false; // Subscriber
}
return $show;
});
Set mật khẩu mặc định cho user khi đăng ký
function set_default_password_for_new_user( $user_id ) {
$default_password = '123456@Abc'; // ⚠️ đổi mật khẩu mặc định tại đây
wp_set_password( $default_password, $user_id );
}
add_action( 'user_register', 'set_default_password_for_new_user' );