skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    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];
    }
    

  2. 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')
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search