Discussion:
array_push on update_user_meta
BenderisGreat
2013-10-31 17:32:34 UTC
Permalink
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.
J.D. Grimes
2013-10-31 18:32:38 UTC
Permalink
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.
Post by BenderisGreat
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?
$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
http://lists.automattic.com/mailman/listinfo/wp-hackers
Gregory Lancaster
2013-10-31 18:48:03 UTC
Permalink
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?
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.
Post by BenderisGreat
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?
$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 BenderisGreat
Sent 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
J.D. Grimes
2013-10-31 18:53:42 UTC
Permalink
Yes, I would write a separate function for removal, and hook it to a different AJAX action for removal.
Post by Gregory Lancaster
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?
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.
Post by BenderisGreat
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?
$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 BenderisGreat
Sent 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
Gregory Lancaster
2013-10-31 19:14:23 UTC
Permalink
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;
Post by J.D. Grimes
Yes, I would write a separate function for removal, and hook it to a
different AJAX action for removal.
Post by Gregory Lancaster
That works easier :) Once someone is friended I have it set so an
unfriend
Post by Gregory Lancaster
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
Post by Gregory Lancaster
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?
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.
Post by BenderisGreat
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?
$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 Lancaster
Post by BenderisGreat
Post by BenderisGreat
Sent 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
J.D. Grimes
2013-10-31 19:24:16 UTC
Permalink
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.
Post by Gregory Lancaster
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;
Post by J.D. Grimes
Yes, I would write a separate function for removal, and hook it to a
different AJAX action for removal.
Post by Gregory Lancaster
That works easier :) Once someone is friended I have it set so an
unfriend
Post by Gregory Lancaster
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
Post by Gregory Lancaster
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?
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.
Post by BenderisGreat
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?
$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 Lancaster
Post by BenderisGreat
Post by BenderisGreat
Sent 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
Gregory Lancaster
2013-10-31 19:30:14 UTC
Permalink
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. )
Post by J.D. Grimes
$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.
Post by Gregory Lancaster
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.
Post by Gregory Lancaster
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;
Post by J.D. Grimes
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 <
Post by Gregory Lancaster
That works easier :) Once someone is friended I have it set so an
unfriend
Post by Gregory Lancaster
button replaces the add friend button via ajax. Is it necessary to
write
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
another function for deleting a person? Not sure exactly how to make
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
new button function since the data being sent is not attached to the
button
Post by Gregory Lancaster
in any way. maybe I could add a value to the button that says remove
or
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
add, which would be passed to the function and determine what action to
take?
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.
Post by BenderisGreat
I am not sure exactly how this would work because I start with an
empty
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
meta_value field. I dont think I can use array_push if there is not
at
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
least 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 Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
Sent 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
J.D. Grimes
2013-10-31 20:08:51 UTC
Permalink
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.
Post by Gregory Lancaster
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. )
Post by J.D. Grimes
$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.
Post by Gregory Lancaster
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.
Post by Gregory Lancaster
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;
Post by J.D. Grimes
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 <
Post by Gregory Lancaster
That works easier :) Once someone is friended I have it set so an
unfriend
Post by Gregory Lancaster
button replaces the add friend button via ajax. Is it necessary to
write
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
another function for deleting a person? Not sure exactly how to make
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
new button function since the data being sent is not attached to the
button
Post by Gregory Lancaster
in any way. maybe I could add a value to the button that says remove
or
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
add, which would be passed to the function and determine what action to
take?
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.
Post by BenderisGreat
I am not sure exactly how this would work because I start with an
empty
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
meta_value field. I dont think I can use array_push if there is not
at
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
least 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 Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
Sent 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
Gregory Lancaster
2013-10-31 20:30:31 UTC
Permalink
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.
Post by J.D. Grimes
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.
Post by Gregory Lancaster
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.
Post by Gregory Lancaster
(I checked get_user_meta, delete_user_meta, update_user_meta. )
Post by J.D. Grimes
Yes, 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 Lancaster
Post by J.D. Grimes
could leave the get_user_meta() call the way it is, and change it so
that
Post by Gregory Lancaster
Post by J.D. Grimes
each friend is stored in a separate meta row (but with the same meta
key -
Post by Gregory Lancaster
Post by J.D. Grimes
have 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 Lancaster
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.
Post by Gregory Lancaster
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;
Post by J.D. Grimes
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 <
Post by Gregory Lancaster
That works easier :) Once someone is friended I have it set so an
unfriend
Post by Gregory Lancaster
button replaces the add friend button via ajax. Is it necessary to
write
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
another function for deleting a person? Not sure exactly how to make
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
new button function since the data being sent is not attached to the
button
Post by Gregory Lancaster
in any way. maybe I could add a value to the button that says remove
or
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
add, which would be passed to the function and determine what action
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
take?
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 BenderisGreat
I am not sure exactly how this would work because I start with an
empty
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
meta_value field. I dont think I can use array_push if there is
not
Post by Gregory Lancaster
Post by J.D. Grimes
at
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
least 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 Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
Sent 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
J.D. Grimes
2013-10-31 20:40:25 UTC
Permalink
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.
Post by Gregory Lancaster
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
$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. Grimes
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.
Post by Gregory Lancaster
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.
Post by Gregory Lancaster
(I checked get_user_meta, delete_user_meta, update_user_meta. )
Post by J.D. Grimes
Yes, 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 Lancaster
Post by J.D. Grimes
could leave the get_user_meta() call the way it is, and change it so
that
Post by Gregory Lancaster
Post by J.D. Grimes
each friend is stored in a separate meta row (but with the same meta
key -
Post by Gregory Lancaster
Post by J.D. Grimes
have 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 Lancaster
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.
Post by Gregory Lancaster
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;
Post by J.D. Grimes
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 <
Post by Gregory Lancaster
That works easier :) Once someone is friended I have it set so an
unfriend
Post by Gregory Lancaster
button replaces the add friend button via ajax. Is it necessary to
write
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
another function for deleting a person? Not sure exactly how to make
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
new button function since the data being sent is not attached to the
button
Post by Gregory Lancaster
in any way. maybe I could add a value to the button that says remove
or
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
add, which would be passed to the function and determine what action
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
take?
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 BenderisGreat
I am not sure exactly how this would work because I start with an
empty
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
meta_value field. I dont think I can use array_push if there is
not
Post by Gregory Lancaster
Post by J.D. Grimes
at
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
least 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 Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
Sent 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
Gregory Lancaster
2013-10-31 20:49:00 UTC
Permalink
I have it set to true:

$active_user_friends = get_user_meta($active_user, 'friends', true);
But the issue is that it only shows the "friended" code for the last user I
added. If I click add on one profile, and then to a second, the first no
longer shows friend added. Even if I have clearly added several to the
array:

a:2:{i:0;a:2:{i:0;a:2:{i:0;a:2:{i:0;a:2:{i:0;a:2:{i:0;s:0:"";i:1;s:2:"16";}i:1;s:2:"12";}i:1;s:2:"13";}i:1;s:2:"12";}i:1;s:1:"3";}i:1;s:2:"13";}
Post by J.D. Grimes
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
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.
Post by Gregory Lancaster
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
Post by Gregory Lancaster
thing that seems to be left out - but when I tried using $unserialize
like
Post by Gregory Lancaster
$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');
Post by Gregory Lancaster
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. Grimes
There are two ways to use the *_meta functions. The way it looks like
you
Post by Gregory Lancaster
Post by J.D. Grimes
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
Post by Gregory Lancaster
Post by J.D. Grimes
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
Post by Gregory Lancaster
Post by J.D. Grimes
usermeta table, each one’s meta_value the ID of a single user that the
user
Post by Gregory Lancaster
Post by J.D. Grimes
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
Post by Gregory Lancaster
Post by J.D. Grimes
then that could change later…) What if you want to show a list of users
who
Post by Gregory Lancaster
Post by J.D. Grimes
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
Post by Gregory Lancaster
Post by J.D. Grimes
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)
Post by Gregory Lancaster
Post by J.D. Grimes
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 <
Post by Gregory Lancaster
What would be the benefit of storing the friends in separate rows? I
did
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
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. Grimes
Yes, 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 Lancaster
Post by J.D. Grimes
could leave the get_user_meta() call the way it is, and change it so
that
Post by Gregory Lancaster
Post by J.D. Grimes
each friend is stored in a separate meta row (but with the same meta
key -
Post by Gregory Lancaster
Post by J.D. Grimes
have 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 Lancaster
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.
Post by Gregory Lancaster
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;
Post by J.D. Grimes
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 <
Post by Gregory Lancaster
That works easier :) Once someone is friended I have it set so an
unfriend
Post by Gregory Lancaster
button replaces the add friend button via ajax. Is it necessary to
write
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
another function for deleting a person? Not sure exactly how to
make
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
new button function since the data being sent is not attached to
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
button
Post by Gregory Lancaster
in any way. maybe I could add a value to the button that says
remove
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
or
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
add, which would be passed to the function and determine what
action
Post by Gregory Lancaster
Post by J.D. Grimes
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
take?
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 BenderisGreat
I am not sure exactly how this would work because I start with an
empty
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
meta_value field. I dont think I can use array_push if there is
not
Post by Gregory Lancaster
Post by J.D. Grimes
at
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
least 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 Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
Sent from the Wordpress Hackers mailing list archive at
Nabble.com.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
_______________________________________________
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
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
J.D. Grimes
2013-10-31 20:52:51 UTC
Permalink
OK, you need to just delete that meta row and start over. It is messed up from before. Do that and see if it works.
Post by Gregory Lancaster
$active_user_friends = get_user_meta($active_user, 'friends', true);
But the issue is that it only shows the "friended" code for the last user I
added. If I click add on one profile, and then to a second, the first no
longer shows friend added. Even if I have clearly added several to the
a:2:{i:0;a:2:{i:0;a:2:{i:0;a:2:{i:0;a:2:{i:0;a:2:{i:0;s:0:"";i:1;s:2:"16";}i:1;s:2:"12";}i:1;s:2:"13";}i:1;s:2:"12";}i:1;s:1:"3";}i:1;s:2:"13";}
Post by J.D. Grimes
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
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.
Post by Gregory Lancaster
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
Post by Gregory Lancaster
thing that seems to be left out - but when I tried using $unserialize
like
Post by Gregory Lancaster
$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');
Post by Gregory Lancaster
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. Grimes
There are two ways to use the *_meta functions. The way it looks like
you
Post by Gregory Lancaster
Post by J.D. Grimes
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
Post by Gregory Lancaster
Post by J.D. Grimes
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
Post by Gregory Lancaster
Post by J.D. Grimes
usermeta table, each one’s meta_value the ID of a single user that the
user
Post by Gregory Lancaster
Post by J.D. Grimes
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
Post by Gregory Lancaster
Post by J.D. Grimes
then that could change later…) What if you want to show a list of users
who
Post by Gregory Lancaster
Post by J.D. Grimes
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
Post by Gregory Lancaster
Post by J.D. Grimes
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)
Post by Gregory Lancaster
Post by J.D. Grimes
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 <
Post by Gregory Lancaster
What would be the benefit of storing the friends in separate rows? I
did
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
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. Grimes
Yes, 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 Lancaster
Post by J.D. Grimes
could leave the get_user_meta() call the way it is, and change it so
that
Post by Gregory Lancaster
Post by J.D. Grimes
each friend is stored in a separate meta row (but with the same meta
key -
Post by Gregory Lancaster
Post by J.D. Grimes
have 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 Lancaster
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.
Post by Gregory Lancaster
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;
Post by J.D. Grimes
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 <
Post by Gregory Lancaster
That works easier :) Once someone is friended I have it set so an
unfriend
Post by Gregory Lancaster
button replaces the add friend button via ajax. Is it necessary to
write
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
another function for deleting a person? Not sure exactly how to
make
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
new button function since the data being sent is not attached to
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
button
Post by Gregory Lancaster
in any way. maybe I could add a value to the button that says
remove
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
or
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
add, which would be passed to the function and determine what
action
Post by Gregory Lancaster
Post by J.D. Grimes
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
take?
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 BenderisGreat
I am not sure exactly how this would work because I start with an
empty
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
meta_value field. I dont think I can use array_push if there is
not
Post by Gregory Lancaster
Post by J.D. Grimes
at
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
least 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 Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
Sent from the Wordpress Hackers mailing list archive at
Nabble.com.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
_______________________________________________
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
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Gregory Lancaster
2013-10-31 21:08:28 UTC
Permalink
No I must have screwed something up man, it only shows the last value. I
searched for awhile before responding here, and I see some people say
in_array adds a line between items and sometimes other folks needed to
strip those. But I have no idea if thats whats going on here. It all
looks like it should function. :-/ Maybe I need to get off the computer
for awhile.

