简体   繁体   English

检查用户是否在主站点上的组中从网络的子站点(WP Multisite)

[英]Check if user is in a group on the main site FROM a subsite of a network (WP Multisite)

I'm using Woocommerce + the groups plugin on my main site to promote users to a 'Premium' group upon purchasing items which works great. 我在我的主网站上使用Woocommerce +群组插件,在购买效果很好的商品时,将用户推广到“高级”群组。

If a user on the main site then browses to the second site within my network, I can no longer check to see if they're within the 'Premium' group. 如果主站点上的用户然后浏览到我网络中的第二个站点,我将无法再检查他们是否在“高级”组中。

On my main site, I can use this code: 在我的主站点上,我可以使用以下代码:

<?php 
    $user_id = get_current_user_id();
    $group = Groups_Group::read_by_name( 'Premium' );
    if ( Groups_User_Group::read( $user_id, $group->group_id ) ) { 
?>

    Premium content here!

<?php } ?>

But this does not work on the subsite. 但这不适用于子网站。 Is there anyway I can check to see if the user is in a group on my main site FROM a subsite? 无论如何我可以检查用户是否在我的主站点上的一个组中来自子站点?

I managed to achieve this using the following code: 我设法使用以下代码实现此目的:

<?php

    $blog_id = 1; //set the blog id to the main site id 

    switch_to_blog( $blog_id );

    $user_id = get_current_user_id();
    $group = Groups_Group::read_by_name( 'Premium' );
    if ( Groups_User_Group::read( $user_id, $group->group_id ) ) { 
        echo 'PREMIUM!!';
    } else {
        echo 'Not premium';
    }

    restore_current_blog();

?>

Probably not a definitive answer but this should lead to it at least. 可能不是一个明确的答案,但这应该至少导致它。

Your current code is using a wordpress function to get the users id. 您当前的代码使用wordpress函数来获取用户ID。 This should work across the board and is therefore fine. 这应该全面运作,因此很好。 The next variabe/method uses the class from the groups plugin which I presume is just not being pulled on the subsite. 下一个变量/方法使用来自groups插件的类,我认为它不是在子网站上被拉动的。

$user_id = get_current_user_id();
$group = Groups_Group::read_by_name( 'Premium' );
Groups_User_Group::read( $user_id, $group->group_id )

From the look of the class files 从类文件的外观

$group = Groups_Group::read_by_name( 'Premium' );

This line simply does the method below 该行简单地执行以下方法

public static function read_by_name( $name ) {
    global $wpdb;
    $result = false;
    $group_table = _groups_get_tablename( 'group' );
    $group = $wpdb->get_row( $wpdb->prepare(
        "SELECT * FROM $group_table WHERE name = %s",
        $name
    ) );
    if ( isset( $group->group_id ) ) {
        $result = $group;
    }
    return $result;
}

The next line 下一行

Groups_User_Group::read( $user_id, $group->group_id )

Does this method 这种方法吗?

public static function read( $user_id, $group_id ) {
    global $wpdb;
    $result = false;

    $user_group_table = _groups_get_tablename( 'user_group' );
    $user_group = $wpdb->get_row( $wpdb->prepare(
        "SELECT * FROM $user_group_table WHERE user_id = %d AND group_id = %d",
        Groups_Utility::id( $user_id ),
        Groups_Utility::id( $group_id )
    ) );
    if ( $user_group !== null ) {
        $result = $user_group;
    }
    return $result;
}

So all you are doing is pretty simple calls to the database. 所以你所做的只是对数据库的简单调用。 The issue being that due to the use of multisite there will be discrepancies with the table prefixes etc. 问题是由于使用多站点,表格前缀等会有差异。

According to this part of the codex 根据这部分法典

http://codex.wordpress.org/Class_Reference/wpdb#Multi-Site_Variables http://codex.wordpress.org/Class_Reference/wpdb#Multi-Site_Variables

You can access other sites on the network much in the same way you can access the current site using $wpdb as a global variable. 您可以使用$wpdb作为全局变量以与访问当前站点相同的方式访问网络上的其他站点。 Therefore once you have ascertained the correct names of your tables, you should be able to modify these methods into simple functions that do the necessary work. 因此,一旦确定了表的正确名称,就应该能够将这些方法修改为执行必要工作的简单函数。

The first method doesn't even need to be used 甚至不需要使用第一种方法

$group = Groups_Group::read_by_name( 'Premium' );

As $group simply equals the id shown in the dashboard 由于$group只是等于仪表板中显示的id

woocommerce仪表板

Therefore the line can be coded statically ie 因此,线路可以静态编码,即

$group = 2;

The second method is purely checking if there is an entry of user_id 2 for example and group_id 2 say, in the group_users_group table. 第二种方法是在group_users_group表中纯粹检查是否存在user_id 2和group_id 2的条目。 Here we have user 2 with both registered and premium group privileges. 这里我们有用户2具有注册和高级组权限。 Shown here. 这里显示。

group_user_group woocommerce

Therefore user 2 (again id can be seen in the dashboard in the users section) will qualify. 因此,用户2(再次在用户部分的仪表板中可以看到id)将符合资格。

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Magento多站点-在主站点和子站点中显示产品,但只能通过子站点进行订购 - Magento multisite - display of product in main site and subsite but order it only through the subsite WordPress多站点-从网络上的其他站点获取用户角色 - Wordpress multisite - Get a users role from a different site on the network 如何从另一个站点(在同一网络上)替换一个WordPress多站点站点中的所有页面 - How to replace all pages in one WordPress multisite site from another site (on same network) WP 查询正在从临时站点访问帖子,而不是主站点 - WP query is accessing posts from staging site, not main site 如何将多站点站点重定向到主站点 - How to redirect Multisite sites to the main site 如果网址为“www”,我的子网站重定向会显示主网站的主页 - My subsite redirects do the homepage of the main site if the url has 'www' 如何将Wordpress子站点从登台迁移到实时服务器(在Wordpress多站点上)? - How do I migrate a wordpress subsite from a staging to a live server (on wordpress multisite)? 在WordPress Multisite子站点目录中创建目录 - Create a directory in WordPress Multisite subsite directory Wordpress 多站点问题 - 子站点管理员不再工作 - Wordpress multisite issue - subsite admin no longer working 如何在wordpress多站点中重建子站点url - how to reconstruct subsite url in wordpress multisite
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM