Wordpress-在function.php中更改插件选项
对于我的网站,我正在使用插件Woocommerce变体到表格网格,但我想将此插件仅限于某些角色“管理员”和“批发商”。(我的网站面向批发商和“普通”客户) 无论如何,我想通过检查用户角色来取消插件的激活,所以我尝试了以下解决方案: 不起作用 我的插件中有一个名为$vartable_disabled的变量,它是一个布尔值,用于“全局禁用”插件 所以我想在functions.php中做一些事情,比如:Wordpress-在function.php中更改插件选项,php,wordpress,user-roles,custom-wordpress-pages,Php,Wordpress,User Roles,Custom Wordpress Pages,对于我的网站,我正在使用插件Woocommerce变体到表格网格,但我想将此插件仅限于某些角色“管理员”和“批发商”。(我的网站面向批发商和“普通”客户) 无论如何,我想通过检查用户角色来取消插件的激活,所以我尝试了以下解决方案: 不起作用 我的插件中有一个名为$vartable_disabled的变量,它是一个布尔值,用于“全局禁用”插件 所以我想在functions.php中做一些事情,比如: add_action('admin_init', 'my_option_change_plugin
add_action('admin_init', 'my_option_change_plugins');
function my_option_change_plugins()
{
global $current_user;
if (!in_array('administrator' || 'wholesaler', $current_user->roles)) {
deactivate_plugins( // activate for variation-table
$vartable_disabled == 0
$vartable_position == 'under'
);
} else { // desactivate for those than can't use it
activate_plugins(
$vartable_disabled == 1
$vartable_position == 'side'
);
}
但可以肯定的是,我做错了什么,我一整天都在尝试很多不同的事情,不可能找到答案
有人能帮忙吗
干杯
需要停用插件。基本插件名称。但是您正在传递变量
我找到了一个使用函数“update\u option()的部分解决方案
我的插件选项切换到“1”(已禁用)。。但问题是,这个角色并不重要,因为它总是只出现在第一行
我有点不知所措,我不明白为什么它以一种方式工作,而不是另一种方式
我的插件功能是:
if ( ((get_post_meta($product->id, 'disable_variations_table', true) == 1 || !empty($checkcat)) || $vartable_disabled == 1) && $vartable_shortcd != 1) {
// Enqueue variation scripts
wp_enqueue_script( 'wc-add-to-cart-variation' );
// Load the template
wc_get_template( 'single-product/add-to-cart/variable.php', array(
'available_variations' => $product->get_available_variations(),
'attributes' => $product->get_variation_attributes(),
'selected_attributes' => $product->get_variation_default_attributes()
) );
return;
}
我认为有一点误解。插件不能为一个用户激活而为另一个用户停用(在插件激活/停用的纯概念中)。它被全局激活或全局停用
这样说,根据插件的编程方式,其功能可能会被某些用户或组禁用。相反,你可以继续尝试停用方法,例如,找到你的插件所在的部分并利用它。如果没有筛选器,您可以尝试:
$current_user = wp_get_current_user();
if ( !in_array( array( 'administrator' , 'wholesaler' ) , (array) $current_user->roles ) ) {
update_option( 'vartable_disabled' , 1 ); // Assuming 'vartable_disabled' parameter really disables the plugin for the current user
}
此外,您还可以隐藏与该选项(复选框、菜单元素和其他)相关的所有html。它最终没有按预期工作,因为更改选项需要某种“管理员”权限,因此对于“客户”等其他角色,它没有按预期更改选项
无论如何,我与插件开发人员合作(感谢Spyros),解决方案是直接在插件中挂钩(Functionality现在添加到新的插件版本中;)
已添加以下代码:
// if the table is disabled for this product display the default select menus
$checkcat = array();
if (is_array($vartable_categories_exc) && is_array($pcids)) {
$checkcat = array_intersect($pcids, $vartable_categories_exc);
}
$checkrole = array();
if (is_array($vartable_roles_exc) && is_user_logged_in()) {
$user_info = get_userdata(get_current_user_id());
$checkrole = array_intersect($user_info->roles, $vartable_roles_exc);
}
if (!is_user_logged_in() && is_array($vartable_roles_exc) && in_array('guest', $vartable_roles_exc)) {
$checkrole['guest'] = 'guest';
}
if (
((get_post_meta($product->id, 'disable_variations_table', true) == 1 || !empty($checkcat)) || $vartable_disabled == 1 || !empty($checkrole))
&& get_post_meta($product->id, 'disable_variations_table', true) != 2
&& $vartable_shortcd != 1) {
// Enqueue variation scripts
wp_enqueue_script( 'wc-add-to-cart-variation' );
// Load the template
wc_get_template( 'single-product/add-to-cart/variable.php', array(
'available_variations' => $product->get_available_variations(),
'attributes' => $product->get_variation_attributes(),
'selected_attributes' => $product->get_variation_default_attributes()
) );
return;
}
谢谢大家的时间/帮助。你好,阿希德。有效地,我尝试了这一个去激活依赖于角色的基础。问题是,在那之后不可能基于角色重新激活插件(我猜插件激活是在启动时完成的)。无论如何,我想“静音”或根据角色暂停插件。如果我不能动态更改其他插件的选项,我想禁用特定用户角色的插件为什么添加两个等号“$vartable_disabled==1”对不起,这是一个错误,但没有更改任何内容,我可以访问此变量(我的插件)动态?是的,这从一开始就是一个误解。但是,我尝试了您的解决方案并添加了以下内容:else{update_option('vartable_disabled',0);},但它具有相同的行为。。当我在admin/guest之间切换时,只需关闭(“1”)插件选项(vartable_disable),但从不打开(“0”)
// if the table is disabled for this product display the default select menus
$checkcat = array();
if (is_array($vartable_categories_exc) && is_array($pcids)) {
$checkcat = array_intersect($pcids, $vartable_categories_exc);
}
$checkrole = array();
if (is_array($vartable_roles_exc) && is_user_logged_in()) {
$user_info = get_userdata(get_current_user_id());
$checkrole = array_intersect($user_info->roles, $vartable_roles_exc);
}
if (!is_user_logged_in() && is_array($vartable_roles_exc) && in_array('guest', $vartable_roles_exc)) {
$checkrole['guest'] = 'guest';
}
if (
((get_post_meta($product->id, 'disable_variations_table', true) == 1 || !empty($checkcat)) || $vartable_disabled == 1 || !empty($checkrole))
&& get_post_meta($product->id, 'disable_variations_table', true) != 2
&& $vartable_shortcd != 1) {
// Enqueue variation scripts
wp_enqueue_script( 'wc-add-to-cart-variation' );
// Load the template
wc_get_template( 'single-product/add-to-cart/variable.php', array(
'available_variations' => $product->get_available_variations(),
'attributes' => $product->get_variation_attributes(),
'selected_attributes' => $product->get_variation_default_attributes()
) );
return;
}