I var_dump using the function you shared, and this is what the friend array
looks like from there:

array(2) {\n [0]=>\n array(2) {\n [0]=>\n array(2) {\n [0]=>\n
array(2) {\n [0]=>\n array(2) {\n [0]=>\n
array(1) {\n [0]=>\n string(1) "1"\n }\n
[1]=>\n string(1) "9"\n }\n [1]=>\n
string(2) "13"\n }\n [1]=>\n string(2) "16"\n }\n
[1]=>\n string(2) "13"\n }\n [1]=>\n string(1) "3"\n}\n

I dont know if that assists in debugging.
Post by J.D. Grimes
OK, you need to just delete that meta row and start over. It is messed up
from before. Do that and see if it works.
Post by Gregory Lancaster
$active_user_friends = get_user_meta($active_user, 'friends', true);
But the issue is that it only shows the "friended" code for the last
user I
Post by Gregory Lancaster
added. If I click add on one profile, and then to a second, the first no
longer shows friend added. Even if I have clearly added several to the
a:2:{i:0;a:2:{i:0;a:2:{i:0;a:2:{i:0;a:2:{i:0;a:2:{i:0;s:0:"";i:1;s:2:"16";}i:1;s:2:"12";}i:1;s:2:"13";}i:1;s:2:"12";}i:1;s:1:"3";}i:1;s:2:"13";}
Post by Gregory Lancaster
Post by J.D. Grimes
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
Post by Gregory Lancaster
Post by J.D. Grimes
array(
0 => array( 3, 45, 6, 33 )
)
When you call in_array() it is checking if the user ID (say, 3 ) is in
the
Post by Gregory Lancaster
Post by J.D. Grimes
outer array. But it isn’t, the only thing in it is another array. When
you
Post by Gregory Lancaster
Post by J.D. Grimes
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 <
Post by Gregory Lancaster
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
Post by Gregory Lancaster
thing that seems to be left out - but when I tried using $unserialize
like
Post by Gregory Lancaster
$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');
Post by Gregory Lancaster
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. Grimes
There are two ways to use the *_meta functions. The way it looks like
you
Post by Gregory Lancaster
Post by J.D. Grimes
are doing it is that each user has a single entry in the usermeta
table,
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
with the key ‘friends’. The value for this meta key will be stored in
the
Post by Gregory Lancaster
Post by J.D. Grimes
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
Post by Gregory Lancaster
Post by J.D. Grimes
usermeta table, each one’s meta_value the ID of a single user that the
user
Post by Gregory Lancaster
Post by J.D. Grimes
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
Post by Gregory Lancaster
Post by J.D. Grimes
then that could change later…) What if you want to show a list of
users
Post by Gregory Lancaster
Post by J.D. Grimes
who
Post by Gregory Lancaster
Post by J.D. Grimes
have friended a user? You will be able to do that easily with this.
With
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the first option, it is possible, but more complex and probably harder
on
Post by Gregory Lancaster
Post by J.D. Grimes
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)
Post by Gregory Lancaster
Post by J.D. Grimes
or all rows with a key (as in the later case). Whichever way you go,
you
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
just need to be consistent through all of the code, obviously.
-J.D.
On Oct 31, 2013, at 3:30 PM, Gregory Lancaster <
Post by Gregory Lancaster
What would be the benefit of storing the friends in separate rows? I
did
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
not know that was an option, but don't see any clear advantages
either.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
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. Grimes
Yes, when you call get_user_meta() you need to set the third
parameter
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
$active_user_friends = get_user_meta( $active_user, ‘friends’, true
);
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Otherwise it will return a nested array. Also, just FYI,
alternatively
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
you
Post by Gregory Lancaster
Post by J.D. Grimes
could leave the get_user_meta() call the way it is, and change it so
that
Post by Gregory Lancaster
Post by J.D. Grimes
each friend is stored in a separate meta row (but with the same meta
key -
Post by Gregory Lancaster
Post by J.D. Grimes
have 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 Lancaster
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
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
load.
Post by Gregory Lancaster
But regardless if someone has been friended or not, it shows the
add
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
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>";
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
} else {
$friend_status = "<button id='add_friend'>Add Friend</button>";
}
return $friend_status;
}
Then on page: echo $friend_status;
Post by J.D. Grimes
Yes, I would write a separate function for removal, and hook it
to a
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
different AJAX action for removal.
On Oct 31, 2013, at 2:48 PM, Gregory Lancaster <
Post by Gregory Lancaster
That works easier :) Once someone is friended I have it set so
an
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
unfriend
Post by Gregory Lancaster
button replaces the add friend button via ajax. Is it necessary
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
write
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
another function for deleting a person? Not sure exactly how to
make
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
new button function since the data being sent is not attached to
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
button
Post by Gregory Lancaster
in any way. maybe I could add a value to the button that says
remove
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
or
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
add, which would be passed to the function and determine what
action
Post by Gregory Lancaster
Post by J.D. Grimes
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
take?
On Thu, Oct 31, 2013 at 11:32 AM, J.D. Grimes <
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 BenderisGreat
I am not sure exactly how this would work because I start with
an
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
empty
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
meta_value field. I dont think I can use array_push if there
is
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
not
Post by Gregory Lancaster
Post by J.D. Grimes
at
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
least 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 Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
Sent from the Wordpress Hackers mailing list archive at
Nabble.com.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
_______________________________________________
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
_______________________________________________
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
J.D. Grimes
2013-10-31 21:16:13 UTC
Permalink
Could you post your code again? Somewhere you are still getting the value wrapped in an extra array.
Post by Gregory Lancaster
No I must have screwed something up man, it only shows the last value. I
searched for awhile before responding here, and I see some people say
in_array adds a line between items and sometimes other folks needed to
strip those. But I have no idea if thats whats going on here. It all
looks like it should function. :-/ Maybe I need to get off the computer
for awhile.
I var_dump using the function you shared, and this is what the friend array
array(2) {\n [0]=>\n array(2) {\n [0]=>\n array(2) {\n [0]=>\n
array(2) {\n [0]=>\n array(2) {\n [0]=>\n
array(1) {\n [0]=>\n string(1) "1"\n }\n
[1]=>\n string(1) "9"\n }\n [1]=>\n
string(2) "13"\n }\n [1]=>\n string(2) "16"\n }\n
[1]=>\n string(2) "13"\n }\n [1]=>\n string(1) "3"\n}\n
I dont know if that assists in debugging.
Post by J.D. Grimes
OK, you need to just delete that meta row and start over. It is messed up
from before. Do that and see if it works.
Post by Gregory Lancaster
$active_user_friends = get_user_meta($active_user, 'friends', true);
But the issue is that it only shows the "friended" code for the last
user I
Post by Gregory Lancaster
added. If I click add on one profile, and then to a second, the first no
longer shows friend added. Even if I have clearly added several to the
a:2:{i:0;a:2:{i:0;a:2:{i:0;a:2:{i:0;a:2:{i:0;a:2:{i:0;s:0:"";i:1;s:2:"16";}i:1;s:2:"12";}i:1;s:2:"13";}i:1;s:2:"12";}i:1;s:1:"3";}i:1;s:2:"13";}
Post by Gregory Lancaster
Post by J.D. Grimes
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
Post by Gregory Lancaster
Post by J.D. Grimes
array(
0 => array( 3, 45, 6, 33 )
)
When you call in_array() it is checking if the user ID (say, 3 ) is in
the
Post by Gregory Lancaster
Post by J.D. Grimes
outer array. But it isn’t, the only thing in it is another array. When
you
Post by Gregory Lancaster
Post by J.D. Grimes
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 <
Post by Gregory Lancaster
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
Post by Gregory Lancaster
thing that seems to be left out - but when I tried using $unserialize
like
Post by Gregory Lancaster
$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');
Post by Gregory Lancaster
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. Grimes
There are two ways to use the *_meta functions. The way it looks like
you
Post by Gregory Lancaster
Post by J.D. Grimes
are doing it is that each user has a single entry in the usermeta
table,
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
with the key ‘friends’. The value for this meta key will be stored in
the
Post by Gregory Lancaster
Post by J.D. Grimes
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
Post by Gregory Lancaster
Post by J.D. Grimes
usermeta table, each one’s meta_value the ID of a single user that the
user
Post by Gregory Lancaster
Post by J.D. Grimes
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
Post by Gregory Lancaster
Post by J.D. Grimes
then that could change later…) What if you want to show a list of
users
Post by Gregory Lancaster
Post by J.D. Grimes
who
Post by Gregory Lancaster
Post by J.D. Grimes
have friended a user? You will be able to do that easily with this.
With
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the first option, it is possible, but more complex and probably harder
on
Post by Gregory Lancaster
Post by J.D. Grimes
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)
Post by Gregory Lancaster
Post by J.D. Grimes
or all rows with a key (as in the later case). Whichever way you go,
you
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
just need to be consistent through all of the code, obviously.
-J.D.
On Oct 31, 2013, at 3:30 PM, Gregory Lancaster <
Post by Gregory Lancaster
What would be the benefit of storing the friends in separate rows? I
did
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
not know that was an option, but don't see any clear advantages
either.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
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. Grimes
Yes, when you call get_user_meta() you need to set the third
parameter
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
$active_user_friends = get_user_meta( $active_user, ‘friends’, true
);
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Otherwise it will return a nested array. Also, just FYI,
alternatively
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
you
Post by Gregory Lancaster
Post by J.D. Grimes
could leave the get_user_meta() call the way it is, and change it so
that
Post by Gregory Lancaster
Post by J.D. Grimes
each friend is stored in a separate meta row (but with the same meta
key -
Post by Gregory Lancaster
Post by J.D. Grimes
have 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 Lancaster
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
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
load.
Post by Gregory Lancaster
But regardless if someone has been friended or not, it shows the
add
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
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>";
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
} else {
$friend_status = "<button id='add_friend'>Add Friend</button>";
}
return $friend_status;
}
Then on page: echo $friend_status;
Post by J.D. Grimes
Yes, I would write a separate function for removal, and hook it
to a
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
different AJAX action for removal.
On Oct 31, 2013, at 2:48 PM, Gregory Lancaster <
Post by Gregory Lancaster
That works easier :) Once someone is friended I have it set so
an
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
unfriend
Post by Gregory Lancaster
button replaces the add friend button via ajax. Is it necessary
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
write
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
another function for deleting a person? Not sure exactly how to
make
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
new button function since the data being sent is not attached to
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
button
Post by Gregory Lancaster
in any way. maybe I could add a value to the button that says
remove
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
or
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
add, which would be passed to the function and determine what
action
Post by Gregory Lancaster
Post by J.D. Grimes
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
take?
On Thu, Oct 31, 2013 at 11:32 AM, J.D. Grimes <
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 BenderisGreat
I am not sure exactly how this would work because I start with
an
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
empty
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
meta_value field. I dont think I can use array_push if there
is
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
not
Post by Gregory Lancaster
Post by J.D. Grimes
at
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
least 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 Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
Sent from the Wordpress Hackers mailing list archive at
Nabble.com.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
_______________________________________________
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
_______________________________________________
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
Gregory Lancaster
2013-10-31 21:18:42 UTC
Permalink
Sure;

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 follow_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', true);

if (in_array($author_id, $active_user_friends)) {
$follow_status = "<button id='remove_friend'>Remove Friend</button>";
} else {
$follow_status = "<button id='add_friend'>Add Friend</button>";
}

return $follow_status;
}

and calling in the profile page with:

echo follow_status($author_id);
Post by J.D. Grimes
Could you post your code again? Somewhere you are still getting the value
wrapped in an extra array.
Post by Gregory Lancaster
No I must have screwed something up man, it only shows the last value. I
searched for awhile before responding here, and I see some people say
in_array adds a line between items and sometimes other folks needed to
strip those. But I have no idea if thats whats going on here. It all
looks like it should function. :-/ Maybe I need to get off the computer
for awhile.
I var_dump using the function you shared, and this is what the friend
array
Post by Gregory Lancaster
array(2) {\n [0]=>\n array(2) {\n [0]=>\n array(2) {\n
[0]=>\n
Post by Gregory Lancaster
array(2) {\n [0]=>\n array(2) {\n [0]=>\n
array(1) {\n [0]=>\n string(1) "1"\n }\n
[1]=>\n string(1) "9"\n }\n [1]=>\n
string(2) "13"\n }\n [1]=>\n string(2) "16"\n }\n
[1]=>\n string(2) "13"\n }\n [1]=>\n string(1) "3"\n}\n
I dont know if that assists in debugging.
Post by J.D. Grimes
OK, you need to just delete that meta row and start over. It is messed
up
Post by Gregory Lancaster
Post by J.D. Grimes
from before. Do that and see if it works.
On Oct 31, 2013, at 4:49 PM, Gregory Lancaster <
Post by Gregory Lancaster
$active_user_friends = get_user_meta($active_user, 'friends', true);
But the issue is that it only shows the "friended" code for the last
user I
Post by Gregory Lancaster
added. If I click add on one profile, and then to a second, the first
no
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
longer shows friend added. Even if I have clearly added several to the
a:2:{i:0;a:2:{i:0;a:2:{i:0;a:2:{i:0;a:2:{i:0;a:2:{i:0;s:0:"";i:1;s:2:"16";}i:1;s:2:"12";}i:1;s:2:"13";}i:1;s:2:"12";}i:1;s:1:"3";}i:1;s:2:"13";}
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
No, you don’t need to use unserialize, that is all done automatically
by
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the WordPress functions.
In the code you posted you still aren’t calling get_user_meta() with
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
third parameter set to true. You need to do that. Right now the array
it is
Post by Gregory Lancaster
Post by J.D. Grimes
array(
0 => array( 3, 45, 6, 33 )
)
When you call in_array() it is checking if the user ID (say, 3 ) is in
the
Post by Gregory Lancaster
Post by J.D. Grimes
outer array. But it isn’t, the only thing in it is another array. When
you
Post by Gregory Lancaster
Post by J.D. Grimes
call get_user_meta() with the third parameter set to true, the
returned
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
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 <
Post by Gregory Lancaster
Ah I see. Thank you for explaining JD. It sounds like maybe the
easier
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
option is to just create a separate table to store the friends list
in.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
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
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
using in_array? Because right now this code isnt working. Thats the
only
Post by Gregory Lancaster
thing that seems to be left out - but when I tried using $unserialize
like
Post by Gregory Lancaster
$unserialize = unserialize($active_user_friends);
if (in_array($author_id, $unserialize)) {
*Warning*: unserialize() expects parameter 1 to be string, array
given
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
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');
Post by Gregory Lancaster
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
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
user_meta table.
Post by J.D. Grimes
There are two ways to use the *_meta functions. The way it looks
like
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
you
Post by Gregory Lancaster
Post by J.D. Grimes
are doing it is that each user has a single entry in the usermeta
table,
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
with the key ‘friends’. The value for this meta key will be stored
in
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
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
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
usermeta table, each one’s meta_value the ID of a single user that
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
user
Post by Gregory Lancaster
Post by J.D. Grimes
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
Post by Gregory Lancaster
Post by J.D. Grimes
then that could change later…) What if you want to show a list of
users
Post by Gregory Lancaster
Post by J.D. Grimes
who
Post by Gregory Lancaster
Post by J.D. Grimes
have friended a user? You will be able to do that easily with this.
With
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the first option, it is possible, but more complex and probably
harder
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
on
Post by Gregory Lancaster
Post by J.D. Grimes
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)
Post by Gregory Lancaster
Post by J.D. Grimes
or all rows with a key (as in the later case). Whichever way you go,
you
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
just need to be consistent through all of the code, obviously.
-J.D.
On Oct 31, 2013, at 3:30 PM, Gregory Lancaster <
Post by Gregory Lancaster
What would be the benefit of storing the friends in separate rows?
I
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
did
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
not know that was an option, but don't see any clear advantages
either.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Also, I did just search the codex but dont see anything about
unique
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
rows.
Post by Gregory Lancaster
(I checked get_user_meta, delete_user_meta, update_user_meta. )
Post by J.D. Grimes
Yes, when you call get_user_meta() you need to set the third
parameter
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
$active_user_friends = get_user_meta( $active_user, ‘friends’,
true
Post by Gregory Lancaster
Post by J.D. Grimes
);
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Otherwise it will return a nested array. Also, just FYI,
alternatively
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
you
Post by Gregory Lancaster
Post by J.D. Grimes
could leave the get_user_meta() call the way it is, and change it
so
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
that
Post by Gregory Lancaster
Post by J.D. Grimes
each friend is stored in a separate meta row (but with the same
meta
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
key -
Post by Gregory Lancaster
Post by J.D. Grimes
have 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 Lancaster
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
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
load.
Post by Gregory Lancaster
But regardless if someone has been friended or not, it shows the
add
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
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>";
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
} 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 <
Post by J.D. Grimes
Yes, I would write a separate function for removal, and hook it
to a
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
different AJAX action for removal.
On Oct 31, 2013, at 2:48 PM, Gregory Lancaster <
Post by Gregory Lancaster
That works easier :) Once someone is friended I have it set so
an
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
unfriend
Post by Gregory Lancaster
button replaces the add friend button via ajax. Is it
necessary
Post by Gregory Lancaster
Post by J.D. Grimes
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
write
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
another function for deleting a person? Not sure exactly how
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
make
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
new button function since the data being sent is not attached
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
button
Post by Gregory Lancaster
in any way. maybe I could add a value to the button that says
remove
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
or
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
add, which would be passed to the function and determine what
action
Post by Gregory Lancaster
Post by J.D. Grimes
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
take?
On Thu, Oct 31, 2013 at 11:32 AM, J.D. Grimes <
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 BenderisGreat
I am not sure exactly how this would work because I start
with
Post by Gregory Lancaster
Post by J.D. Grimes
an
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
empty
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
meta_value field. I dont think I can use array_push if there
is
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
not
Post by Gregory Lancaster
Post by J.D. Grimes
at
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
least 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 Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
Sent from the Wordpress Hackers mailing list archive at
Nabble.com.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
_______________________________________________
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
_______________________________________________
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
Gregory Lancaster
2013-10-31 21:20:47 UTC
Permalink
Sorry for the naming by the way. I decided after I did the first part,
that rather than a friending system it would be used to follow another
users activity.


