2

I have set up a user profile field called "Testing" though the woocommerce membership panel.

enter image description here

I now want to access this field and its data on the front-end of the website.

I looked up the documentation and the closest function I could find is wc_memberships_get_members_area_sections() which will list only the name of the sections within the membership dashboard and nothing about the content that it contains.

I also looked in the database and found that the user submitted data is stored in the wp_usermeta table.

So how do I access this data? I want to add three fields i.e. name, age and picture for the user to fill-in and display them back on the front-end pages when the user is logged-in.

Vinith Almeida
  • 1,421
  • 3
  • 13
  • 32

2 Answers2

2

The wordpress function get_user_meta() will work to fetch the profile field data.

$data = get_user_meta(get_current_user_id());

The profile field data is returned as an array and you can use var_dump() function to check the returned data.

var_dump($data);

I had a "company_name" profile field so I used the code below to fetch it,

if ( $data['_wc_memberships_profile_field_company_name'][0] ) {
    $companyName = $data['_wc_memberships_profile_field_company_name'][0];
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Vinith Almeida
  • 1,421
  • 3
  • 13
  • 32
  • You can also simplify this by accessing the field directly and using the $single param. That way you don't need to access the first item in the array ( the [0] ) eg: get_user_meta( get_current_user_id(), '_wc_memberships_profile_field_company_name', true); see https://developer.wordpress.org/reference/functions/get_user_meta/ – Chris Chalmers Apr 01 '22 at 15:36
1

I created a helper function to mimic how Woo Memberships saves the data in the wp_user_meta. Pass in the user ID and the 'slug' property with a hyphen as your $meta_key order to access the meta value of a given profile field.

function get_membership_meta($user_id, $meta_key){
    
    $meta_key_prefix = '_wc_memberships_profile_field_';

    return get_user_meta($user_id, $meta_key_prefix . str_replace( '-', '_', $meta_key), true);

} 

Ex

$company_name = get_membership_meta($post_author_id, 'company-name')
trevb
  • 11
  • 1