[wp-hackers] array_push on update_user_meta

J.D. Grimes jdg at codesymphony.co
Thu Oct 31 20:08:51 UTC 2013


There are two ways to use the *_meta functions. The way it looks like you are doing it is that each user has a single entry in the usermeta table, with the key ‘friends’. The value for this meta key will be stored in the database as a serialized array, and when it is pulled out (by get_user_meta()) it gets unserialized into a PHP array. 

Alternative is this: Each user has multiple ‘friend’ meta key rows in the usermeta table, each one’s meta_value the ID of a single user that the user has friended.

The benefit of the second option is that the values may be easier to search. Much easier. But that may not be important to you right now. (But then that could change later…) What if you want to show a list of users who have friended a user? You will be able to do that easily with this. With the first option, it is possible, but more complex and probably harder on the DB.

The downside is that there are many more rows in usermeta table.

So each of these functions let you target a single row (as in first case) or all rows with a key (as in the later case). Whichever way you go, you just need to be consistent through all of the code, obviously.

-J.D.

On Oct 31, 2013, at 3:30 PM, Gregory Lancaster <greglancaster71 at gmail.com> wrote:

> What would be the benefit of storing the friends in separate rows? I did
> not know that was an option, but don't see any clear advantages either.
> Also, I did just search the codex but dont see anything about unique rows.
> (I checked get_user_meta, delete_user_meta, update_user_meta. )
> 
> 
> On Thu, Oct 31, 2013 at 12:24 PM, J.D. Grimes <jdg at codesymphony.co> wrote:
> 
>> Yes, when you call get_user_meta() you need to set the third parameter
>> ($single) to true:
>> 
>> $active_user_friends = get_user_meta( $active_user, ‘friends’, true );
>> 
>> Otherwise it will return a nested array. Also, just FYI, alternatively you
>> could leave the get_user_meta() call the way it is, and change it so that
>> each friend is stored in a separate meta row (but with the same meta key -
>> have a look at the *_user_meta functions on the codex).
>> 
>> -J.D.
>> 
>> On Oct 31, 2013, at 3:14 PM, Gregory Lancaster <greglancaster71 at gmail.com>
>> wrote:
>> 
>>> I thought so.  One more question if you dont mind helping;
>>> 
>>> This is the function I wrote to determine what button to show on page
>> load.
>>> But regardless if someone has been friended or not, it shows the add
>>> friend option.  Is something wrong with this?
>>> 
>>> 
>>> function friend_status($author_id) {
>>> global $wpdb, $current_user;
>>>     get_currentuserinfo();
>>> 
>>> //authorID is the userID of the profile owner
>>> 
>>> $active_user = $current_user->ID;
>>> $active_user_friends = get_user_meta($active_user, 'friends');
>>> 
>>> if (in_array($author_id, $active_user_friends)) {
>>> $friend_status = "<button id='remove_friend'>Remove Friend</button>";
>>> } else {
>>> $friend_status = "<button id='add_friend'>Add Friend</button>";
>>> }
>>> 
>>> return $friend_status;
>>> }
>>> 
>>> Then on page:  echo $friend_status;
>>> 
>>> 
>>> On Thu, Oct 31, 2013 at 11:53 AM, J.D. Grimes <jdg at codesymphony.co>
>> wrote:
>>> 
>>>> Yes, I would write a separate function for removal, and hook it to a
>>>> different AJAX action for removal.
>>>> 
>>>> On Oct 31, 2013, at 2:48 PM, Gregory Lancaster <
>> greglancaster71 at gmail.com>
>>>> wrote:
>>>> 
>>>>> That works easier :)  Once someone is friended I have it set so an
>>>> unfriend
>>>>> button replaces the add friend button via ajax.  Is it necessary to
>> write
>>>>> another function for deleting a person?  Not sure exactly how to make
>> the
>>>>> new button function since the data being sent is not attached to the
>>>> button
>>>>> in any way.  maybe I could add a value to the button that says remove
>> or
>>>>> add, which would be passed to the function and determine what action to
>>>>> take?
>>>>> 
>>>>> 
>>>>> On Thu, Oct 31, 2013 at 11:32 AM, J.D. Grimes <jdg at codesymphony.co>
>>>> wrote:
>>>>> 
>>>>>> I would do this:
>>>>>> 
>>>>>> $profileID = $_POST['profileID'];
>>>>>> 
>>>>>> $chkMetaValue = get_user_meta($userID,"friends");
>>>>>> 
>>>>>> if ( ! is_array($chkMetaValue) )
>>>>>>      $chkMetaValue = array();
>>>>>> 
>>>>>> $chkMetaValue[] = $profileID; // or use array_push()
>>>>>> 
>>>>>> update_user_meta( $userID, 'friends', $chkMetaValue );
>>>>>> 
>>>>>> -J.D.
>>>>>> 
>>>>>> On Oct 31, 2013, at 1:32 PM, BenderisGreat <greglancaster71 at gmail.com
>>> 
>>>>>> wrote:
>>>>>> 
>>>>>>> I am not sure exactly how this would work because I start with an
>> empty
>>>>>>> meta_value field.  I dont think I can use array_push if there is not
>> at
>>>>>>> least one value in the field, correct?
>>>>>>> 
>>>>>>> So maybe something like this:
>>>>>>> 
>>>>>>> $profileID = $_POST['profileID'];
>>>>>>> 
>>>>>>> $chkMetaValue = get_user_meta($userID,"friends");
>>>>>>>    if (!empty($chkMetaValue))
>>>>>>>            {
>>>>>>>            array_push($profileID);
>>>>>>>            } else {
>>>>>>>            $profileID;
>>>>>>>            }
>>>>>>> 
>>>>>>> update_user_meta( $userID, 'friends', $chkMetaValue );
>>>>>>> 
>>>>>>> Is that right?
>>>>>>> 
>>>>>>> 
>>>>>>> 
>>>>>>> 
>>>>>>> --
>>>>>>> View this message in context:
>>>>>> 
>>>> 
>> http://wordpress-hackers.1065353.n5.nabble.com/array-push-on-update-user-meta-tp42688.html
>>>>>>> Sent from the Wordpress Hackers mailing list archive at Nabble.com.
>>>>>>> _______________________________________________
>>>>>>> wp-hackers mailing list
>>>>>>> wp-hackers at lists.automattic.com
>>>>>>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>>>>> 
>>>>>> _______________________________________________
>>>>>> wp-hackers mailing list
>>>>>> wp-hackers at lists.automattic.com
>>>>>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>>>>> 
>>>>> _______________________________________________
>>>>> wp-hackers mailing list
>>>>> wp-hackers at lists.automattic.com
>>>>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>>> 
>>>> _______________________________________________
>>>> wp-hackers mailing list
>>>> wp-hackers at lists.automattic.com
>>>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>>> 
>>> _______________________________________________
>>> wp-hackers mailing list
>>> wp-hackers at lists.automattic.com
>>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>> 
>> _______________________________________________
>> wp-hackers mailing list
>> wp-hackers at lists.automattic.com
>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>> 
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers



More information about the wp-hackers mailing list