The added_usermeta WordPress PHP action is triggered after user meta data is added to the database.
Usage
add_action('added_usermeta', 'your_function_name', 10, 4); function your_function_name($mid, $user_id, $meta_key, $meta_value) { // your custom code here }
Parameters
$mid
(int) – The ID of the newly added user meta data.$user_id
(int) – The ID of the user associated with the meta data.$meta_key
(string) – The key of the added meta data.$meta_value
(mixed) – The value of the added meta data.
More information
See WordPress Developer Resources: added_usermeta
Examples
Send a notification when a specific user meta is added
This example sends an email to the admin when the user_membership
meta key is added to a user.
add_action('added_usermeta', 'send_notification_on_membership_addition', 10, 4); function send_notification_on_membership_addition($mid, $user_id, $meta_key, $meta_value) { if ($meta_key === 'user_membership') { $admin_email = get_option('admin_email'); $subject = 'New user membership added'; $message = 'A new user membership has been added for user ID ' . $user_id . '.'; wp_mail($admin_email, $subject, $message); } }
Log user meta additions
This example logs all added user meta data to a custom log file.
add_action('added_usermeta', 'log_usermeta_addition', 10, 4); function log_usermeta_addition($mid, $user_id, $meta_key, $meta_value) { $log_file = 'usermeta_log.txt'; $log_message = sprintf( 'User ID: %d | Meta Key: %s | Meta Value: %s | Meta ID: %d', $user_id, $meta_key, $meta_value, $mid ); file_put_contents($log_file, $log_message . PHP_EOL, FILE_APPEND); }
Update user role based on user meta
This example automatically updates a user’s role to ‘subscriber’ when the user_activation_status
meta key is set to ‘active’.
add_action('added_usermeta', 'update_user_role_on_activation', 10, 4); function update_user_role_on_activation($mid, $user_id, $meta_key, $meta_value) { if ($meta_key === 'user_activation_status' && $meta_value === 'active') { $user = get_user_by('id', $user_id); $user->set_role('subscriber'); } }
Create a post for a user when a specific meta is added
This example creates a new post for the user when the user_profile_complete
meta key is added.
add_action('added_usermeta', 'create_user_post_on_profile_completion', 10, 4); function create_user_post_on_profile_completion($mid, $user_id, $meta_key, $meta_value) { if ($meta_key === 'user_profile_complete') { $user = get_userdata($user_id); $post_data = array( 'post_title' => $user->display_name . '\'s Profile', 'post_status' => 'publish', 'post_type' => 'post', 'post_author' => $user_id, ); wp_insert_post($post_data); } }
Notify followers when a user adds a new skill
This example sends a notification to a user’s followers when the user_skills
meta key is updated with a new skill.
add_action('added_usermeta', 'notify_followers_on_new_skill', 10, 4); function notify_followers_on_new_skill($mid, $user_id, $meta_key, $meta_value) { if ($meta_key === 'user_skills') { $followers = get_user_meta($user_id, 'followers', true); if (!empty($followers)) { $user = get_userdata($user_id); $subject = $user->display_name . ' has added a new skill!'; $message = $user->display_name . ' has added a new skill: ' . $meta_value . '.'; foreach ($followers as $follower_id) { $follower_email = get_user_meta($follower_id, 'user_email', true); wp_mail($follower_email, $subject, $message); } } } }