Creating a log of user role changes
-
I had been using the default user role change in wordpress and created an function to record those changes in the database:
function record_role_change( $user_id, $new_role, $old_roles ) { $timestamp = current_time( 'mysql' ); // Get the current date and time $user = get_user_by( 'id', $user_id ); // Get the user's information // Create an associative array with the new role and timestamp $log_entry = array( 'new_role' => $new_role, 'date' => $timestamp, ); // Get the existing log entries from user meta (if any) $existing_log = get_user_meta( $user_id, 'role_change_log', true ); // Append the new log entry to the existing log (if any) $updated_log = $existing_log ? json_decode( $existing_log, true ) : array(); $updated_log[] = $log_entry; // Save the updated log entry as user meta data update_user_meta( $user_id, 'role_change_log', wp_json_encode( $updated_log ) ); } add_action( 'set_user_role', 'record_role_change', 10, 3 );
This worked great and allowed me to add a table to the user profile so I had a list of role changes with a time stamp.
Can I modify this action to work with your plugin to record a similar change or does your plugin already do this and I can just reference those changes?
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘Creating a log of user role changes’ is closed to new replies.