7

I'm looking for the hook that fires when a user's information are updated. Specifically, I want to update a post with the value of a custom profile field (in my example info) everytime that user's profile is updated.

I tried the profile_update hook, but it doesn't seem to fire:

add_action( 'profile_update', 'add_info_to_post' );
function add_info_to_post( $user_id ) {

        $info=get_user_meta($user_id,'info',true);

        //get all items of that user
        $args=array(
                'author' => $user_id,
                'post_type' => 'item',
        );

        $items=get_posts($args);            
        foreach ($items as $item){
        update_post_meta($item->ID,'user_info',$info);
        }
}

Any suggestions how to make this work?

EDIT: Maybe I should mention that I access the users' profile pages through the backend..

2
  • In your code the function name and the function you have in the add_action don't match. Commented Jun 5, 2014 at 10:25
  • right! corrected
    – yumba
    Commented Jun 5, 2014 at 10:26

2 Answers 2

11

From Codex: Plugin API - Action Reference - profile_update:

Note: This hook is not used on user edit/profile pages. To hook into the admin's user edit pages, use the hook edit_user_profile_update which is located in /wp-includes/user-edit.php instead.

From Codex: Plugin API - Action Reference - edit_user_profile_update:

This hook only triggers when a user is viewing another user's profile page (not their own). If you want to apply your hook to ALL profile pages (including the current user) then you also need to use the personal_options_update hook.

0
0

You can use the updated_user_meta action hook. It will fire immediately after updating user metadata. More about the hook

I have used this hook to check if the user profile photo has been updated. Here is the working code for my case:

/** 
 * Set profile avatar URL on user profile photo updated
 * 
 * Problem fixed: it needs to reload the page to populate the 
 * profile photo in the sidebar, even after add/remove the avatar photo in the user profile form.
 */
add_action( 'updated_user_meta', 'gem_updated_profile_photo', 10, 4);
function gem_updated_profile_photo( $meta_id, $object_id, $meta_key, $meta_value ){
    if ( $meta_key === 'avater' ) {
        // do your stuff here
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.