Wordpress - 在 function.php 中更改插件选项

Wordpress - Change plugin option in function.php

对于我的网站,我正在使用插件 Woocommerce Variations to Table Grid,但我想限制这个插件只用于某些角色“管理员”和'批发商'。 (我的网站是为批发商和“普通”客户准备的)

无论如何,我想通过检查用户角色来停用插件,所以我尝试了以下解决方案:https://wordpress.stackexchange.com/questions/159085/deactivate-plugin-for-a-specific-user-group 没用。

我的插件中有一个名为 $vartable_disabled 的变量,它是一个“全局禁用”插件的布尔值。

所以我想在我的 functions.php 中做一些事情,例如:

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'
        );
    }

但可以肯定的是我做错了什么,我整天尝试了很多不同的东西,不可能弄清楚。

有人可以帮忙吗?

干杯

deactivate_plugins 是需要的。基本插件名称。但是你正在传递变量。

https://codex.wordpress.org/Function_Reference/deactivate_plugins

我找到了函数 "update_option()"

的部分解决方案
add_action('admin_init', 'my_filter_the_plugins');
function my_filter_the_plugins()
{
    global $current_user;
    if (in_array('administrator', $current_user->roles)) {
        update_option( 'vartable_disabled', 0 );
    } else { // activate for those than can use it
        update_option( 'vartable_disabled', 1 );
    }
}

有了这个,我的插件选项切换到“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;
}

我觉得有一点误解。插件不能为一个用户激活而为另一个用户停用(在插件 Activation/Deactivation 的纯粹概念中)。它在全球范围内 activated 或在全球范围内 deactivated.

也就是说,根据您的插件编程方式,某些用户或组可能会禁用其功能。相反,您可以继续尝试停用方法,例如,找到您的插件 admits to be filtered 所在的部分并加以利用。如果没有过滤器,您可以尝试:

$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(复选框、菜单元素和其他)。

它最终没有按预期工作,因为要更改选项你需要某种 "administrator" 权限,因此对于 "customer" 等其他角色,它没有按预期更改选项。

无论如何,我与插件开发人员合作(感谢 Spyros),解决方案是直接在插件中挂钩(功能现在已添加到新插件版本;))

已添加以下代码:

    //  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;
}

感谢大家time/help。