[英]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
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.
这里显示。
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.