Using update_user_meta with an array
-
I’m building a plugin where I’m trying to use a user meta field to store a history of user activity. I’m having a hard time wrapping my head around how to update and store the array of data.
So, my question is: can somebody tell how to update my code? Or, would it be simpler to create a table with this data? Create a new post type?
(I thought a user meta field would be better, so that I wouldn’t clutter the database with another table, but maybe my reasoning is off.)Anyway, here’s what I’m trying to accomplish:
1 – In the front end, the user chooses from a drop-down menu their selection for the day. For example: “Today, I ate an APPLE or ORANGE or BANANA”.
2 – The plugin saves the vote of fruit for the day in the user meta field I callmyplugin-hist.
3 – The next day, if the user chooses a new fruit, the plugin adds this new fruit to themyplugin-histso that we can see what the choice was yesterday and again today.
4 – I’ve tried various ways of storing the data, and I get arrays within arrays, not resembling the structure I have in mind, which should ideally have this structure:array( day 1 => apple day 2 => banana day 3 => apple )Here’s a sample of my code. So far, I’ve written just the code to save to the database (which is where I need help.)
// This function receives the (int)$fruit vote from another function // (apple is 1, banana is 2, etc) public function i_vote_for($fruit) { global $MainPluginClass; // First, we obtain the user's vote history $hist = get_user_meta($this->ID, 'myplugin-hist', false); // I added this, in case it's a brand-new user with no history if (!array($hist)) $hist = array(); // This is a custom function that returns an int, to count day 1, day 2, etc $d = $MainPluginClass->current_date(); //This is the part where I'm scrathcing my head. //I've tried $hist[$d][] = $fruit; //I've tried $hist[] = array ($d, fruit); $hist[0][] = $fruit; update_user_meta($this->ID, 'myplugin-hist', $hist); // the $this->ID is a shorthand in my plugin for get_current_user... }-
This topic was modified 8 years ago by
jd-fb.
-
This topic was modified 8 years ago by
-
No need to explicitly fetch an array, you can do it like this
$hist = get_user_meta($this->ID, 'myplugin-hist', true);
or else the meta value will be wrapped in another array.This is not the condition you want to check,
if (!array($hist))
change it to,
if (!is_array($hist))Finally, store your value like this,
$hist[$d] = $fruit;Lets put this all together,
// This function receives the (int)$fruit vote from another function // (apple is 1, banana is 2, etc) public function i_vote_for($fruit) { global $MainPluginClass; // First, we obtain the user's vote history $hist = get_user_meta($this->ID, 'myplugin-hist', true); // I added this, in case it's a brand-new user with no history if (!is_array($hist)) $hist = array(); // This is a custom function that returns an int, to count day 1, day 2, etc $d = $MainPluginClass->current_date(); //This is the part where I'm scrathcing my head. $hist[$d] = $fruit; update_user_meta($this->ID, 'myplugin-hist', $hist); // the $this->ID is a shorthand in my plugin for get_current_user... }-
This reply was modified 8 years ago by
Soumanta Bhowmick.
Thanks for the info.
I do a var_dump of the array after saving it, and I’m still getting variations ofarray( array( day 1=>apple day 2=>banana day 3=>orange ) )(An array inside of an array. This makes it difficult to use foreach to iterate through the data.)
I’ve just re-read the Codex for
add_user_meta, and it looks like I could use that to add multiple lines to the database with my myplugin-hist meta key. If I useadd_user_metawith $unique set to false, then WP adds my meta key as many times as I need.I think I’ll try that to see how it works.
-
This reply was modified 8 years ago by
The topic ‘Using update_user_meta with an array’ is closed to new replies.