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:
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.
Post by Gregory LancasterAh 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
$unserialize = unserialize($active_user_friends);
if (in_array($author_id, $unserialize)) {
*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.
Post by J.D. GrimesThere 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.
Post by Gregory LancasterWhat 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.
Post by Gregory Lancaster(I checked get_user_meta, delete_user_meta, update_user_meta. )
Post by J.D. GrimesYes, when you call get_user_meta() you need to set the third parameter
$active_user_friends = get_user_meta( $active_user, ‘friends’, true );
Otherwise it will return a nested array. Also, just FYI, alternatively
you
Post by Gregory LancasterPost by J.D. Grimescould leave the get_user_meta() call the way it is, and change it so
that
Post by Gregory LancasterPost by J.D. Grimeseach friend is stored in a separate meta row (but with the same meta
key -
Post by Gregory LancasterPost by J.D. Grimeshave a look at the *_user_meta functions on the codex).
-J.D.
On Oct 31, 2013, at 3:14 PM, Gregory Lancaster <
Post by Gregory LancasterI 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.
Post by Gregory LancasterBut 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;
Post by J.D. GrimesYes, 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 <
Post by Gregory LancasterThat works easier :) Once someone is friended I have it set so an
unfriend
Post by Gregory Lancasterbutton replaces the add friend button via ajax. Is it necessary to
write
the
Post by Gregory LancasterPost by J.D. GrimesPost by Gregory Lancasternew button function since the data being sent is not attached to the
button
Post by Gregory Lancasterin any way. maybe I could add a value to the button that says remove
or
to
Post by Gregory LancasterPost by J.D. GrimesPost by Gregory LancasterPost by J.D. GrimesPost by Gregory Lancastertake?
Post by BenderisGreat$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 <
Post by BenderisGreatI am not sure exactly how this would work because I start with an
empty
not
Post by Gregory LancasterPost by J.D. Grimesat
Post by Gregory LancasterPost by J.D. GrimesPost by Gregory LancasterPost by BenderisGreatPost by BenderisGreatleast one value in the field, correct?
$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?
--
http://wordpress-hackers.1065353.n5.nabble.com/array-push-on-update-user-meta-tp42688.html
Post by Gregory LancasterPost by J.D. GrimesPost by Gregory LancasterPost by J.D. GrimesPost by Gregory LancasterPost by BenderisGreatPost by BenderisGreatSent from the Wordpress Hackers mailing list archive at Nabble.com.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers