This code allows getting the number of points each user earned.
function qa_get_user_point ($user_id) {
return get_user_meta( $user_id, 'qa_point', true );
}
The problem is that in multisite case, the same user may be registered to more than one subsite (subdomain) and that the above code will return the same number of points for all subsites.
In a similar problem, they suggest to use get_user_option()
instead of get_user_meta
, which is deprecated from wordpress 3.0.0.
I think that the solution should be some thing like this (but didn't work)
function qa_get_user_point ($user_id) {
if ( is_single() ){
return get_user_meta( $user_id, 'qa_point', true ); // Original
}elseif ( is_multisite() ) {
return get_user_meta( array( 'blog_id' => get_current_blog_id(),$user_id ), 'qa_point', true );
}
}
Any Idea?
This code allows getting the number of points each user earned.
function qa_get_user_point ($user_id) {
return get_user_meta( $user_id, 'qa_point', true );
}
The problem is that in multisite case, the same user may be registered to more than one subsite (subdomain) and that the above code will return the same number of points for all subsites.
In a similar problem, they suggest to use get_user_option()
instead of get_user_meta
, which is deprecated from wordpress 3.0.0.
I think that the solution should be some thing like this (but didn't work)
function qa_get_user_point ($user_id) {
if ( is_single() ){
return get_user_meta( $user_id, 'qa_point', true ); // Original
}elseif ( is_multisite() ) {
return get_user_meta( array( 'blog_id' => get_current_blog_id(),$user_id ), 'qa_point', true );
}
}
Any Idea?
Share Improve this question asked May 7, 2019 at 12:25 HamedHamed 1033 bronze badges 7 | Show 2 more comments1 Answer
Reset to default 1You could catch and store the data separately on a per blog basis by filtering the user meta update call, via the update_user_metadata filter...
function myplugin_init() {
add_filter( 'update_user_metadata', 'myplugin_update_points', 10, 5 );
}
function myplugin_update_points( $null, $object_id, $meta_key, $meta_value, $prev_value ) {
if ($meta_key == 'qa_point') {
remove_filter( 'update_user_metadata', 'myplugin_update_points', 10, 5 );
global $wpdb; $blog_prefix = $wpdb->get_blog_prefix();
$current_blog_points = get_user_meta( $object_id, $blog_prefix.$meta_key, true );
$points_change = $meta_value - $prev_value;
if (!$current_blog_points) {$new_blog_points = $points_change;}
else {$new_blog_points = $current_blog_points + $points_change;}
update_user_meta( $object_id, $blog_prefix.meta_key, $new_blog_points );
add_filter( 'update_user_metadata', 'myplugin_update_points', 10, 5 );
}
// this means: go on with the normal execution in meta.php
return null;
}
This will automatically store points for a blog via an extra user meta entry (only) when the global points are updated. Then you could have a separate function to retrieve those points for user for a blog (2nd argument optional, defaults to current):
function myplugin_get_blog_points($user_id, $blog_id=false) {
if (!$blog_id) {$blog_id = get_current_blog_id();}
$meta_key = 'qapoint';
global $wpdb; $blog_prefix = $wpdb->get_blog_prefix($blog_id);
$points = get_user_meta( $user_id, $blog_prefix.'qapoints', true );
return $points;
}
Note this may not cover other functions handling points data within the theme, you'd have to check if it modified points via anything besides update_user_meta
(eg. delete_user_meta
) and add more code to adapt to that accordingly too. It may or may not be too difficult to be worth the hassle, but it seems possible.
Also note this will note handle points retrospectively, meaning you won't know from which blog the points came if they already exist - that will only start to be recorded once you have code like this in place.
get_user_option()
isn't deprecated. What makes you think that? Regardless, if you've saved the points as user meta, then you're not going to be able to retrieve them based on site. That information is lost. You need to set the points withupdate_user_option()
to be able to save points per site, which you will then retrieve withget_user_option()
. – Jacob Peattie Commented May 7, 2019 at 13:08get_user_option()
isn't working for me and it tells that it is deprecated. In the theme I'm using there is something like this to create points for usersif( !empty( $point->create_question ) ) qa_update_user_point( $post->post_author, $point->create_question );
and to update user's points there is a function calledqa_update_user_point($user_id, $point)
(taking two arguments) and in which there is the use ofupdate_user_meta($user_id, 'qa_point', $new_point);
. Any suggestion? – Hamed Commented May 7, 2019 at 14:11