xprofile_updated_profile
Fires when a user's profile fields are updated, passing the user ID and updated field information.
add_action( 'xprofile_updated_profile', $callback, 10, 4 );
Description
Fires after a user's BuddyBoss profile fields are updated. Developers can use this hook to perform custom actions based on profile changes, such as triggering notifications or updating related data. It passes the user ID, updated field IDs, old and new profile values. The `false` parameter is for internal use.
Usage
add_action( 'xprofile_updated_profile', 'your_function_name', 10, 4 );
Parameters
-
$user_id(mixed) - The ID of the user whose profile was updated.
-
$posted_field_ids(mixed) - The ID of the user whose profile was updated.
-
$old_values(mixed) - This parameter is not used and is always passed as false.
-
$new_values(mixed) - This parameter contains an array of the old values for the profile fields that were updated.
Examples
add_action( 'xprofile_updated_profile', 'my_custom_xprofile_update_handler', 10, 5 );
/**
* Handles updates to BuddyPress user profiles.
*
* This function is triggered when a user's profile fields are updated.
* It logs the changes and potentially triggers other actions based on the updated fields.
*
* @param int $user_id The ID of the user whose profile was updated.
* @param array $posted_field_ids An array of field IDs that were posted/updated.
* @param bool $false_param This parameter is unused in this context, a remnant from older versions or specific integrations.
* @param array $old_values An associative array of the old values for the updated fields.
* @param array $new_values An associative array of the new values for the updated fields.
*/
function my_custom_xprofile_update_handler( $user_id, $posted_field_ids, $false_param, $old_values, $new_values ) {
// For demonstration purposes, let's log which fields were updated and their new values.
// In a real-world scenario, you might use this to trigger notifications,
// update other plugin data, or perform other integrations.
error_log( "BuddyPress profile updated for user ID: " . $user_id );
if ( ! empty( $posted_field_ids ) ) {
error_log( "Updated field IDs: " . implode( ', ', $posted_field_ids ) );
foreach ( $posted_field_ids as $field_id ) {
if ( isset( $new_values[ $field_id ] ) ) {
$field_label = xprofile_get_field_label( $field_id ); // Get the human-readable label for the field.
$old_val = isset( $old_values[ $field_id ] ) ? $old_values[ $field_id ] : 'N/A';
$new_val = $new_values[ $field_id ];
error_log( "Field: '" . $field_label . "' (ID: " . $field_id . ") changed from '" . $old_val . "' to '" . $new_val . "'." );
// Example: If a specific field (e.g., 'Website' field ID 5) is updated,
// you might want to perform a specific action.
if ( 5 === $field_id && 'http://example.com' === $new_val ) {
error_log( "User " . $user_id . " updated their website to example.com. Triggering a special notification." );
// In a real scenario, you would trigger a notification or another action here.
// e.g., wp_mail( '[email protected]', 'Special Website Update', 'User ' . $user_id . ' updated their website.' );
}
}
}
} else {
error_log( "No specific fields were marked as updated, but the profile update action was triggered." );
}
// The third parameter `$false_param` is typically not used directly in custom handlers.
// It's important to acknowledge its presence in the hook signature.
// You can also use the $old_values and $new_values to compare and react.
if ( isset( $old_values['location'] ) && 'New York' === $new_values['location'] && 'London' === $old_values['location'] ) {
error_log( "User " . $user_id . " moved from London to New York." );
// Perform actions related to location change.
}
}
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
uncanny-automator-pro/src/integrations/buddyboss/actions/bdb-setuserprofiledata.php:149
uncanny-automator-pro/src/integrations/buddypress/actions/bp-setuserprofiledata.php:149
public function update_user_profile_data( $user_id, $action_data, $recipe_id, $args ) {
// Bail required function doesn't exist.
if ( ! function_exists( 'xprofile_set_field_data' ) ) {
$action_data['complete_with_errors'] = true;
Automator()->complete_action( $user_id, $action_data, $recipe_id, __( 'xprofile_set_field_data function does not exist.', 'uncanny-automator-pro' ) );
return;
}
$user_fields_data = $action_data['meta']['BDBPROFILEDATA'];
// Set Variables in case we need to do xprofile_updated_profile action.
$posted_field_ids = array();
$old_values = array();
$new_values = array();
// Adding other users
if ( ! empty( $user_fields_data ) ) {
$user_selectors = json_decode( $user_fields_data, true );
if ( ! empty( $user_selectors ) ) {
foreach ( $user_selectors as $user_selector ) {
$field_id = $user_selector['BDBUSERFIELD'];
if ( ! empty( $user_selector['VALUE'] ) ) {
$value = Automator()->parse->text( $user_selector['VALUE'], $recipe_id, $user_id, $args );
// Format value based on field type.
$value = $this->maybe_format_field_value( $value, $field_id );
$value = apply_filters( 'automator_buddyboss_set_user_profile_data_value', $value, $field_id, $user_id, $recipe_id, $args );
// Populate variables for do xprofile_updated_profile action.
$field_array = array(
'value' => xprofile_get_field_data( $field_id, $user_id ),
'visibility' => xprofile_get_field_visibility_level( $field_id, $user_id ),
);
$posted_field_ids[] = $field_id;
$old_values[ $field_id ] = $field_array;
$field_array['value'] = $value;
$new_values[ $field_id ] = $field_array;
// Update Field.
xprofile_set_field_data( $field_id, $user_id, $value );
}
}
}
}
// Maybe do xprofile_updated_profile action.
if ( empty( did_action( 'xprofile_updated_profile' ) ) && ! empty( $posted_field_ids ) ) {
do_action( 'xprofile_updated_profile', $user_id, $posted_field_ids, false, $old_values, $new_values );
}
Automator()->complete_action( $user_id, $action_data, $recipe_id );
}