[wp-hackers] array_push on update_user_meta
J.D. Grimes
jdg at codesymphony.co
Thu Oct 31 19:24:16 UTC 2013
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
More information about the wp-hackers
mailing list