Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- The admin updates the user option page:
- -> edit_user_profile_update or personal_options_update hooks activated
- -> edit_user() function is called
- -> wp_update_user() function is called within edit_user()
- -> wp_insert_user() function is called within wp_update_user()
- -> profile_update hook activated within wp_insert_user()
- for user updates, not user inserts
- -> wp_redirect() function called on successful user updates
- -> wp_redirect filter activated
- -> The page reloads
- -> admin_notices hook activated
- add_filter( 'show_password_fields', 'wpse_notification_html' );
- function wpse_notification_html( $show )
- {
- if( current_user_can( 'manage_options' ) ):
- ?>
- <tr>
- <th scope="row">
- <label for="wpse_send_notification"><?php _e('Send a notification?') ?></label>
- </th>
- <td>
- <label for="wpse_send_notification">
- <input type="checkbox" name="wpse_send_notification" id="wpse_send_notification" value="1" />
- <?php _e( 'Send an email to user and notify that the password has changed.' ); ?>
- </label>
- </td>
- </tr>
- <?php
- endif;
- return $show;
- }
- add_action( 'edit_user_profile_update', 'wpse_user_update' );
- add_action( 'personal_options_update', 'wpse_user_update' );
- function wpse_user_update( $user_id )
- {
- if( current_user_can( 'manage_options' ) )
- add_action( 'profile_update', 'wpse_controller', 10, 2 );
- }
- function wpse_controller( $user_id, $old_user_data )
- {
- // Input:
- $pass1 = filter_input( INPUT_POST, 'pass1' );
- $pass2 = filter_input( INPUT_POST, 'pass2' );
- $send = filter_input( INPUT_POST, 'wpse_send_notification', FILTER_SANITIZE_NUMBER_INT );
- // Run this action only once:
- remove_action( current_action(), __FUNCTION__ );
- // Send the notification:
- if( 1 == $send )
- {
- if( ! empty( $pass1 )
- && $pass1 === $pass2
- && sanitize_text_field( $pass1 ) === $pass1
- ):
- if( wpse_user_password_notification( $user_id, wp_unslash( sanitize_text_field( $pass1 ) ) ) )
- add_filter( 'wp_redirect', 'wpse_redirect_notification_success' );
- else
- add_filter( 'wp_redirect', 'wpse_redirect_notification_error' );
- else:
- add_filter( 'wp_redirect', 'wpse_redirect_pass_validation_error' );
- endif;
- }
- }
- function wpse_redirect_notification_success( $location )
- {
- return add_query_arg( 'wpse_notification', 'mail_success', $location );
- }
- function wpse_redirect_notification_error( $location )
- {
- return add_query_arg( 'wpse_notification', 'mail_error', $location );
- }
- function wpse_redirect_pass_validation_error( $location )
- {
- return add_query_arg( 'wpse_notification', 'pass_validation_error', $location );
- }
- function wpse_user_password_notification( $user_id, $plaintext_pass = '' )
- {
- if ( empty( $plaintext_pass ) )
- return false;
- $user = get_userdata( $user_id );
- $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
- $message = sprintf( __( 'Username: %s' ), $user->user_login ) . "rn";
- $message .= sprintf( __( 'New Password: %s' ), $plaintext_pass ) . "rn";
- $message .= wp_login_url() . "rn";
- return wp_mail( $user->user_email, sprintf(__('[%s] Your username and new password'), $blogname), $message );
- }
- add_action( 'admin_notices', 'wpse_admin_notices' );
- function wpse_admin_notices()
- {
- $status = filter_input( INPUT_GET, 'wpse_notification', FILTER_SANITIZE_STRING );
- switch ( $status )
- {
- case 'mail_success':
- ?><div id="message" class="updated"><p><strong>Notification Sent!</strong>: Notification email successfully sent to the user</p></div><?php
- break;
- case 'mail_error':
- ?><div class="error"><p><strong>ERROR</strong>: Notification email not sent to the user</p></div><?php
- break;
- case 'pass_validation_error':
- ?><div class="error"><p><strong>ERROR</strong>: Notification email not sent to the user, because of symbol chars in the password </p></div><?php
- break;
- } // end switch
- }
- $pass_change_email = apply_filters( 'password_change_email', $pass_change_email, $user, $userdata );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement