• Resolved designerken

    (@kcharity)


    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)
  • Plugin Author Caseproof

    (@caseproof)

    Hi @kcharity

    Members uses the same functionality to manage roles and users as WordPress so your function should work with Members as well.

    Best

    Thread Starter designerken

    (@kcharity)

    Hi there.

    Yes it works fine as long as i don’t use the option for allowing multiple user roles. We are however experimenting with this option for our site. So I think your plugin does something different for that use case?

    Plugin Author Caseproof

    (@caseproof)

    We use the profile_update hook to add multiple user roles so you may try that as well.

    Best

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Creating a log of user role changes’ is closed to new replies.