Last active
December 24, 2022 03:57
-
-
Save josanua/ccbaa98549e12744747c9f2f69bd67b3 to your computer and use it in GitHub Desktop.
wp users
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /************ user system ************/ | |
| https://wordpress.stackexchange.com/questions/61353/how-to-check-username-password-without-signing-in-the-user | |
| // Authenticate a user, confirming the username and password are valid. | |
| wp_authenticate_username_password( $user, $username, $password ); | |
| username_exists( $username); | |
| https://developer.wordpress.org/reference/functions/get_userdata/ | |
| get_userdata(int $user_id) | |
| // Check if user login in | |
| is_user_logged_in(); | |
| if ( is_user_logged_in() ) { | |
| echo 'Welcome, registered user!'; | |
| } else { | |
| echo 'Welcome, visitor!'; | |
| } | |
| // get current user ID | |
| $current_user_id = get_current_user_id(); | |
| //-------------- User Roles --------------// | |
| // get current user role data | |
| https://newbedev.com/wordpress-get-user-role-name-code-example | |
| wp_get_current_user()->roles; | |
| function get_actual_user_role(){ | |
| // need to create code | |
| } | |
| /* get all role in wordpress in associative key = role(slug) => value(Role Display name) */ | |
| function get_role_names() { | |
| global $wp_roles; | |
| if ( ! isset( $wp_roles ) ) | |
| $wp_roles = new WP_Roles(); | |
| return $wp_roles->get_names(); | |
| } | |
| // Get current user role | |
| $user = wp_get_current_user(); | |
| if ( in_array( 'author', (array) $user->roles ) ) { | |
| //The user has the "author" role | |
| } | |
| // another example from | |
| // https://wordpress.stackexchange.com/questions/239769/how-to-get-the-role-name-of-the-current-user-wordpress | |
| function get_user_role($user_id) { | |
| global $wp_roles; | |
| $roles = array(); | |
| $user = new WP_User( $user_id ); | |
| if ( !empty( $user->roles ) && is_array( $user->roles ) ) { | |
| foreach ( $user->roles as $role ) | |
| $roles[] .= translate_user_role( $role ); | |
| } | |
| return implode(', ',$roles); | |
| } | |
| //then call the function with userid param | |
| echo get_user_role(7); | |
| // change user roles | |
| https://usersinsights.com/wordpress-custom-role/ | |
| https://developer.wordpress.org/reference/classes/wp_user/ | |
| https://wordpress.stackexchange.com/questions/4725/how-to-change-a-users-role | |
| // change user role | |
| $user = new WP_User($current_user_id); | |
| $user->set_role($membership_selected_role); | |
| // user registration | |
| wp_signon( array $credentials = array(), string|bool $secure_cookie = '' ) // Authenticates and logs a user in with ‘remember’ capability. | |
| $info = array(); | |
| $info['user_login'] = $_POST['log']; | |
| $info['user_password'] = $_POST['pwd']; | |
| $info['remember'] = true; | |
| $user_signon = wp_signon( $info, false ); | |
| if( is_wp_error( $user_signon )){ | |
| // echo json_encode( array( "status" => 0 )); | |
| echo $user_signon->get_error_message(); | |
| } else { | |
| echo json_encode( array( "status" => 1 )); | |
| } | |
| //-------------- user logination, user login --------------// | |
| // standard, de mine cu redirect, cu functia wp_signon | |
| wp_signon( array( 'user_login' => $username, 'user_password' => $password, 'remember' => false ), false ); | |
| wp_redirect(admin_url()); | |
| exit; | |
| // la fel cu wp_signon | |
| $creds = array(); | |
| $creds['user_login'] = 'login'; | |
| $creds['user_password'] = 'password'; | |
| $creds['remember'] = true; | |
| $user = wp_signon( $creds, false ); | |
| if ( is_wp_error($user) ) | |
| { | |
| echo $user->get_error_message(); | |
| } | |
| // alta metoda din start cu apel la hook wp_authenticate | |
| // пробуем получить юзера | |
| $auth = wp_authenticate( $username, $password ); | |
| // Проверка ошибок | |
| if ( is_wp_error( $auth ) ) { | |
| $error_string = $auth->get_error_message(); | |
| echo '<div id="message" class="error"><p>' . $error_string . '</p></div>'; | |
| } | |
| else { | |
| // юзер с указанным логином и паролем существует! | |
| // авторизуем его | |
| wp_set_auth_cookie( $auth->ID ); | |
| } | |
| // alta metoda | |
| // https://wp-kama.ru/function/wp_authenticate | |
| $user_obj = wp_authenticate($name, $pass); | |
| if(!is_wp_error($user_obj)){ | |
| // Юзер зашел!!! | |
| nocache_headers(); | |
| wp_clear_auth_cookie(); | |
| wp_set_auth_cookie($user_obj->ID); | |
| }else{ | |
| // Юзер не зашел | |
| $error_string = $user_obj->get_error_message(); | |
| echo $error_string; | |
| } | |
| //-------------- save user data --------------// | |
| https://developer.wordpress.org/plugins/users/working-with-user-metadata/ | |
| // Update user meta field based on user ID. | |
| update_user_meta( int $user_id, string $meta_key, mixed $meta_value, mixed $prev_value = '' ); | |
| wp_update_user(array('ID' => $currentUserId, 'last_name' => esc_html($_POST['updateUserLastname']))); | |
| // Cimp aditional in profil | |
| /** | |
| * | |
| * The idnp field on the editing screens. | |
| * | |
| * @param $user WP_User user object | |
| */ | |
| function idnp_usermeta_form_field( $user ) | |
| { | |
| // TODO: de facut verificare de cimp | |
| ?> | |
| <h2> | |
| <?php esc_html_e( 'MPass settings', 'wp-bootstrap-starter' ); ?> | |
| </h2> | |
| <table class="form-table"> | |
| <tr> | |
| <th> | |
| <label for="user_idnp_code"> | |
| <?php esc_html_e( 'Codul IDNP', 'wp-bootstrap-starter' ); ?> | |
| </label> | |
| </th> | |
| <td> | |
| <input type="number" | |
| maxlength="13" | |
| class="regular-text ltr" | |
| id="user_idnp_code" | |
| name="user_idnp_code" | |
| value="<?= esc_attr( get_user_meta( $user->ID, 'user_idnp_code', true ) ) ?>" | |
| title="<?php esc_html_e( 'Please use number format', 'wp-bootstrap-starter' ); ?>" | |
| required> | |
| <p class="description"> | |
| <?php esc_html_e( 'Indroduceti codul IDNP', 'wp-bootstrap-starter' ); ?> | |
| </p> | |
| </td> | |
| </tr> | |
| </table> | |
| <?php | |
| } | |
| /** | |
| * The save action. | |
| * | |
| * @param $user_id int the ID of the current user. | |
| * | |
| * @return bool Meta ID if the key didn't exist, true on successful update, false on failure. | |
| */ | |
| function idnp_usermeta_form_field_update( $user_id ) | |
| { | |
| // check that the current user have the capability to edit the $user_id | |
| if ( ! current_user_can( 'edit_user', $user_id ) ) { | |
| return false; | |
| } | |
| // create/update user meta for the $user_id | |
| // TODO: de facut convertire in sha256 | |
| return update_user_meta( | |
| $user_id, | |
| 'user_idnp_code', | |
| $_POST['user_idnp_code'] | |
| ); | |
| } | |
| // Add the field to user's own profile editing screen. | |
| add_action( | |
| 'show_user_profile', | |
| 'idnp_usermeta_form_field' | |
| ); | |
| // Add the field to user profile editing screen. | |
| add_action( | |
| 'edit_user_profile', | |
| 'idnp_usermeta_form_field' | |
| ); | |
| // Add the save action to user's own profile editing screen update. | |
| add_action( | |
| 'personal_options_update', | |
| 'idnp_usermeta_form_field_update' | |
| ); | |
| // Add the save action to user profile editing screen update. | |
| add_action( | |
| 'edit_user_profile_update', | |
| 'idnp_usermeta_form_field_update' | |
| ); | |
| //-------------- Custom user form for front-end editing --------------// | |
| https://wordpress.stackexchange.com/questions/9775/how-to-edit-a-user-profile-on-the-front-end | |
| https://stackoverflow.com/questions/6320113/how-to-prevent-form-resubmission-when-page-is-refreshed-f5-ctrlr | |
| <?php | |
| // User form for change user profile data. | |
| // [userinfoform][/userinfoform] | |
| function render_userinfoform() | |
| { | |
| $currentUserId = get_current_user_id(); | |
| $currentUserMetaData = get_user_meta($currentUserId); | |
| $currentUserData = get_userdata($currentUserId); | |
| $currentPagePermalink = get_the_permalink(); | |
| $redirectLinkLostPass = wp_lostpassword_url(get_home_url() . '/' . get_page_uri()); | |
| $errorData = []; | |
| // $new_email = get_user_meta( $current_user->ID, '_new_email', true ); | |
| // $currentUserMetaData['first_name'][0]; | |
| // $currentUserMetaData['last_name'][0]; | |
| // $currentUserMetaData['r_number'][0]; | |
| // $currentUserData->data->user_email; | |
| // $currentUserData->roles[0]; | |
| ob_start(); | |
| // echo '<pre>'; | |
| // var_dump(); | |
| // echo '</pre>'; | |
| ?> | |
| <div class="updateUserDataForm column is-6"> | |
| <div class="text"> | |
| <h2><?php echo __('My account', 'industria'); ?></h2> | |
| </div> | |
| <form method="post" id="updateUserData" action="<?= $currentPagePermalink ?>"> | |
| <input name="updateUserFirstname" id="updateUserFirstname" type="text" | |
| placeholder="<?= $currentUserMetaData['first_name'][0] ?>"/> | |
| <input name="updateUserLastname" id="updateUserLastname" type="text" | |
| placeholder="<?= $currentUserMetaData['last_name'][0] ?>"/> | |
| <input name="rNumber" type="text" class="r-number" id="updateRNumber" | |
| placeholder="<?php echo __('R number: ') . $currentUserMetaData['r_number'][0] ?>"/> | |
| <span class="info-text r-number"><?php echo __('If student, required to enter current R number.') ?></span> | |
| <span class="info-text r-number"><?php echo __('If an alumnus, optionally enter former R number.') ?></span> | |
| <input name="userEmail" class="user-email" type="email" id="userEmail" | |
| placeholder="<?= $currentUserData->data->user_email ?>"/> | |
| <input name="" type="text" placeholder="<?php echo __('Type: ', 'industria') . current($currentUserData->roles) ?>" | |
| disabled/> | |
| <span class="info-text"><?php echo __('Forgot password?', 'industria') ?></span> | |
| <span class="info-text"> | |
| <?php echo __('Reset it: ', 'industria') ?> | |
| <a href="<?= $redirectLinkLostPass ?>"> | |
| <?php echo __('Forgot your password?', 'industria') ?> | |
| </a> | |
| </span> | |
| <input name="userDataUpdateAction" type="hidden" id="action" value="update-user-data"/> | |
| <button class="button has-arrow" id="formSendButton" disabled> | |
| <?php _e('Save changes', 'industria'); ?> | |
| </button> | |
| <!-- .form-submit --> | |
| </form> | |
| <?php | |
| if ('POST' == $_SERVER['REQUEST_METHOD'] && !empty($_POST['userDataUpdateAction']) && $_POST['userDataUpdateAction'] == 'update-user-data') { | |
| if (!empty($_POST['updateUserFirstname'])) { | |
| wp_update_user(array('ID' => $currentUserId, 'first_name' => esc_html($_POST['updateUserFirstname']))); | |
| } | |
| if (!empty($_POST['updateUserLastname'])) { | |
| wp_update_user(array('ID' => $currentUserId, 'last_name' => esc_html($_POST['updateUserLastname']))); | |
| } | |
| if (!empty($_POST['rNumber'])) { | |
| update_user_meta( $currentUserId, 'r_number', esc_html($_POST['rNumber'])); | |
| } | |
| if (!empty($_POST['userEmail'])) { | |
| $userNewEmail = sanitize_email($_POST['userEmail']); | |
| if (!is_email($userNewEmail)) { | |
| $errorData[] = __('The Email you entered is not valid. please try again.', 'industria'); | |
| } elseif (email_exists($userNewEmail) == $currentUserId) { | |
| $errorData[] = __('This email is already used by another user. try a different one.', 'industria'); | |
| } else { | |
| wp_update_user(array('ID' => $currentUserId, 'user_email' => $userNewEmail)); | |
| } | |
| } | |
| // Show user messages | |
| if (count($errorData) > 0) : ?> | |
| <div class="form-notice error"> | |
| <?php echo $errorData[0] ?> | |
| </div> | |
| <?php else : | |
| // echo __('User data was updated'); | |
| wp_redirect($currentPagePermalink); | |
| exit(); | |
| endif; | |
| } | |
| ?> | |
| </div> | |
| <?php return ob_get_clean(); ?> | |
| <?php } | |
| add_shortcode('userinfoform', 'render_userinfoform'); | |
| // Login last pass redirect | |
| $redirectLinkLostPass = wp_lostpassword_url(get_home_url() . get_page_uri()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment