最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

multisite - get_user_meta() in multiste with respect to subdomain

programmeradmin0浏览0评论

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
  • 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 with update_user_option() to be able to save points per site, which you will then retrieve with get_user_option(). – Jacob Peattie Commented May 7, 2019 at 13:08
  • is this for an existing plugin integration or your own code? – majick Commented May 7, 2019 at 13:09
  • @majick it is from a theme I'm using – Hamed Commented May 7, 2019 at 13:26
  • @JacobPeattie get_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 users if( !empty( $point->create_question ) ) qa_update_user_point( $post->post_author, $point->create_question ); and to update user's points there is a function called qa_update_user_point($user_id, $point) (taking two arguments) and in which there is the use of update_user_meta($user_id, 'qa_point', $new_point);. Any suggestion? – Hamed Commented May 7, 2019 at 14:11
  • If this isn't functionality that you've built yourself, then you need to take it up with the theme author, but it looks like the theme is built so that points are stored globally. You're not going to be able to retrieve points per site without the theme being changed. – Jacob Peattie Commented May 8, 2019 at 1:16
 |  Show 2 more comments

1 Answer 1

Reset to default 1

You 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.

发布评论

评论列表(0)

  1. 暂无评论