Tập hợp những functions hay dùng cho user
Nội dung bài viết
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//bằng id user $user_info = get_userdata(1); $username = $user_info->user_login; $first_name = $user_info->first_name; $last_name = $user_info->last_name; $user_name = $user_info->display_name; $user_email = $user_info->user_email; //current user $current_user->ID $current_user = wp_get_current_user(); $current_user->user_login, $current_user->user_email, $current_user->user_url, |
1 |
get_current_user_id() |
1 2 3 4 5 |
if ( is_user_logged_in() ) { echo 'User ID: ' . get_current_user_id(); } else { echo 'Hello visitor!'; } |
1 2 3 4 5 6 7 8 9 10 11 |
if ( username_exists( $username ) ) { echo "Username In Use!"; } else { echo "Username Not In Use!"; } $exists = email_exists( $email ); if ( $exists ) { echo "That E-mail is registered to user number " . $exists; } else { echo "That E-mail doesn't belong to any registered users on this site"; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$exists = email_exists($data['email']); if ( $exists ) { $parent_foster_user_id = $exists; } else { $random_password = wp_generate_password( $length = 12, $include_standard_special_chars = false ); $new_user_data = array( 'user_login' => $data['full_name'], 'user_pass' => $random_password, 'user_email' => $data['email'], 'role' => 'subscriber' ); $parent_foster_user_id = wp_insert_user( $new_user_data ); } |
1 2 3 4 5 6 7 8 9 10 |
//auto login $creds = array(); $creds['user_login'] = $user_name; $creds['user_password'] = $password; $creds['remember'] = true; $user = wp_signon( $creds, is_ssl() ); //cách 2 wp_clear_auth_cookie(); wp_set_current_user($user->ID); wp_set_auth_cookie($user->ID); |
1 2 3 4 |
$user = wp_get_current_user(); if ( in_array( 'author', (array) $user->roles ) ) { //The user has the "author" role } |
1 |
<a href="<?php echo wp_logout_url(); ?>">Thoát</a> |
1 2 3 4 5 |
// get all sessions for user with ID $user_id $sessions = WP_Session_Tokens::get_instance($user_id); // we have got the sessions, destroy them all! $sessions->destroy_all(); |
Mọi sự sao chép xin ghi rõ nguồn là fcwordpress.net
Chuyên trang về wordpress: hướng dẫn thiết kế theme, plugin, thủ thuật wordpress