[wp-hackers] array_push on update_user_meta

J.D. Grimes jdg at codesymphony.co
Thu Oct 31 20:40:25 UTC 2013


No, you don’t need to use unserialize, that is all done automatically by the WordPress functions. 

In the code you posted you still aren’t calling get_user_meta() with the third parameter set to true. You need to do that. Right now the array it is returning looks something like this:

array(
	0 => array( 3, 45, 6, 33 )
)

When you call in_array() it is checking if the user ID (say, 3 ) is in the outer array. But it isn’t, the only thing in it is another array. When you call get_user_meta() with the third parameter set to true, the returned value won’t be wrapped in the outer array like that, so it will work.


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

> Ah I see. Thank you for explaining JD.  It sounds like maybe the easier
> option is to just create a separate table to store the friends list in.
> Trying to work with user_meta this way seems like a bad idea.
> 
> Quick question, should I be using unserialize to check the array values
> using in_array?  Because right now this code isnt working.  Thats the only
> thing that seems to be left out - but when I tried using $unserialize like
> this:
> 
> $unserialize = unserialize($active_user_friends);
>  if (in_array($author_id, $unserialize)) {
> 
> it says this:
> 
> 
> *Warning*: unserialize() expects parameter 1 to be string, array given
> and
> *Warning*: in_array() expects parameter 2 to be array, boolean given
> 
> 
> 
> 
> --------
> 
> 
> 
> add_action('wp_ajax_my_add_friend_action', 'my_add_friend_action');
> add_action('wp_ajax_nopriv_my_add_friend_action', 'my_add_friend_action');
> function my_add_friend_action() {
> global $wpdb;
> $profileID = $_POST['profileID'];
> $userID = $_POST['userID'];
> 
> $chkMetaValue = get_user_meta($userID,"friends");
> if ( ! is_array($chkMetaValue) )
> $chkMetaValue = array();
> $chkMetaValue[] = $profileID;
> 
> // activity_tracker($current_user, 'added_friend', 'prof');
> update_user_meta( $userID, 'friends', $chkMetaValue );
> $response = array( 'success' => true );
> wp_send_json_success($response);
> 
> die();
> }
> 
> 
> 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, true)) {
>  $friend_status = "<button id='remove_friend'>Remove Friend</button>";
>  } else {
>  $friend_status = "<button id='add_friend'>Add Friend</button>";
>  }
> 
> return $friend_status;
> }
> 
> 
> That is basically the code I am using to insert the users id into their
> user_meta table.
> 
> 
> On Thu, Oct 31, 2013 at 1:08 PM, J.D. Grimes <jdg at codesymphony.co> wrote:
> 
>> 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
>> 
>> _______________________________________________
>> 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