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
2
Answers
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
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.
WordPress uses the wp_users table to store user data in its database. Only the most basic user information is stored in wp_users table. It includes just the username (user_login), password (user_pass) and email (user_email) of each user. In order to store additional user data, the wp_usermeta table is used. The ID field from the users table and the user_id field from wp_usermeta table are used to link the record of the same user.
To retrieve user meta you can use the get_user_meta function.
For more details please review the WordPress documentation https://developer.wordpress.org/reference/functions/get_user_meta/