On Thu, Oct 31, 2013 at 2:18 PM, Gregory Lancaster <
Post by Gregory Lancaster
Sure;
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 follow_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', true);
if (in_array($author_id, $active_user_friends)) {
$follow_status = "<button id='remove_friend'>Remove Friend</button>";
} else {
$follow_status = "<button id='add_friend'>Add Friend</button>";
}
return $follow_status;
}
echo follow_status($author_id);
Post by J.D. Grimes
Could you post your code again? Somewhere you are still getting the value
wrapped in an extra array.
Post by Gregory Lancaster
No I must have screwed something up man, it only shows the last value.
I
Post by Gregory Lancaster
searched for awhile before responding here, and I see some people say
in_array adds a line between items and sometimes other folks needed to
strip those. But I have no idea if thats whats going on here. It all
looks like it should function. :-/ Maybe I need to get off the
computer
Post by Gregory Lancaster
for awhile.
I var_dump using the function you shared, and this is what the friend
array
Post by Gregory Lancaster
array(2) {\n [0]=>\n array(2) {\n [0]=>\n array(2) {\n
[0]=>\n
Post by Gregory Lancaster
array(2) {\n [0]=>\n array(2) {\n [0]=>\n
array(1) {\n [0]=>\n string(1) "1"\n
}\n
Post by Gregory Lancaster
[1]=>\n string(1) "9"\n }\n [1]=>\n
string(2) "13"\n }\n [1]=>\n string(2) "16"\n }\n
[1]=>\n string(2) "13"\n }\n [1]=>\n string(1) "3"\n}\n
I dont know if that assists in debugging.
Post by J.D. Grimes
OK, you need to just delete that meta row and start over. It is messed
up
Post by Gregory Lancaster
Post by J.D. Grimes
from before. Do that and see if it works.
On Oct 31, 2013, at 4:49 PM, Gregory Lancaster <
Post by Gregory Lancaster
$active_user_friends = get_user_meta($active_user, 'friends', true);
But the issue is that it only shows the "friended" code for the last
user I
Post by Gregory Lancaster
added. If I click add on one profile, and then to a second, the
first no
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
longer shows friend added. Even if I have clearly added several to the
a:2:{i:0;a:2:{i:0;a:2:{i:0;a:2:{i:0;a:2:{i:0;a:2:{i:0;s:0:"";i:1;s:2:"16";}i:1;s:2:"12";}i:1;s:2:"13";}i:1;s:2:"12";}i:1;s:1:"3";}i:1;s:2:"13";}
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
No, you don’t need to use unserialize, that is all done
automatically by
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the WordPress functions.
In the code you posted you still aren’t calling get_user_meta() with
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
third parameter set to true. You need to do that. Right now the array
it is
Post by Gregory Lancaster
Post by J.D. Grimes
array(
0 => array( 3, 45, 6, 33 )
)
When you call in_array() it is checking if the user ID (say, 3 ) is
in
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
outer array. But it isn’t, the only thing in it is another array.
When
Post by Gregory Lancaster
Post by J.D. Grimes
you
Post by Gregory Lancaster
Post by J.D. Grimes
call get_user_meta() with the third parameter set to true, the
returned
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
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 <
Post by Gregory Lancaster
Ah I see. Thank you for explaining JD. It sounds like maybe the
easier
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
option is to just create a separate table to store the friends list
in.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
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
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
using in_array? Because right now this code isnt working. Thats
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
only
Post by Gregory Lancaster
thing that seems to be left out - but when I tried using
$unserialize
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
like
Post by Gregory Lancaster
$unserialize = unserialize($active_user_friends);
if (in_array($author_id, $unserialize)) {
*Warning*: unserialize() expects parameter 1 to be string, array
given
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
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');
Post by Gregory Lancaster
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>";
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
} 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
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
user_meta table.
Post by J.D. Grimes
There are two ways to use the *_meta functions. The way it looks
like
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
you
Post by Gregory Lancaster
Post by J.D. Grimes
are doing it is that each user has a single entry in the usermeta
table,
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
with the key ‘friends’. The value for this meta key will be stored
in
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
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
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
usermeta table, each one’s meta_value the ID of a single user that
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
user
Post by Gregory Lancaster
Post by J.D. Grimes
has friended.
The benefit of the second option is that the values may be easier
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
search. Much easier. But that may not be important to you right
now.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
(But
Post by Gregory Lancaster
Post by J.D. Grimes
then that could change later…) What if you want to show a list of
users
Post by Gregory Lancaster
Post by J.D. Grimes
who
Post by Gregory Lancaster
Post by J.D. Grimes
have friended a user? You will be able to do that easily with this.
With
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the first option, it is possible, but more complex and probably
harder
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
on
Post by Gregory Lancaster
Post by J.D. Grimes
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)
Post by Gregory Lancaster
Post by J.D. Grimes
or all rows with a key (as in the later case). Whichever way you
go,
Post by Gregory Lancaster
Post by J.D. Grimes
you
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
just need to be consistent through all of the code, obviously.
-J.D.
On Oct 31, 2013, at 3:30 PM, Gregory Lancaster <
Post by Gregory Lancaster
What would be the benefit of storing the friends in separate
rows? I
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
did
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
not know that was an option, but don't see any clear advantages
either.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Also, I did just search the codex but dont see anything about
unique
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
rows.
Post by Gregory Lancaster
(I checked get_user_meta, delete_user_meta, update_user_meta. )
On Thu, Oct 31, 2013 at 12:24 PM, J.D. Grimes <
Post by J.D. Grimes
Yes, when you call get_user_meta() you need to set the third
parameter
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
$active_user_friends = get_user_meta( $active_user, ‘friends’,
true
Post by Gregory Lancaster
Post by J.D. Grimes
);
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Otherwise it will return a nested array. Also, just FYI,
alternatively
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
you
Post by Gregory Lancaster
Post by J.D. Grimes
could leave the get_user_meta() call the way it is, and change
it so
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
that
Post by Gregory Lancaster
Post by J.D. Grimes
each friend is stored in a separate meta row (but with the same
meta
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
key -
Post by Gregory Lancaster
Post by J.D. Grimes
have 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 Lancaster
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
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
load.
Post by Gregory Lancaster
But regardless if someone has been friended or not, it shows the
add
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
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>";
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
} 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 <
Post by J.D. Grimes
Yes, I would write a separate function for removal, and hook it
to a
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
different AJAX action for removal.
On Oct 31, 2013, at 2:48 PM, Gregory Lancaster <
Post by Gregory Lancaster
That works easier :) Once someone is friended I have it set
so
Post by Gregory Lancaster
Post by J.D. Grimes
an
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
unfriend
Post by Gregory Lancaster
button replaces the add friend button via ajax. Is it
necessary
Post by Gregory Lancaster
Post by J.D. Grimes
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
write
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
another function for deleting a person? Not sure exactly how
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
make
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
new button function since the data being sent is not attached
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
button
Post by Gregory Lancaster
in any way. maybe I could add a value to the button that says
remove
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
or
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
add, which would be passed to the function and determine what
action
Post by Gregory Lancaster
Post by J.D. Grimes
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
take?
On Thu, Oct 31, 2013 at 11:32 AM, J.D. Grimes <
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 BenderisGreat
I am not sure exactly how this would work because I start
with
Post by Gregory Lancaster
Post by J.D. Grimes
an
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
empty
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
meta_value field. I dont think I can use array_push if
there
Post by Gregory Lancaster
Post by J.D. Grimes
is
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
not
Post by Gregory Lancaster
Post by J.D. Grimes
at
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
least 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 Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
Sent from the Wordpress Hackers mailing list archive at
Nabble.com.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
_______________________________________________
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
_______________________________________________
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
J.D. Grimes
2013-10-31 21:22:24 UTC
Permalink
Ah.. We forgot to add true to the gat_user_meta() in my_add_friend_action(). Add that and you should be good to go.
Post by Gregory Lancaster
Sure;
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 follow_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', true);
if (in_array($author_id, $active_user_friends)) {
$follow_status = "<button id='remove_friend'>Remove Friend</button>";
} else {
$follow_status = "<button id='add_friend'>Add Friend</button>";
}
return $follow_status;
}
echo follow_status($author_id);
Post by J.D. Grimes
Could you post your code again? Somewhere you are still getting the value
wrapped in an extra array.
Post by Gregory Lancaster
No I must have screwed something up man, it only shows the last value. I
searched for awhile before responding here, and I see some people say
in_array adds a line between items and sometimes other folks needed to
strip those. But I have no idea if thats whats going on here. It all
looks like it should function. :-/ Maybe I need to get off the computer
for awhile.
I var_dump using the function you shared, and this is what the friend
array
Post by Gregory Lancaster
array(2) {\n [0]=>\n array(2) {\n [0]=>\n array(2) {\n
[0]=>\n
Post by Gregory Lancaster
array(2) {\n [0]=>\n array(2) {\n [0]=>\n
array(1) {\n [0]=>\n string(1) "1"\n }\n
[1]=>\n string(1) "9"\n }\n [1]=>\n
string(2) "13"\n }\n [1]=>\n string(2) "16"\n }\n
[1]=>\n string(2) "13"\n }\n [1]=>\n string(1) "3"\n}\n
I dont know if that assists in debugging.
Post by J.D. Grimes
OK, you need to just delete that meta row and start over. It is messed
up
Post by Gregory Lancaster
Post by J.D. Grimes
from before. Do that and see if it works.
On Oct 31, 2013, at 4:49 PM, Gregory Lancaster <
Post by Gregory Lancaster
$active_user_friends = get_user_meta($active_user, 'friends', true);
But the issue is that it only shows the "friended" code for the last
user I
Post by Gregory Lancaster
added. If I click add on one profile, and then to a second, the first
no
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
longer shows friend added. Even if I have clearly added several to the
a:2:{i:0;a:2:{i:0;a:2:{i:0;a:2:{i:0;a:2:{i:0;a:2:{i:0;s:0:"";i:1;s:2:"16";}i:1;s:2:"12";}i:1;s:2:"13";}i:1;s:2:"12";}i:1;s:1:"3";}i:1;s:2:"13";}
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
No, you don’t need to use unserialize, that is all done automatically
by
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the WordPress functions.
In the code you posted you still aren’t calling get_user_meta() with
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
third parameter set to true. You need to do that. Right now the array
it is
Post by Gregory Lancaster
Post by J.D. Grimes
array(
0 => array( 3, 45, 6, 33 )
)
When you call in_array() it is checking if the user ID (say, 3 ) is in
the
Post by Gregory Lancaster
Post by J.D. Grimes
outer array. But it isn’t, the only thing in it is another array. When
you
Post by Gregory Lancaster
Post by J.D. Grimes
call get_user_meta() with the third parameter set to true, the
returned
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
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 <
Post by Gregory Lancaster
Ah I see. Thank you for explaining JD. It sounds like maybe the
easier
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
option is to just create a separate table to store the friends list
in.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
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
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
using in_array? Because right now this code isnt working. Thats the
only
Post by Gregory Lancaster
thing that seems to be left out - but when I tried using $unserialize
like
Post by Gregory Lancaster
$unserialize = unserialize($active_user_friends);
if (in_array($author_id, $unserialize)) {
*Warning*: unserialize() expects parameter 1 to be string, array
given
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
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');
Post by Gregory Lancaster
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
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
user_meta table.
Post by J.D. Grimes
There are two ways to use the *_meta functions. The way it looks
like
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
you
Post by Gregory Lancaster
Post by J.D. Grimes
are doing it is that each user has a single entry in the usermeta
table,
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
with the key ‘friends’. The value for this meta key will be stored
in
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
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
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
usermeta table, each one’s meta_value the ID of a single user that
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
user
Post by Gregory Lancaster
Post by J.D. Grimes
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
Post by Gregory Lancaster
Post by J.D. Grimes
then that could change later…) What if you want to show a list of
users
Post by Gregory Lancaster
Post by J.D. Grimes
who
Post by Gregory Lancaster
Post by J.D. Grimes
have friended a user? You will be able to do that easily with this.
With
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the first option, it is possible, but more complex and probably
harder
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
on
Post by Gregory Lancaster
Post by J.D. Grimes
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)
Post by Gregory Lancaster
Post by J.D. Grimes
or all rows with a key (as in the later case). Whichever way you go,
you
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
just need to be consistent through all of the code, obviously.
-J.D.
On Oct 31, 2013, at 3:30 PM, Gregory Lancaster <
Post by Gregory Lancaster
What would be the benefit of storing the friends in separate rows?
I
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
did
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
not know that was an option, but don't see any clear advantages
either.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Also, I did just search the codex but dont see anything about
unique
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
rows.
Post by Gregory Lancaster
(I checked get_user_meta, delete_user_meta, update_user_meta. )
Post by J.D. Grimes
Yes, when you call get_user_meta() you need to set the third
parameter
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
$active_user_friends = get_user_meta( $active_user, ‘friends’,
true
Post by Gregory Lancaster
Post by J.D. Grimes
);
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Otherwise it will return a nested array. Also, just FYI,
alternatively
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
you
Post by Gregory Lancaster
Post by J.D. Grimes
could leave the get_user_meta() call the way it is, and change it
so
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
that
Post by Gregory Lancaster
Post by J.D. Grimes
each friend is stored in a separate meta row (but with the same
meta
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
key -
Post by Gregory Lancaster
Post by J.D. Grimes
have 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 Lancaster
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
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
load.
Post by Gregory Lancaster
But regardless if someone has been friended or not, it shows the
add
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
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>";
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
} 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 <
Post by J.D. Grimes
Yes, I would write a separate function for removal, and hook it
to a
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
different AJAX action for removal.
On Oct 31, 2013, at 2:48 PM, Gregory Lancaster <
Post by Gregory Lancaster
That works easier :) Once someone is friended I have it set so
an
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
unfriend
Post by Gregory Lancaster
button replaces the add friend button via ajax. Is it
necessary
Post by Gregory Lancaster
Post by J.D. Grimes
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
write
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
another function for deleting a person? Not sure exactly how
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
make
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
new button function since the data being sent is not attached
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
button
Post by Gregory Lancaster
in any way. maybe I could add a value to the button that says
remove
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
or
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
add, which would be passed to the function and determine what
action
Post by Gregory Lancaster
Post by J.D. Grimes
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
take?
On Thu, Oct 31, 2013 at 11:32 AM, J.D. Grimes <
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 BenderisGreat
I am not sure exactly how this would work because I start
with
Post by Gregory Lancaster
Post by J.D. Grimes
an
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
empty
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
meta_value field. I dont think I can use array_push if there
is
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
not
Post by Gregory Lancaster
Post by J.D. Grimes
at
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
least 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 Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
Sent from the Wordpress Hackers mailing list archive at
Nabble.com.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
_______________________________________________
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
_______________________________________________
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
Gregory Lancaster
2013-10-31 21:28:16 UTC
Permalink
did I really spend a couple hours reading for a solution over the word
true. Thank you JD. Real quick, you said:

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.

