PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, May 15, 2022

[FIXED] Where is the data stored for custom fields in WooCommerce/Wordpress

 May 15, 2022     php, plugins, wordpress, wordpress-hook     No comments   

Issue

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


Solution

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.



Answered By - Mohamed Ali O.Ameur
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing