Hook or action to save profile fields at specific time

i want to make a field in the user profile to know who added this user, the problem is that this field displays the current logged in user, i wonder if the is a hook or action to make this custom field preserve the value of the user who added or edited it.
here is my code:

function custom_user_profile_fields($user){
    if(is_object($user)){
      //$created_by = esc_attr( the_author_meta( 'created_by', $user->ID ) );
      $current_user = wp_get_current_user();
      $created_by = $current_user->user_login;
    }
      //var_dump($created_by);
    else{
        $created_by = null;
        }
      //var_dump($created_by);
    ?>
    <h3>Extra profile information</h3>
    <table class="form-table">
        <tr>
            <th><label for="created_by">Created By</label></th>
            <td>
                <input type="text" class="regular-text" name="created_by" value="<?= $created_by; ?>" id="created_by" disabled /><br />
                <span class="description">The person who creates or updates this user</span>
            </td>
        </tr>
    </table>
<?php
}
add_action( 'show_user_profile', 'custom_user_profile_fields' );
add_action( 'edit_user_profile', 'custom_user_profile_fields' );
add_action( "user_new_form", "custom_user_profile_fields" );

function save_custom_user_profile_fields($user_id){
    # again do this only if you can
    //if(!current_user_can('manage_options'))
      //  return false;

    # save my custom field
    update_user_meta($user_id, 'created_by', $_POST['created_by']);
}
add_action('user_register', 'save_custom_user_profile_fields');
add_action('profile_update', 'save_custom_user_profile_fields');

0

Leave a Comment