• lorenzo

    (@psycolor)


    hi folks,

    i am trying to create a hook to add to my theme functions.php file which sends an email notification to the admin when a user update their profile.

    as you can see below i have managed to get the work half done (i.e. prepare the message and pass the details of the updates), however i would like to pass also the details *before the update* (in my case the old_user_data).

    the code below passes the new info. can anyone suggest how to pick up the details before the update is actually performed?

    function user_profile_update($userid, $old_user_data) {
    	$userdata = get_userdata($userid);
    	$message = "A user profile has been updated\n\n";
    	foreach($_POST as $key => $value){
                $message .= $key . ": ". $value ."\n";
            }
    	//$message .= print_r($userdata,true);
    	$message .= "old data\n\n";
    	$message .= print_r($old_user_data,true);
    	@wp_mail(get_option('admin_email'), 'User Profile Update', $message);
    }
    add_action('profile_update','user_profile_update');

    thanks

    lorenzo

Viewing 5 replies - 1 through 5 (of 5 total)
  • sozot

    (@sozot)

    $old_user_data has the WP_User object and it looks like you’re already appending that to the $message. It looks to me like you’re already including the old and new data.

    Thread Starter lorenzo

    (@psycolor)

    hi Andy,

    thanks for the reply. yes, that would be the theory (or what i thought i was doing with this function), but the problem is that $old_user_data seems to be empty and i don’t know how to ‘catch’ the data before the update is carried out.

    the codex docs are not very clear and i should also say that i’m not a programmer, which makes this a bit of a struggle.
    any suggestion will be very welcome!

    lorenzo

    Moderator bcworkz

    (@bcworkz)

    Modify add_action() like so:
    add_action('profile_update','user_profile_update', 10, 2);

    You must supply the ‘2’ parameter to get both parameters passed to your callback function. The ’10’ is the default priority value, which should be fine as is, it’s mainly there so the ‘2’ can be passed.

    Thread Starter lorenzo

    (@psycolor)

    this is brilliant thanks, adding the parameter did the trick, but i have absolutely no idea why. if you have any link explaining things in more detail i’d love to learn more (and hopefully it will be helpful for other reading the thread in the future.

    Moderator bcworkz

    (@bcworkz)

    Function_Reference/add_filter has a slightly better explanation, the concept applies equally to add_action(). Unfortunately, it’s a less than satisfactory explanation IMO. If it’s still not making sense, if you can ask a specific question here, I will attempt to explain further.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[wp_update_user hook]’ is closed to new replies.