Why are arrays stored so many tiers deep like that? Is this a wordpress
only thing, or is this normal? It looks like everytime a persons ID is
added, another instance of array(array(array( is added. What is the
reasoning behind this? Or do I understand it incorrectly?
Post by J.D. Grimes
Ah.. We forgot to add true to the gat_user_meta() in
my_add_friend_action(). Add that and you should be good to go.
Post by Gregory Lancaster
Sure;
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');
Post by Gregory Lancaster
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 follow_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', true);
if (in_array($author_id, $active_user_friends)) {
$follow_status = "<button id='remove_friend'>Remove Friend</button>";
} else {
$follow_status = "<button id='add_friend'>Add Friend</button>";
}
return $follow_status;
}
echo follow_status($author_id);
Post by J.D. Grimes
Could you post your code again? Somewhere you are still getting the
value
Post by Gregory Lancaster
Post by J.D. Grimes
wrapped in an extra array.
On Oct 31, 2013, at 5:08 PM, Gregory Lancaster <
Post by Gregory Lancaster
No I must have screwed something up man, it only shows the last value.
I
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
searched for awhile before responding here, and I see some people say
in_array adds a line between items and sometimes other folks needed to
strip those. But I have no idea if thats whats going on here. It all
looks like it should function. :-/ Maybe I need to get off the
computer
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
for awhile.
I var_dump using the function you shared, and this is what the friend
array
Post by Gregory Lancaster
array(2) {\n [0]=>\n array(2) {\n [0]=>\n array(2) {\n
[0]=>\n
Post by Gregory Lancaster
array(2) {\n [0]=>\n array(2) {\n [0]=>\n
array(1) {\n [0]=>\n string(1) "1"\n
}\n
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
[1]=>\n string(1) "9"\n }\n [1]=>\n
string(2) "13"\n }\n [1]=>\n string(2) "16"\n }\n
[1]=>\n string(2) "13"\n }\n [1]=>\n string(1) "3"\n}\n
I dont know if that assists in debugging.
Post by J.D. Grimes
OK, you need to just delete that meta row and start over. It is messed
up
Post by Gregory Lancaster
Post by J.D. Grimes
from before. Do that and see if it works.
On Oct 31, 2013, at 4:49 PM, Gregory Lancaster <
Post by Gregory Lancaster
$active_user_friends = get_user_meta($active_user, 'friends', true);
But the issue is that it only shows the "friended" code for the last
user I
Post by Gregory Lancaster
added. If I click add on one profile, and then to a second, the
first
Post by Gregory Lancaster
Post by J.D. Grimes
no
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
longer shows friend added. Even if I have clearly added several to
the
a:2:{i:0;a:2:{i:0;a:2:{i:0;a:2:{i:0;a:2:{i:0;a:2:{i:0;s:0:"";i:1;s:2:"16";}i:1;s:2:"12";}i:1;s:2:"13";}i:1;s:2:"12";}i:1;s:1:"3";}i:1;s:2:"13";}
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
No, you don’t need to use unserialize, that is all done
automatically
Post by Gregory Lancaster
Post by J.D. Grimes
by
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the WordPress functions.
In the code you posted you still aren’t calling get_user_meta() with
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
third parameter set to true. You need to do that. Right now the
array
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
it is
Post by Gregory Lancaster
Post by J.D. Grimes
array(
0 => array( 3, 45, 6, 33 )
)
When you call in_array() it is checking if the user ID (say, 3 ) is
in
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
outer array. But it isn’t, the only thing in it is another array.
When
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
you
Post by Gregory Lancaster
Post by J.D. Grimes
call get_user_meta() with the third parameter set to true, the
returned
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
value won’t be wrapped in the outer array like that, so it will
work.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
On Oct 31, 2013, at 4:30 PM, Gregory Lancaster <
Post by Gregory Lancaster
Ah I see. Thank you for explaining JD. It sounds like maybe the
easier
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
option is to just create a separate table to store the friends list
in.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
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
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
using in_array? Because right now this code isnt working. Thats
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
only
Post by Gregory Lancaster
thing that seems to be left out - but when I tried using
$unserialize
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
like
Post by Gregory Lancaster
$unserialize = unserialize($active_user_friends);
if (in_array($author_id, $unserialize)) {
*Warning*: unserialize() expects parameter 1 to be string, array
given
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
and
*Warning*: in_array() expects parameter 2 to be array, boolean
given
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
--------
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');
Post by Gregory Lancaster
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>";
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
} 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
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
user_meta table.
Post by J.D. Grimes
There are two ways to use the *_meta functions. The way it looks
like
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
you
Post by Gregory Lancaster
Post by J.D. Grimes
are doing it is that each user has a single entry in the usermeta
table,
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
with the key ‘friends’. The value for this meta key will be stored
in
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
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
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
usermeta table, each one’s meta_value the ID of a single user that
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
user
Post by Gregory Lancaster
Post by J.D. Grimes
has friended.
The benefit of the second option is that the values may be easier
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
search. Much easier. But that may not be important to you right
now.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
(But
Post by Gregory Lancaster
Post by J.D. Grimes
then that could change later…) What if you want to show a list of
users
Post by Gregory Lancaster
Post by J.D. Grimes
who
Post by Gregory Lancaster
Post by J.D. Grimes
have friended a user? You will be able to do that easily with
this.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
With
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the first option, it is possible, but more complex and probably
harder
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
on
Post by Gregory Lancaster
Post by J.D. Grimes
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
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
case)
Post by Gregory Lancaster
Post by J.D. Grimes
or all rows with a key (as in the later case). Whichever way you
go,
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
you
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
just need to be consistent through all of the code, obviously.
-J.D.
On Oct 31, 2013, at 3:30 PM, Gregory Lancaster <
Post by Gregory Lancaster
What would be the benefit of storing the friends in separate
rows?
Post by Gregory Lancaster
Post by J.D. Grimes
I
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
did
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
not know that was an option, but don't see any clear advantages
either.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Also, I did just search the codex but dont see anything about
unique
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
rows.
Post by Gregory Lancaster
(I checked get_user_meta, delete_user_meta, update_user_meta. )
On Thu, Oct 31, 2013 at 12:24 PM, J.D. Grimes <
Post by J.D. Grimes
Yes, when you call get_user_meta() you need to set the third
parameter
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
$active_user_friends = get_user_meta( $active_user, ‘friends’,
true
Post by Gregory Lancaster
Post by J.D. Grimes
);
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Otherwise it will return a nested array. Also, just FYI,
alternatively
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
you
Post by Gregory Lancaster
Post by J.D. Grimes
could leave the get_user_meta() call the way it is, and change
it
Post by Gregory Lancaster
Post by J.D. Grimes
so
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
that
Post by Gregory Lancaster
Post by J.D. Grimes
each friend is stored in a separate meta row (but with the same
meta
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
key -
Post by Gregory Lancaster
Post by J.D. Grimes
have 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 Lancaster
I thought so. One more question if you dont mind helping;
This is the function I wrote to determine what button to show
on
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
page
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
load.
Post by Gregory Lancaster
But regardless if someone has been friended or not, it shows
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
add
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
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>";
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
} 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 <
Post by J.D. Grimes
Yes, I would write a separate function for removal, and hook
it
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
to a
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
different AJAX action for removal.
On Oct 31, 2013, at 2:48 PM, Gregory Lancaster <
Post by Gregory Lancaster
That works easier :) Once someone is friended I have it set
so
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
an
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
unfriend
Post by Gregory Lancaster
button replaces the add friend button via ajax. Is it
necessary
Post by Gregory Lancaster
Post by J.D. Grimes
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
write
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
another function for deleting a person? Not sure exactly how
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
make
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
new button function since the data being sent is not attached
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
button
Post by Gregory Lancaster
in any way. maybe I could add a value to the button that
says
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
remove
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
or
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
add, which would be passed to the function and determine what
action
Post by Gregory Lancaster
Post by J.D. Grimes
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
take?
On Thu, Oct 31, 2013 at 11:32 AM, J.D. Grimes <
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 BenderisGreat
I am not sure exactly how this would work because I start
with
Post by Gregory Lancaster
Post by J.D. Grimes
an
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
empty
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
meta_value field. I dont think I can use array_push if
there
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
is
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
not
Post by Gregory Lancaster
Post by J.D. Grimes
at
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
least 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 Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
Sent from the Wordpress Hackers mailing list archive at
Nabble.com.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
_______________________________________________
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
_______________________________________________
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
J.D. Grimes
2013-10-31 21:33:38 UTC
Permalink
Yes, that is what was happening with the arrays, because each time you pulled the value out of the database, it was being wrapped in an array (because get_user_meta() was pulling the values for all ‘friends’ keys for the user). You added one user ID to it, and then put it back. That was happening each time you followed a user. Now that it is working correctly, the array will be only one layer deep. I remember coming up against this thing with get_user_meta() myself once when I was first learning WP development. :-)
Post by Gregory Lancaster
did I really spend a couple hours reading for a solution over the word
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.
Why are arrays stored so many tiers deep like that? Is this a wordpress
only thing, or is this normal? It looks like everytime a persons ID is
added, another instance of array(array(array( is added. What is the
reasoning behind this? Or do I understand it incorrectly?
Post by J.D. Grimes
Ah.. We forgot to add true to the gat_user_meta() in
my_add_friend_action(). Add that and you should be good to go.
Post by Gregory Lancaster
Sure;
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');
Post by Gregory Lancaster
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 follow_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', true);
if (in_array($author_id, $active_user_friends)) {
$follow_status = "<button id='remove_friend'>Remove Friend</button>";
} else {
$follow_status = "<button id='add_friend'>Add Friend</button>";
}
return $follow_status;
}
echo follow_status($author_id);
Post by J.D. Grimes
Could you post your code again? Somewhere you are still getting the
value
Post by Gregory Lancaster
Post by J.D. Grimes
wrapped in an extra array.
On Oct 31, 2013, at 5:08 PM, Gregory Lancaster <
Post by Gregory Lancaster
No I must have screwed something up man, it only shows the last value.
I
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
searched for awhile before responding here, and I see some people say
in_array adds a line between items and sometimes other folks needed to
strip those. But I have no idea if thats whats going on here. It all
looks like it should function. :-/ Maybe I need to get off the
computer
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
for awhile.
I var_dump using the function you shared, and this is what the friend
array
Post by Gregory Lancaster
array(2) {\n [0]=>\n array(2) {\n [0]=>\n array(2) {\n
[0]=>\n
Post by Gregory Lancaster
array(2) {\n [0]=>\n array(2) {\n [0]=>\n
array(1) {\n [0]=>\n string(1) "1"\n
}\n
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
[1]=>\n string(1) "9"\n }\n [1]=>\n
string(2) "13"\n }\n [1]=>\n string(2) "16"\n }\n
[1]=>\n string(2) "13"\n }\n [1]=>\n string(1) "3"\n}\n
I dont know if that assists in debugging.
Post by J.D. Grimes
OK, you need to just delete that meta row and start over. It is messed
up
Post by Gregory Lancaster
Post by J.D. Grimes
from before. Do that and see if it works.
On Oct 31, 2013, at 4:49 PM, Gregory Lancaster <
Post by Gregory Lancaster
$active_user_friends = get_user_meta($active_user, 'friends', true);
But the issue is that it only shows the "friended" code for the last
user I
Post by Gregory Lancaster
added. If I click add on one profile, and then to a second, the
first
Post by Gregory Lancaster
Post by J.D. Grimes
no
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
longer shows friend added. Even if I have clearly added several to
the
a:2:{i:0;a:2:{i:0;a:2:{i:0;a:2:{i:0;a:2:{i:0;a:2:{i:0;s:0:"";i:1;s:2:"16";}i:1;s:2:"12";}i:1;s:2:"13";}i:1;s:2:"12";}i:1;s:1:"3";}i:1;s:2:"13";}
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
No, you don’t need to use unserialize, that is all done
automatically
Post by Gregory Lancaster
Post by J.D. Grimes
by
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the WordPress functions.
In the code you posted you still aren’t calling get_user_meta() with
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
third parameter set to true. You need to do that. Right now the
array
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
it is
Post by Gregory Lancaster
Post by J.D. Grimes
array(
0 => array( 3, 45, 6, 33 )
)
When you call in_array() it is checking if the user ID (say, 3 ) is
in
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
outer array. But it isn’t, the only thing in it is another array.
When
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
you
Post by Gregory Lancaster
Post by J.D. Grimes
call get_user_meta() with the third parameter set to true, the
returned
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
value won’t be wrapped in the outer array like that, so it will
work.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
On Oct 31, 2013, at 4:30 PM, Gregory Lancaster <
Post by Gregory Lancaster
Ah I see. Thank you for explaining JD. It sounds like maybe the
easier
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
option is to just create a separate table to store the friends list
in.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
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
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
using in_array? Because right now this code isnt working. Thats
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
only
Post by Gregory Lancaster
thing that seems to be left out - but when I tried using
$unserialize
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
like
Post by Gregory Lancaster
$unserialize = unserialize($active_user_friends);
if (in_array($author_id, $unserialize)) {
*Warning*: unserialize() expects parameter 1 to be string, array
given
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
and
*Warning*: in_array() expects parameter 2 to be array, boolean
given
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
--------
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');
Post by Gregory Lancaster
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>";
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
} 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
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
user_meta table.
Post by J.D. Grimes
There are two ways to use the *_meta functions. The way it looks
like
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
you
Post by Gregory Lancaster
Post by J.D. Grimes
are doing it is that each user has a single entry in the usermeta
table,
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
with the key ‘friends’. The value for this meta key will be stored
in
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
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
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
usermeta table, each one’s meta_value the ID of a single user that
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
user
Post by Gregory Lancaster
Post by J.D. Grimes
has friended.
The benefit of the second option is that the values may be easier
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
search. Much easier. But that may not be important to you right
now.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
(But
Post by Gregory Lancaster
Post by J.D. Grimes
then that could change later…) What if you want to show a list of
users
Post by Gregory Lancaster
Post by J.D. Grimes
who
Post by Gregory Lancaster
Post by J.D. Grimes
have friended a user? You will be able to do that easily with
this.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
With
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the first option, it is possible, but more complex and probably
harder
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
on
Post by Gregory Lancaster
Post by J.D. Grimes
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
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
case)
Post by Gregory Lancaster
Post by J.D. Grimes
or all rows with a key (as in the later case). Whichever way you
go,
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
you
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
just need to be consistent through all of the code, obviously.
-J.D.
On Oct 31, 2013, at 3:30 PM, Gregory Lancaster <
Post by Gregory Lancaster
What would be the benefit of storing the friends in separate
rows?
Post by Gregory Lancaster
Post by J.D. Grimes
I
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
did
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
not know that was an option, but don't see any clear advantages
either.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Also, I did just search the codex but dont see anything about
unique
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
rows.
Post by Gregory Lancaster
(I checked get_user_meta, delete_user_meta, update_user_meta. )
On Thu, Oct 31, 2013 at 12:24 PM, J.D. Grimes <
Post by J.D. Grimes
Yes, when you call get_user_meta() you need to set the third
parameter
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
$active_user_friends = get_user_meta( $active_user, ‘friends’,
true
Post by Gregory Lancaster
Post by J.D. Grimes
);
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Otherwise it will return a nested array. Also, just FYI,
alternatively
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
you
Post by Gregory Lancaster
Post by J.D. Grimes
could leave the get_user_meta() call the way it is, and change
it
Post by Gregory Lancaster
Post by J.D. Grimes
so
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
that
Post by Gregory Lancaster
Post by J.D. Grimes
each friend is stored in a separate meta row (but with the same
meta
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
key -
Post by Gregory Lancaster
Post by J.D. Grimes
have 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 Lancaster
I thought so. One more question if you dont mind helping;
This is the function I wrote to determine what button to show
on
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
page
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
load.
Post by Gregory Lancaster
But regardless if someone has been friended or not, it shows
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
add
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
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>";
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
} 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 <
Post by J.D. Grimes
Yes, I would write a separate function for removal, and hook
it
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
to a
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
different AJAX action for removal.
On Oct 31, 2013, at 2:48 PM, Gregory Lancaster <
Post by Gregory Lancaster
That works easier :) Once someone is friended I have it set
so
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
an
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
unfriend
Post by Gregory Lancaster
button replaces the add friend button via ajax. Is it
necessary
Post by Gregory Lancaster
Post by J.D. Grimes
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
write
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
another function for deleting a person? Not sure exactly how
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
make
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
new button function since the data being sent is not attached
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
button
Post by Gregory Lancaster
in any way. maybe I could add a value to the button that
says
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
remove
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
or
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
add, which would be passed to the function and determine what
action
Post by Gregory Lancaster
Post by J.D. Grimes
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
take?
On Thu, Oct 31, 2013 at 11:32 AM, J.D. Grimes <
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 BenderisGreat
I am not sure exactly how this would work because I start
with
Post by Gregory Lancaster
Post by J.D. Grimes
an
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
empty
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
meta_value field. I dont think I can use array_push if
there
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
is
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
not
Post by Gregory Lancaster
Post by J.D. Grimes
at
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
least 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 Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
Sent from the Wordpress Hackers mailing list archive at
Nabble.com.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
_______________________________________________
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
_______________________________________________
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
Gregory Lancaster
2013-10-31 22:23:09 UTC
Permalink
Ah i see. The wordpress codex is inexpendable to me right now haha, I dont
have all this stuff programmed into my brain yet.

So for the unfollow option, I will be using unset right? and there isnt
any special magic or voodoo I should know about here?

unset($reChkMetaValue[ $profileID ]);
I have unset arrays before in class, but never a wordpress serialized
array. Same thing?
Post by J.D. Grimes
Yes, that is what was happening with the arrays, because each time you
pulled the value out of the database, it was being wrapped in an array
(because get_user_meta() was pulling the values for all ‘friends’ keys for
the user). You added one user ID to it, and then put it back. That was
happening each time you followed a user. Now that it is working correctly,
the array will be only one layer deep. I remember coming up against this
thing with get_user_meta() myself once when I was first learning WP
development. :-)
Post by Gregory Lancaster
did I really spend a couple hours reading for a solution over the word
When you call in_array() it is checking if the user ID (say, 3 ) is in
the
Post by Gregory Lancaster
outer array. But it isn’t, the only thing in it is another array. When
you
Post by Gregory Lancaster
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.
Why are arrays stored so many tiers deep like that? Is this a wordpress
only thing, or is this normal? It looks like everytime a persons ID is
added, another instance of array(array(array( is added. What is the
reasoning behind this? Or do I understand it incorrectly?
Post by J.D. Grimes
Ah.. We forgot to add true to the gat_user_meta() in
my_add_friend_action(). Add that and you should be good to go.
On Oct 31, 2013, at 5:18 PM, Gregory Lancaster <
Post by Gregory Lancaster
Sure;
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');
Post by Gregory Lancaster
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 follow_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', true);
if (in_array($author_id, $active_user_friends)) {
$follow_status = "<button id='remove_friend'>Remove Friend</button>";
} else {
$follow_status = "<button id='add_friend'>Add Friend</button>";
}
return $follow_status;
}
echo follow_status($author_id);
Post by J.D. Grimes
Could you post your code again? Somewhere you are still getting the
value
Post by Gregory Lancaster
Post by J.D. Grimes
wrapped in an extra array.
On Oct 31, 2013, at 5:08 PM, Gregory Lancaster <
Post by Gregory Lancaster
No I must have screwed something up man, it only shows the last
value.
Post by Gregory Lancaster
Post by J.D. Grimes
I
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
searched for awhile before responding here, and I see some people say
in_array adds a line between items and sometimes other folks needed
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
strip those. But I have no idea if thats whats going on here. It
all
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
looks like it should function. :-/ Maybe I need to get off the
computer
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
for awhile.
I var_dump using the function you shared, and this is what the friend
array
Post by Gregory Lancaster
array(2) {\n [0]=>\n array(2) {\n [0]=>\n array(2) {\n
[0]=>\n
Post by Gregory Lancaster
array(2) {\n [0]=>\n array(2) {\n [0]=>\n
array(1) {\n [0]=>\n string(1) "1"\n
}\n
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
[1]=>\n string(1) "9"\n }\n [1]=>\n
string(2) "13"\n }\n [1]=>\n string(2) "16"\n }\n
[1]=>\n string(2) "13"\n }\n [1]=>\n string(1) "3"\n}\n
I dont know if that assists in debugging.
Post by J.D. Grimes
OK, you need to just delete that meta row and start over. It is
messed
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
up
Post by Gregory Lancaster
Post by J.D. Grimes
from before. Do that and see if it works.
On Oct 31, 2013, at 4:49 PM, Gregory Lancaster <
Post by Gregory Lancaster
$active_user_friends = get_user_meta($active_user, 'friends',
true);
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
But the issue is that it only shows the "friended" code for the
last
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
user I
Post by Gregory Lancaster
added. If I click add on one profile, and then to a second, the
first
Post by Gregory Lancaster
Post by J.D. Grimes
no
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
longer shows friend added. Even if I have clearly added several to
the
a:2:{i:0;a:2:{i:0;a:2:{i:0;a:2:{i:0;a:2:{i:0;a:2:{i:0;s:0:"";i:1;s:2:"16";}i:1;s:2:"12";}i:1;s:2:"13";}i:1;s:2:"12";}i:1;s:1:"3";}i:1;s:2:"13";}
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
No, you don’t need to use unserialize, that is all done
automatically
Post by Gregory Lancaster
Post by J.D. Grimes
by
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the WordPress functions.
In the code you posted you still aren’t calling get_user_meta()
with
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
third parameter set to true. You need to do that. Right now the
array
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
it is
Post by Gregory Lancaster
Post by J.D. Grimes
array(
0 => array( 3, 45, 6, 33 )
)
When you call in_array() it is checking if the user ID (say, 3 )
is
Post by Gregory Lancaster
Post by J.D. Grimes
in
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
outer array. But it isn’t, the only thing in it is another array.
When
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
you
Post by Gregory Lancaster
Post by J.D. Grimes
call get_user_meta() with the third parameter set to true, the
returned
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
value won’t be wrapped in the outer array like that, so it will
work.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
On Oct 31, 2013, at 4:30 PM, Gregory Lancaster <
Post by Gregory Lancaster
Ah I see. Thank you for explaining JD. It sounds like maybe the
easier
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
option is to just create a separate table to store the friends
list
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
in.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
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
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
using in_array? Because right now this code isnt working. Thats
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
only
Post by Gregory Lancaster
thing that seems to be left out - but when I tried using
$unserialize
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
like
Post by Gregory Lancaster
$unserialize = unserialize($active_user_friends);
if (in_array($author_id, $unserialize)) {
*Warning*: unserialize() expects parameter 1 to be string, array
given
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
and
*Warning*: in_array() expects parameter 2 to be array, boolean
given
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
--------
add_action('wp_ajax_my_add_friend_action',
'my_add_friend_action');
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
add_action('wp_ajax_nopriv_my_add_friend_action',
'my_add_friend_action');
Post by Gregory Lancaster
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>";
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
} 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
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
user_meta table.
On Thu, Oct 31, 2013 at 1:08 PM, J.D. Grimes <
Post by J.D. Grimes
There are two ways to use the *_meta functions. The way it looks
like
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
you
Post by Gregory Lancaster
Post by J.D. Grimes
are doing it is that each user has a single entry in the
usermeta
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
table,
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
with the key ‘friends’. The value for this meta key will be
stored
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
in
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
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
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
in
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
usermeta table, each one’s meta_value the ID of a single user
that
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
user
Post by Gregory Lancaster
Post by J.D. Grimes
has friended.
The benefit of the second option is that the values may be
easier
Post by Gregory Lancaster
Post by J.D. Grimes
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
search. Much easier. But that may not be important to you right
now.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
(But
Post by Gregory Lancaster
Post by J.D. Grimes
then that could change later…) What if you want to show a list
of
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
users
Post by Gregory Lancaster
Post by J.D. Grimes
who
Post by Gregory Lancaster
Post by J.D. Grimes
have friended a user? You will be able to do that easily with
this.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
With
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the first option, it is possible, but more complex and probably
harder
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
on
Post by Gregory Lancaster
Post by J.D. Grimes
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
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
case)
Post by Gregory Lancaster
Post by J.D. Grimes
or all rows with a key (as in the later case). Whichever way you
go,
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
you
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
just need to be consistent through all of the code, obviously.
-J.D.
On Oct 31, 2013, at 3:30 PM, Gregory Lancaster <
Post by Gregory Lancaster
What would be the benefit of storing the friends in separate
rows?
Post by Gregory Lancaster
Post by J.D. Grimes
I
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
did
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
not know that was an option, but don't see any clear advantages
either.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Also, I did just search the codex but dont see anything about
unique
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
rows.
Post by Gregory Lancaster
(I checked get_user_meta, delete_user_meta, update_user_meta. )
On Thu, Oct 31, 2013 at 12:24 PM, J.D. Grimes <
Post by J.D. Grimes
Yes, when you call get_user_meta() you need to set the third
parameter
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
$active_user_friends = get_user_meta( $active_user, ‘friends’,
true
Post by Gregory Lancaster
Post by J.D. Grimes
);
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Otherwise it will return a nested array. Also, just FYI,
alternatively
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
you
Post by Gregory Lancaster
Post by J.D. Grimes
could leave the get_user_meta() call the way it is, and change
it
Post by Gregory Lancaster
Post by J.D. Grimes
so
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
that
Post by Gregory Lancaster
Post by J.D. Grimes
each friend is stored in a separate meta row (but with the
same
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
meta
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
key -
Post by Gregory Lancaster
Post by J.D. Grimes
have 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 Lancaster
I thought so. One more question if you dont mind helping;
This is the function I wrote to determine what button to show
on
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
page
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
load.
Post by Gregory Lancaster
But regardless if someone has been friended or not, it shows
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
add
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
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');
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
if (in_array($author_id, $active_user_friends)) {
$friend_status = "<button id='remove_friend'>Remove
Friend</button>";
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
} else {
$friend_status = "<button id='add_friend'>Add
Friend</button>";
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
}
return $friend_status;
}
Then on page: echo $friend_status;
On Thu, Oct 31, 2013 at 11:53 AM, J.D. Grimes <
Post by J.D. Grimes
Yes, I would write a separate function for removal, and hook
it
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
to a
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
different AJAX action for removal.
On Oct 31, 2013, at 2:48 PM, Gregory Lancaster <
Post by Gregory Lancaster
That works easier :) Once someone is friended I have it
set
Post by Gregory Lancaster
Post by J.D. Grimes
so
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
an
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
unfriend
Post by Gregory Lancaster
button replaces the add friend button via ajax. Is it
necessary
Post by Gregory Lancaster
Post by J.D. Grimes
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
write
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
another function for deleting a person? Not sure exactly
how
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
make
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
new button function since the data being sent is not
attached
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
button
Post by Gregory Lancaster
in any way. maybe I could add a value to the button that
says
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
remove
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
or
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
add, which would be passed to the function and determine
what
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
action
Post by Gregory Lancaster
Post by J.D. Grimes
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
take?
On Thu, Oct 31, 2013 at 11:32 AM, J.D. Grimes <
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 BenderisGreat
I am not sure exactly how this would work because I start
with
Post by Gregory Lancaster
Post by J.D. Grimes
an
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
empty
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
meta_value field. I dont think I can use array_push if
there
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
is
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
not
Post by Gregory Lancaster
Post by J.D. Grimes
at
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
least 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 Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
Sent from the Wordpress Hackers mailing list archive at
Nabble.com.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
_______________________________________________
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
_______________________________________________
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
Gregory Lancaster
2013-10-31 23:55:38 UTC
Permalink
That was a stupid question - used delete_user_meta. Thanks again


