I have a plugin that adds custom fields to the WooCommerce account page. Take the apikey field below as an example (this is just a random field, it could be anything)
add_action( 'woocommerce_edit_account_form', 'add_api_info_to_account_form' ); function add_api_info_to_account_form() { $user = wp_get_current_user(); ?> <fieldset> <legend><?php esc_html_e( 'GHL/MBO Sync', 'woocommerce' ); ?></legend> <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide"> <label for="apikey"><?php _e( 'MBO API Key', 'woocommerce' ); ?></label> <input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="apikey" id="apikey" value="<?php echo esc_attr( $user->apikey ); ?>" /> </p>
Whenever I go to the profile page, I see that the value $user->apikey is loaded from wp_get_current_user()
However, in my other file, I’m trying to access every user’s API key using get_users(), and the apikey information is not returned.
Is there a way to return custom fields like this with an array of all users?
Here’s the code from the second file:
$users = get_users(); print_r($users);
it returns a big array, but no apikey field
Advertisement
Answer
Welcome to stackoverflow, TO load a user meta (custom field) you need ot use this function get_user_meta($user_id, $meta_key ).
So to put it simple, to get the user API key to use this line
global $current_user; $user_api_key = get_user_meta($current_user->id, 'apikey' );
You can place $user_api_key anywhere you want to display the apikey. Please try this out and let me know if it works for you. I’m here to help.