0

I'm trying to add a "last modified" date to user profiles on WordPress. I'm using the Ultimate Member plugin, so I would prefer to to find a way to save this date to a meta-key that I can pull when I do a search as well.

So far, I this is what I have using a shortcode [wppb-last-updated], but it displays the currently logged in user's last modified date. As an admin, I need to see when others have last modified their own information. Any suggestions?

add_filter('wppb_edit_profile_all_changes_saved', 'wppb_last_updated_save');
add_filter('wppb_edit_profile_all_changes_saved_except_existing_email', 'wppb_last_updated_save');
add_filter('wppb_edit_profile_all_changes_saved_except_invalid_email', 'wppb_last_updated_save');
add_filter('wppb_edit_profile_all_changes_saved_except_mismatch_password', 'wppb_last_updated_save');
add_filter('wppb_edit_profile_all_changes_saved_except_uncompleted_password', 'wppb_last_updated_save');
function wppb_last_updated_save($content){
    $user = wp_get_current_user();
    update_user_meta( $user->ID, 'wppb_last_updated', date("d / M / Y") );
    return $content;
}

add_shortcode( 'wppb-last-updated', 'wppb_last_updated_print');
function wppb_last_updated_print(){
    $user = wp_get_current_user();
    $last_updated = get_user_meta($user->ID,  'wppb_last_updated', true );
    if ( $last_updated == '' ){
        $udata = get_userdata( $user->ID );
        $registered = $udata->user_registered;

        return date( "m/d/Y", strtotime( $registered ) );
    }

    return $last_updated;
}

EDIT: Is there an alternative method of pulling up the modified date of a user profile via shortcode and parameter anywhere on the site? Something like this?

/**
 * Shortcode for Displaying Last Modified Date of User Profile
 * Usage: [wppb-last-updated user_id="1"]
 */
add_filter('wppb_edit_profile_all_changes_saved', 'wppb_last_updated_save');
add_filter('wppb_edit_profile_all_changes_saved_except_existing_email', 'wppb_last_updated_save');
add_filter('wppb_edit_profile_all_changes_saved_except_invalid_email', 'wppb_last_updated_save');
add_filter('wppb_edit_profile_all_changes_saved_except_mismatch_password', 'wppb_last_updated_save');
add_filter('wppb_edit_profile_all_changes_saved_except_uncompleted_password', 'wppb_last_updated_save');
function wppb_last_updated_save($content){
    $user = $user_id;
    update_user_meta( $user->ID, 'wppb_last_updated', date("d / M / Y") );
    return $content;
}

add_shortcode( 'wppb-last-updated', 'wppb_last_updated_print');
function wppb_last_updated_print($atts){
    $user_id = shortcode_atts( array('user_id' => ''), $atts );
    $last_updated = get_user_meta($user->ID,  'wppb_last_updated', true );
    if ( $last_updated == '' ){
        $udata = get_userdata( $user->ID );
        $registered = $udata->user_registered;

        return date( "m/d/Y", strtotime( $registered ) );
    }

    return $last_updated;
}
  • 1
    wp_get_current_user() always gets the current user - which is you. In both the filter and the shortcode, you need to get the user whose profile you're on. – WebElaine May 8 at 21:21
  • @WebElaine, if I switch wp_get_current_user() to wp_get_user() will it work? I'm not sure how to change it. – John Smithly May 9 at 16:29
  • wp_get_user() alone is only part of the solution - you will also need to pass in the user ID, which you need to get from the query string on the edit profile page. Try something like global $_GET; $id = $_GET['user_id']; wp_get_user($id); – WebElaine May 9 at 18:39
  • I am trying to figure this out, but I'm still learning as I go. Do I add this into the filter? Replace something else? I've tried a number of different things and can't seem to get it to work. I'm also thinking there might be an alternative way of pulling the information, such as identifying the user ID in a parameter instead of trying to get the user ID that the shortcode is on. This would give me more flexibility anyway to use it in other areas of the site. I edited my post and added what I've done so far to try to make it happen, but am getting stuck here, too. – John Smithly May 10 at 15:52
  • You assign the id to the variable $user_id, but you still use$user->ID in functions get_user_meta() and get_userdata(). – nmr May 10 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.