On Thu, Oct 31, 2013 at 3:23 PM, Gregory Lancaster <
Post by Gregory Lancaster
Ah i see. The wordpress codex is inexpendable to me right now haha, I dont
have all this stuff programmed into my brain yet.
So for the unfollow option, I will be using unset right? and there isnt
any special magic or voodoo I should know about here?
unset($reChkMetaValue[ $profileID ]);
I have unset arrays before in class, but never a wordpress serialized
array. Same thing?
Post by J.D. Grimes
Yes, that is what was happening with the arrays, because each time you
pulled the value out of the database, it was being wrapped in an array
(because get_user_meta() was pulling the values for all ‘friends’ keys for
the user). You added one user ID to it, and then put it back. That was
happening each time you followed a user. Now that it is working correctly,
the array will be only one layer deep. I remember coming up against this
thing with get_user_meta() myself once when I was first learning WP
development. :-)
Post by Gregory Lancaster
did I really spend a couple hours reading for a solution over the word
When you call in_array() it is checking if the user ID (say, 3 ) is in
the
Post by Gregory Lancaster
outer array. But it isn’t, the only thing in it is another array. When
you
Post by Gregory Lancaster
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.
Why are arrays stored so many tiers deep like that? Is this a wordpress
only thing, or is this normal? It looks like everytime a persons ID is
added, another instance of array(array(array( is added. What is the
reasoning behind this? Or do I understand it incorrectly?
Post by J.D. Grimes
Ah.. We forgot to add true to the gat_user_meta() in
my_add_friend_action(). Add that and you should be good to go.
On Oct 31, 2013, at 5:18 PM, Gregory Lancaster <
Post by Gregory Lancaster
Sure;
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');
Post by Gregory Lancaster
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 follow_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', true);
if (in_array($author_id, $active_user_friends)) {
$follow_status = "<button id='remove_friend'>Remove Friend</button>";
} else {
$follow_status = "<button id='add_friend'>Add Friend</button>";
}
return $follow_status;
}
echo follow_status($author_id);
Post by J.D. Grimes
Could you post your code again? Somewhere you are still getting the
value
Post by Gregory Lancaster
Post by J.D. Grimes
wrapped in an extra array.
On Oct 31, 2013, at 5:08 PM, Gregory Lancaster <
Post by Gregory Lancaster
No I must have screwed something up man, it only shows the last
value.
Post by Gregory Lancaster
Post by J.D. Grimes
I
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
searched for awhile before responding here, and I see some people
say
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
in_array adds a line between items and sometimes other folks needed
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
strip those. But I have no idea if thats whats going on here. It
all
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
looks like it should function. :-/ Maybe I need to get off the
computer
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
for awhile.
I var_dump using the function you shared, and this is what the
friend
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
array
Post by Gregory Lancaster
array(2) {\n [0]=>\n array(2) {\n [0]=>\n array(2) {\n
[0]=>\n
Post by Gregory Lancaster
array(2) {\n [0]=>\n array(2) {\n [0]=>\n
array(1) {\n [0]=>\n string(1) "1"\n
}\n
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
[1]=>\n string(1) "9"\n }\n [1]=>\n
string(2) "13"\n }\n [1]=>\n string(2) "16"\n }\n
[1]=>\n string(2) "13"\n }\n [1]=>\n string(1) "3"\n}\n
I dont know if that assists in debugging.
Post by J.D. Grimes
OK, you need to just delete that meta row and start over. It is
messed
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
up
Post by Gregory Lancaster
Post by J.D. Grimes
from before. Do that and see if it works.
On Oct 31, 2013, at 4:49 PM, Gregory Lancaster <
Post by Gregory Lancaster
$active_user_friends = get_user_meta($active_user, 'friends',
true);
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
But the issue is that it only shows the "friended" code for the
last
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
user I
Post by Gregory Lancaster
added. If I click add on one profile, and then to a second, the
first
Post by Gregory Lancaster
Post by J.D. Grimes
no
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
longer shows friend added. Even if I have clearly added several to
the
a:2:{i:0;a:2:{i:0;a:2:{i:0;a:2:{i:0;a:2:{i:0;a:2:{i:0;s:0:"";i:1;s:2:"16";}i:1;s:2:"12";}i:1;s:2:"13";}i:1;s:2:"12";}i:1;s:1:"3";}i:1;s:2:"13";}
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
No, you don’t need to use unserialize, that is all done
automatically
Post by Gregory Lancaster
Post by J.D. Grimes
by
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the WordPress functions.
In the code you posted you still aren’t calling get_user_meta()
with
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
third parameter set to true. You need to do that. Right now the
array
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
it is
Post by Gregory Lancaster
Post by J.D. Grimes
array(
0 => array( 3, 45, 6, 33 )
)
When you call in_array() it is checking if the user ID (say, 3 )
is
Post by Gregory Lancaster
Post by J.D. Grimes
in
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
outer array. But it isn’t, the only thing in it is another array.
When
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
you
Post by Gregory Lancaster
Post by J.D. Grimes
call get_user_meta() with the third parameter set to true, the
returned
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
value won’t be wrapped in the outer array like that, so it will
work.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
On Oct 31, 2013, at 4:30 PM, Gregory Lancaster <
Post by Gregory Lancaster
Ah I see. Thank you for explaining JD. It sounds like maybe the
easier
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
option is to just create a separate table to store the friends
list
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
in.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
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
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
using in_array? Because right now this code isnt working.
Thats
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
only
Post by Gregory Lancaster
thing that seems to be left out - but when I tried using
$unserialize
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
like
Post by Gregory Lancaster
$unserialize = unserialize($active_user_friends);
if (in_array($author_id, $unserialize)) {
*Warning*: unserialize() expects parameter 1 to be string, array
given
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
and
*Warning*: in_array() expects parameter 2 to be array, boolean
given
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
--------
add_action('wp_ajax_my_add_friend_action',
'my_add_friend_action');
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
add_action('wp_ajax_nopriv_my_add_friend_action',
'my_add_friend_action');
Post by Gregory Lancaster
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>";
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
} 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
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
their
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
user_meta table.
On Thu, Oct 31, 2013 at 1:08 PM, J.D. Grimes <
Post by J.D. Grimes
There are two ways to use the *_meta functions. The way it
looks
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
like
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
you
Post by Gregory Lancaster
Post by J.D. Grimes
are doing it is that each user has a single entry in the
usermeta
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
table,
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
with the key ‘friends’. The value for this meta key will be
stored
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
in
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
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
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
in
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
usermeta table, each one’s meta_value the ID of a single user
that
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
user
Post by Gregory Lancaster
Post by J.D. Grimes
has friended.
The benefit of the second option is that the values may be
easier
Post by Gregory Lancaster
Post by J.D. Grimes
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
search. Much easier. But that may not be important to you right
now.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
(But
Post by Gregory Lancaster
Post by J.D. Grimes
then that could change later…) What if you want to show a list
of
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
users
Post by Gregory Lancaster
Post by J.D. Grimes
who
Post by Gregory Lancaster
Post by J.D. Grimes
have friended a user? You will be able to do that easily with
this.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
With
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the first option, it is possible, but more complex and probably
harder
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
on
Post by Gregory Lancaster
Post by J.D. Grimes
the DB.
The downside is that there are many more rows in usermeta
table.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
So each of these functions let you target a single row (as in
first
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
case)
Post by Gregory Lancaster
Post by J.D. Grimes
or all rows with a key (as in the later case). Whichever way
you
Post by Gregory Lancaster
Post by J.D. Grimes
go,
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
you
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
just need to be consistent through all of the code, obviously.
-J.D.
On Oct 31, 2013, at 3:30 PM, Gregory Lancaster <
Post by Gregory Lancaster
What would be the benefit of storing the friends in separate
rows?
Post by Gregory Lancaster
Post by J.D. Grimes
I
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
did
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
not know that was an option, but don't see any clear
advantages
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
either.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Also, I did just search the codex but dont see anything about
unique
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
rows.
Post by Gregory Lancaster
(I checked get_user_meta, delete_user_meta, update_user_meta.
)
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
On Thu, Oct 31, 2013 at 12:24 PM, J.D. Grimes <
Post by J.D. Grimes
Yes, when you call get_user_meta() you need to set the third
parameter
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
$active_user_friends = get_user_meta( $active_user,
‘friends’,
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
true
Post by Gregory Lancaster
Post by J.D. Grimes
);
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Otherwise it will return a nested array. Also, just FYI,
alternatively
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
you
Post by Gregory Lancaster
Post by J.D. Grimes
could leave the get_user_meta() call the way it is, and
change
Post by Gregory Lancaster
Post by J.D. Grimes
it
Post by Gregory Lancaster
Post by J.D. Grimes
so
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
that
Post by Gregory Lancaster
Post by J.D. Grimes
each friend is stored in a separate meta row (but with the
same
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
meta
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
key -
Post by Gregory Lancaster
Post by J.D. Grimes
have 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 Lancaster
I thought so. One more question if you dont mind helping;
This is the function I wrote to determine what button to
show
Post by Gregory Lancaster
Post by J.D. Grimes
on
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
page
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
load.
Post by Gregory Lancaster
But regardless if someone has been friended or not, it shows
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
add
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
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');
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
if (in_array($author_id, $active_user_friends)) {
$friend_status = "<button id='remove_friend'>Remove
Friend</button>";
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
} else {
$friend_status = "<button id='add_friend'>Add
Friend</button>";
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
}
return $friend_status;
}
Then on page: echo $friend_status;
On Thu, Oct 31, 2013 at 11:53 AM, J.D. Grimes <
Post by J.D. Grimes
Yes, I would write a separate function for removal, and
hook
Post by Gregory Lancaster
Post by J.D. Grimes
it
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
to a
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
different AJAX action for removal.
On Oct 31, 2013, at 2:48 PM, Gregory Lancaster <
Post by Gregory Lancaster
That works easier :) Once someone is friended I have it
set
Post by Gregory Lancaster
Post by J.D. Grimes
so
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
an
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
unfriend
Post by Gregory Lancaster
button replaces the add friend button via ajax. Is it
necessary
Post by Gregory Lancaster
Post by J.D. Grimes
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
write
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
another function for deleting a person? Not sure exactly
how
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
make
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
new button function since the data being sent is not
attached
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
button
Post by Gregory Lancaster
in any way. maybe I could add a value to the button that
says
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
remove
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
or
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
add, which would be passed to the function and determine
what
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
action
Post by Gregory Lancaster
Post by J.D. Grimes
to
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
take?
On Thu, Oct 31, 2013 at 11:32 AM, J.D. Grimes <
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 BenderisGreat
I am not sure exactly how this would work because I
start
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
with
Post by Gregory Lancaster
Post by J.D. Grimes
an
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
empty
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
meta_value field. I dont think I can use array_push if
there
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
is
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
not
Post by Gregory Lancaster
Post by J.D. Grimes
at
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
least 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 Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
Sent from the Wordpress Hackers mailing list archive at
Nabble.com.
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
_______________________________________________
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
_______________________________________________
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
Gregory Lancaster
2013-10-31 19:44:23 UTC
Permalink
I tried adding true but that only returns one at a time, so it only checks
if the last friend added is in the array.
Post by J.D. Grimes
$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.
Post by Gregory Lancaster
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.
Post by Gregory Lancaster
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;
Post by J.D. Grimes
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 <
Post by Gregory Lancaster
That works easier :) Once someone is friended I have it set so an
unfriend
Post by Gregory Lancaster
button replaces the add friend button via ajax. Is it necessary to
write
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
another function for deleting a person? Not sure exactly how to make
the
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
new button function since the data being sent is not attached to the
button
Post by Gregory Lancaster
in any way. maybe I could add a value to the button that says remove
or
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
add, which would be passed to the function and determine what action to
take?
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.
Post by BenderisGreat
I am not sure exactly how this would work because I start with an
empty
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
meta_value field. I dont think I can use array_push if there is not
at
Post by Gregory Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
least 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 Lancaster
Post by J.D. Grimes
Post by Gregory Lancaster
Post by BenderisGreat
Post by BenderisGreat
Sent 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
Continue reading on narkive:
Loading...