UsersWP Function: uwp_get_usermeta($user_id, $field_key)

Is there a PHP function / hook like “get_user_meta” to get the values stored in a UsersWP account custom field given the used ID and the field name?

Yes, there is, the uwp_get_usermeta() function allows developers to retrieve custom user metadata associated with a specific user. This is particularly useful when working with custom fields added via UsersWP registration or profile forms.

This doc will go over our own function, however, you can use the WordPress default function if you prefer

If you prefer to use the standard WP get_user_meta() function, then you can :) Simply prefix the field_key with "uwp_meta_" so to get a field `phone_number` you can use get_user_meta( $user_id, 'uwp_meta_phone_number', true );


Function Syntax

uwp_get_usermeta( int $user_id, string $field_key )

Parameters

  • $user_id (int): The ID of the user whose meta data you want to retrieve.
  • $field_key (string): The key (or name) of the custom field you want to access.
  • $default (string): (optional) The default value to return if the value is empty.

Return Value

  • Mixed: Returns the value of the specified user meta field.
  • Returns null if the field does not exist or has no value.

Function Description

The uwp_get_usermeta() function retrieves the value of a custom user meta field added by UsersWP. Unlike the standard WordPress get_user_meta() function, uwp_get_usermeta() is tailored to handle UsersWP-specific fields, including complex data types and serialized arrays.


Usage Examples

Example 1: Retrieve a User's Phone Number

Suppose you have a custom field with the key phone_number added to your registration form.

$user_id = 123; // Replace with the actual user ID 
$phone_number = uwp_get_usermeta( $user_id, 'phone_number' );
if ( $phone_number ) {
echo 'User Phone Number: ' . esc_html( $phone_number ); } else {
echo 'Phone number not provided.'; }

Example 2: Display User Profile Information

Retrieve and display multiple custom fields for a user's profile page.

$user_id = get_current_user_id(); 
$first_name = uwp_get_usermeta( $user_id, 'first_name' );
$last_name = uwp_get_usermeta( $user_id, 'last_name' );
$bio = uwp_get_usermeta( $user_id, 'bio' );
echo '<h2>' . esc_html( $first_name . ' ' . $last_name ) . '</h2>'; echo '<p>' . esc_html( $bio ) . '</p>';

Example 3: Conditional Content Based on User Meta

Show exclusive content to users with a specific role or meta value.

$user_id = get_current_user_id(); 
$membership_level = uwp_get_usermeta( $user_id, 'membership_level' );
if ( $membership_level === 'premium' ) { echo 'Welcome to the premium content section!'; } else { echo 'Upgrade to premium to access this content.'; }

Example 4: Loop Through Users to Display Custom Meta

List all users along with a specific custom field.

$users = get_users(); 
foreach ( $users as $user ) { 
$user_id = $user->ID; 
$company = uwp_get_usermeta( $user_id, 'company_name' ); 
echo 'User: ' . esc_html( $user->display_name ) . '<br>'; echo 'Company: ' . esc_html( $company ) . '<br><br>'; 
}

Example 5: Prefill Form Fields with User Meta

When creating forms, prefill fields with existing user data.

$user_id = get_current_user_id(); 
$address = uwp_get_usermeta( $user_id, 'address' ); 
?> 
<form method="post" action=""> 
	<label for="address">Address:</label> 
	<input type="text" name="address" id="address" value="<?php echo esc_attr( $address ); ?>"> 
	<input type="submit" value="Update Address"> 
</form> 
<?php

Notes and Best Practices

  • Field Key Accuracy: Ensure that the $field_key matches the exact key used when the custom field was created in UsersWP.
  • Sanitize Output: Always sanitize and escape output to prevent security vulnerabilities.
    $value = uwp_get_usermeta( $user_id, 'custom_field' ); 
    echo esc_html( $value );
  • Check for Null Values: The function may return null if the field does not exist. Implement checks to handle such cases.
    $value = uwp_get_usermeta( $user_id, 'custom_field' ); 
    if ( $value !== null ) { // Proceed with using $value } else { // Handle the absence of the field }
    	
  • Difference from get_user_meta(): While WordPress's get_user_meta() retrieve user metadata from the wp_usermeta table of the WordPress database, uwp_get_usermeta() is optimized for UsersWP fields stored in the UsersWP custom database table.

Related Functions

  • uwp_update_usermeta( $user_id, $field_key, $value ): Updates or adds a custom user meta field.
  • uwp_delete_usermeta( $user_id, $field_key ): Deletes a custom user meta field.

Troubleshooting

  • Function Not Working: Ensure that the UsersWP plugin is installed and activated.
  • Incorrect Field Key: Double-check the $field_key for typos or case sensitivity.
  • Permission Issues: Make sure you have the necessary permissions to access the user's data.

Additional Examples

Example 6: Display User Meta in the Admin Users List

Add a custom column to the WordPress admin users list to display a UsersWP custom field.

// Add a new column to the users table 
add_filter( 'manage_users_columns', 'add_company_column' ); 
function add_company_column( $columns ) { 
	$columns['company_name'] = 'Company Name'; return $columns; 
} 

// Populate the custom column with data 
add_action( 'manage_users_custom_column', 'show_company_column_data', 10, 3 ); 
function show_company_column_data( $value, $column_name, $user_id ) { 
	if ( 'company_name' === $column_name ) { 
		return esc_html( uwp_get_usermeta( $user_id, 'company_name' ) ); 
	} 

	return $value; 
}

Example 7: Fetching Serialized Data

If a custom field stores serialized data (e.g., an array), uwp_get_usermeta() will handle it correctly.

$user_id = get_current_user_id(); 
$preferences = uwp_get_usermeta( $user_id, 'preferences' ); 
if ( is_array( $preferences ) ) { 
	foreach ( $preferences as $preference ) { 
		echo esc_html( $preference ) . '<br>'; 
	} 
}

Use Cases

  • Profile Pages: Display extended user information on profile or author pages.
  • Custom Dashboards: Build user-specific dashboards based on custom meta data.
  • Form Prefilling: Pre-populate forms with existing user data for updates or edits.
  • Conditional Logic: Adjust content visibility or options based on user meta values.

Further Reading and Resources


By utilizing the uwp_get_usermeta() function, you can effectively manage and display custom user meta data within your WordPress site, enhancing user profiles and creating a more personalized experience.

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.