wpf_user_update

#Overview

This filter allows you to modify user meta data before it’s sent to your CRM. To use the code examples below, add them to your active theme’s functions.php file.

#Parameters

  • $user_meta: Array of user meta data, in the format array('meta_field' => 'value').
  • $user_id: ID for the user being updated.

#Examples

#Save the full URL to a user’s profile photo

If you’re using Ultimate Member to run your site’s membership platform, and want to save a link to the user’s profile photo in your CRM, you’ll find that just syncing the profile_photo field gives you the name of the file, but not the full URL. Using wpf_user_update, we can modify this so the full URL to the user’s profile is sent.

function set_profile_photo_url( $user_meta, $user_id ) {
	
	// Changes "profile_photo.png" to "http://mysite.com/wp-content/uploads/ultimatemember/1/profile_photo.png"
	if( isset( $user_meta['profile_photo'] ) ) {
		$user_meta['profile_photo'] = content_url() . '/uploads/ultimatemember/' . $user_id . '/' . $user_meta['profile_photo'];
	}

	return $user_meta;


}

add_filter( 'wpf_user_update', 'set_profile_photo_url', 10, 2 );

#Sync a role title instead of role slug

This example converts the role slug volunteer_both to “Volunteer” when the role field is being synced to the CRM, either during a profile update or new user registration (via the wpf_user_register filter).

function volunteer_role( $user_meta, $user_id ) {

	if ( isset( $user_meta['role'] ) && 'volunteer_both' === $user_meta['role'] ) {
		$user_meta['role'] = 'Volunteer';
	}

	return $user_meta;

}

add_filter( 'wpf_user_register', 'volunteer_role', 10, 2 );
add_filter( 'wpf_user_update', 'volunteer_role', 10, 2 );

#Convert a field value to a tag

With Constant Contact, Customer.io, Encharge, FluentCRM, Groundhogg, HubSpot, Infusionsoft / Keap, and Ontraport, WP Fusion is able to create new tags by sending an API call.

This snippet looks for a custom field value in the user’s submitted data, in the field custom_field_key. If an existing tag is found with that value, the tag will be applied. If not, an API call is sent to the CRM to create the tag, and then the tag is applied.

function create_tags_from_field_values( $user_meta, $user_id ) {

	if ( isset( $user_meta['custom_field_key'] ) ) {

		$tag_id = wpf_get_tag_id( $user_meta['custom_field_key'] );

		if ( $tag_id ) {
			wp_fusion()->user->apply_tags( array( $tag_id ), $user_id );
		} else {

			// Try to create the tag.
			wpf_log( 'info', $user_id, 'Creating new tag for field value ' . $user_meta['custom_field_key'] );

			try {
				$tag_id = wp_fusion()->crm->add_tag( $user_meta['custom_field_key'] );
			} catch ( Exception $e ) {
				wpf_log( 'error', $user_id, 'Error creating tag for field value ' . $user_meta['custom_field_key'] . ': ' . $e->getMessage() );
			}
			
			if ( ! is_wp_error( $tag_id ) ) {
				wp_fusion()->user->apply_tags( array( $tag_id ), $user_id );
			}
		}
	}

}

add_filter( 'wpf_user_update', 'create_tags_from_field_values', 10, 2 );

Was this helpful?