{"id":130,"date":"2025-02-06T12:40:04","date_gmt":"2025-02-06T12:40:04","guid":{"rendered":"https:\/\/documentation.userswp.io\/article\/userswp-function-uwp_get_usermetauser_id-field_key\/"},"modified":"2025-03-26T17:07:50","modified_gmt":"2025-03-26T17:07:50","slug":"userswp-function-uwp_get_usermetauser_id-field_key","status":"publish","type":"gd_place","link":"https:\/\/userswp.io\/documentation\/article\/userswp-functions\/userswp-function-uwp_get_usermetauser_id-field_key\/","title":{"rendered":"UsersWP Function: uwp_get_usermeta($user_id, $field_key)"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote\">\n<h2 class=\"wp-block-heading\">Is there a PHP function \/ hook like \u201cget_user_meta\u201d to get the values stored in a UsersWP account custom field given the used ID and the field name?<\/h2>\n<\/blockquote>\n\n\n\n<p>Yes, there is, the<br><code>uwp_get_usermeta()<\/code> 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.<\/p>\n\n\n\n<div class=\"wp-block-blockstrap-blockstrap-widget-alert d-flex align-items-center fade show alert alert-info mb-3\" role=\"alert\"><span class=\"fas fa-info-circle me-2\"><\/span><span class=\"flex-grow-1\"><strong>This doc will go over our own function, however, you can use the WordPress default function if you prefer<\/strong><\/span><\/div>\n\n\n\n<p>If you prefer to use the standard WP <code>get_user_meta()<\/code> function, then you can \ud83d\ude42 Simply prefix the field_key with &#8220;uwp_meta_&#8221; so to get a field `phone_number` you can use <code>get_user_meta( $user_id, &#039;uwp_meta_phone_number&#039;, true );<\/code><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Function Syntax<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-php\">uwp_get_usermeta( int $user_id, string $field_key );<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Parameters<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>$user_id<\/code><\/strong> <em>(int)<\/em>: The ID of the user whose meta data you want to retrieve.<\/li>\n\n\n\n<li><strong><code>$field_key<\/code><\/strong> <em>(string)<\/em>: The key (or name) of the custom field you want to access.<\/li>\n\n\n\n<li><code>$default<\/code>\u00a0<em>(string)<\/em>: (optional) The default value to return if the value is empty.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Return Value<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Mixed<\/strong>: Returns the value of the specified user meta field.<\/li>\n\n\n\n<li>Returns <code>null<\/code> if the field does not exist or has no value.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Function Description<\/strong><\/h2>\n\n\n\n<p>The<br><code>uwp_get_usermeta()<\/code> function retrieves the value of a custom user meta field added by UsersWP. Unlike the standard WordPress <code>get_user_meta()<\/code> function, <code>uwp_get_usermeta()<\/code> is tailored to handle UsersWP-specific fields, including complex data types and serialized arrays.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Usage Examples<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example 1: Retrieve a User&#8217;s Phone Number<\/strong><\/h3>\n\n\n\n<p>Suppose you have a custom field with the key<br><code>phone_number<\/code> added to your registration form.<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-php\">$user_id = 123; \/\/ Replace with the actual user ID \n$phone_number = uwp_get_usermeta( $user_id, &#039;phone_number&#039; ); \nif ( $phone_number ) { \n\techo &#039;User Phone Number: &#039; . esc_html( $phone_number ); \n} else { \n\techo &#039;Phone number not provided.&#039;; \n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example 2: Display User Profile Information<\/strong><\/h3>\n\n\n\n<p>Retrieve and display multiple custom fields for a user&#8217;s profile page.<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-php\">$user_id = get_current_user_id(); \n$first_name = uwp_get_usermeta( $user_id, &#039;first_name&#039; ); \n$last_name = uwp_get_usermeta( $user_id, &#039;last_name&#039; ); \n$bio = uwp_get_usermeta( $user_id, &#039;bio&#039; ); \necho &#039;&lt;h2&gt;&#039; . esc_html( $first_name . &#039; &#039; . $last_name ) . &#039;&lt;\/h2&gt;&#039;; echo &#039;&lt;p&gt;&#039; . esc_html( $bio ) . &#039;&lt;\/p&gt;&#039;;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example 3: Conditional Content Based on User Meta<\/strong><\/h3>\n\n\n\n<p>Show exclusive content to users with a specific role or meta value.<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-php\">$user_id = get_current_user_id(); \n$membership_level = uwp_get_usermeta( $user_id, &#039;membership_level&#039; ); \nif ( $membership_level === &#039;premium&#039; ) { \n\techo &#039;Welcome to the premium content section!&#039;; \n} else { \n\techo &#039;Upgrade to premium to access this content.&#039;; \n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example 4: Loop Through Users to Display Custom Meta<\/strong><\/h3>\n\n\n\n<p>List all users along with a specific custom field.<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-php\">$users = get_users(); \nforeach ( $users as $user ) { \n$user_id = $user-&gt;ID; \n$company = uwp_get_usermeta( $user_id, &#039;company_name&#039; ); \necho &#039;User: &#039; . esc_html( $user-&gt;display_name ) . &#039;&lt;br&gt;&#039;; echo &#039;Company: &#039; . esc_html( $company ) . &#039;&lt;br&gt;&lt;br&gt;&#039;; \n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example 5: Prefill Form Fields with User Meta<\/strong><\/h3>\n\n\n\n<p>When creating forms, prefill fields with existing user data.<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-php\">$user_id = get_current_user_id(); \n$address = uwp_get_usermeta( $user_id, &#039;address&#039; ); \n?&gt; \n&lt;form method=&quot;post&quot; action=&quot;&quot;&gt; \n\t&lt;label for=&quot;address&quot;&gt;Address:&lt;\/label&gt; \n\t&lt;input type=&quot;text&quot; name=&quot;address&quot; id=&quot;address&quot; value=&quot;&lt;?php echo esc_attr( $address ); ?&gt;&quot;&gt; \n\t&lt;input type=&quot;submit&quot; value=&quot;Update Address&quot;&gt; \n&lt;\/form&gt; \n&lt;?php<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Notes and Best Practices<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Field Key Accuracy<\/strong>: Ensure that the <code>$field_key<\/code> matches the exact key used when the custom field was created in UsersWP.<\/li>\n\n\n\n<li><strong>Sanitize Output<\/strong>: Always sanitize and escape output to prevent security vulnerabilities.<br><pre>$value = uwp_get_usermeta( $user_id, 'custom_field' ); <br>echo esc_html( $value );<\/pre><\/li>\n\n\n\n<li><strong>Check for Null Values<\/strong>: The function may return <code>null<\/code> if the field does not exist. Implement checks to handle such cases.<br><pre>$value = uwp_get_usermeta( $user_id, 'custom_field' ); <br>if ( $value !== null ) { \/\/ Proceed with using $value } else { \/\/ Handle the absence of the field }<br>\t<\/pre><br><\/li>\n\n\n\n<li><strong>Difference from <code>get_user_meta()<\/code><\/strong>: While WordPress&#8217;s <code>get_user_meta()<\/code>\u00a0retrieve user metadata from the wp_usermeta table of the WordPress database, <code>uwp_get_usermeta()<\/code> is optimized for UsersWP fields stored in the UsersWP custom database table.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Related Functions<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>uwp_update_usermeta( $user_id, $field_key, $value )<\/code><\/strong>: Updates or adds a custom user meta field.<\/li>\n\n\n\n<li><strong><code>uwp_delete_usermeta( $user_id, $field_key )<\/code><\/strong>: Deletes a custom user meta field.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Troubleshooting<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Function Not Working<\/strong>: Ensure that the UsersWP plugin is installed and activated.<\/li>\n\n\n\n<li><strong>Incorrect Field Key<\/strong>: Double-check the <code>$field_key<\/code> for typos or case sensitivity.<\/li>\n\n\n\n<li><strong>Permission Issues<\/strong>: Make sure you have the necessary permissions to access the user&#8217;s data.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Additional Examples<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example 6: Display User Meta in the Admin Users List<\/strong><\/h3>\n\n\n\n<p>Add a custom column to the WordPress admin users list to display a UsersWP custom field.<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-php\">\/\/ Add a new column to the users table \nadd_filter( &#039;manage_users_columns&#039;, &#039;add_company_column&#039; ); \nfunction add_company_column( $columns ) { \n\t$columns[&#039;company_name&#039;] = &#039;Company Name&#039;; return $columns; \n} \n\n\/\/ Populate the custom column with data \nadd_action( &#039;manage_users_custom_column&#039;, &#039;show_company_column_data&#039;, 10, 3 ); \nfunction show_company_column_data( $value, $column_name, $user_id ) { \n\tif ( &#039;company_name&#039; === $column_name ) { \n\t\treturn esc_html( uwp_get_usermeta( $user_id, &#039;company_name&#039; ) ); \n\t} \n\n\treturn $value; \n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example 7: Fetching Serialized Data<\/strong><\/h3>\n\n\n\n<p>If a custom field stores serialized data (e.g., an array),<br><code>uwp_get_usermeta()<\/code> will handle it correctly.<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-php\">$user_id = get_current_user_id(); \n$preferences = uwp_get_usermeta( $user_id, &#039;preferences&#039; ); \nif ( is_array( $preferences ) ) { \n\tforeach ( $preferences as $preference ) { \n\t\techo esc_html( $preference ) . &#039;&lt;br&gt;&#039;; \n\t} \n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Use Cases<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Profile Pages<\/strong>: Display extended user information on profile or author pages.<\/li>\n\n\n\n<li><strong>Custom Dashboards<\/strong>: Build user-specific dashboards based on custom meta data.<\/li>\n\n\n\n<li><strong>Form Prefilling<\/strong>: Pre-populate forms with existing user data for updates or edits.<\/li>\n\n\n\n<li><strong>Conditional Logic<\/strong>: Adjust content visibility or options based on user meta values.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Further Reading and Resources<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>UsersWP Documentation<\/strong>: <a href=\"https:\/\/userswp.io\/documentation\/\">https:\/\/userswp.io\/documentation\/<\/a><\/li>\n\n\n\n<li><strong>GitHub Repository<\/strong>: <a href=\"https:\/\/github.com\/AyeCode\/userswp\">UsersWP on GitHub<\/a><\/li>\n\n\n\n<li><strong>UsersWP Support<\/strong>: <a href=\"https:\/\/userswp.io\/support\/\">UsersWP Support<\/a><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>By utilizing the<br><code>uwp_get_usermeta()<\/code> function, you can effectively manage and display custom user meta data within your WordPress site, enhancing user profiles and creating a more personalized experience.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Is there a PHP function \/ hook like \u201cget_user_meta\u201d to get the values stored in a UsersWP account custom field given the used ID and the field name? Yes, there is, theuwp_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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"footnotes":""},"article\/tags":[],"article\/categories":[40,42,7],"class_list":["post-130","gd_place","type-gd_place","status-publish","hentry","gd_placecategory-code-snippets","gd_placecategory-userswp-functions","gd_placecategory-tips-tricks-more"],"_links":{"self":[{"href":"https:\/\/userswp.io\/documentation\/wp-json\/wp\/v2\/article\/130","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/userswp.io\/documentation\/wp-json\/wp\/v2\/article"}],"about":[{"href":"https:\/\/userswp.io\/documentation\/wp-json\/wp\/v2\/types\/gd_place"}],"author":[{"embeddable":true,"href":"https:\/\/userswp.io\/documentation\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/userswp.io\/documentation\/wp-json\/wp\/v2\/comments?post=130"}],"version-history":[{"count":0,"href":"https:\/\/userswp.io\/documentation\/wp-json\/wp\/v2\/article\/130\/revisions"}],"wp:attachment":[{"href":"https:\/\/userswp.io\/documentation\/wp-json\/wp\/v2\/media?parent=130"}],"wp:term":[{"taxonomy":"gd_place_tags","embeddable":true,"href":"https:\/\/userswp.io\/documentation\/wp-json\/wp\/v2\/article\/tags?post=130"},{"taxonomy":"gd_placecategory","embeddable":true,"href":"https:\/\/userswp.io\/documentation\/wp-json\/wp\/v2\/article\/categories?post=130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}