【问题标题】:Wordpress - Change plugin option in function.phpWordpress - 更改 function.php 中的插件选项
【发布时间】:2017-02-25 21:25:29
【问题描述】:

对于我的网站,我使用插件 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'
        );
    }

但我肯定做错了什么,我一整天都尝试了很多不同的东西,却无法弄清楚。

有人可以帮忙吗?

干杯????

【问题讨论】:

    标签: php wordpress user-roles custom-wordpress-pages


    【解决方案1】:

    它最终没有按预期工作,因为要更改选项,您需要某种“管理员”权限,因此对于“客户”等其他角色,它没有按预期更改选项。

    无论如何,我与插件开发人员合作(感谢 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;
    }
    

    感谢大家的时间/帮助。

    【讨论】:

      【解决方案2】:

      我认为有一点误解。不能为用户激活插件并为另一个用户停用插件(在插件激活/停用的纯概念中)。它是全局激活或全局停用

      也就是说,根据您的插件的编程方式,它的功能可能对某些用户或组禁用。相反,您可以继续尝试停用方法,例如,找到您的插件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(复选框、菜单元素等)。

      【讨论】:

      • 是的,从一开始就是个误会。
      • 但是,我尝试了您的解决方案并添加了以下内容: else{ update_option( 'vartable_disabled' , 0 );但它具有相同的行为.. 只是关闭('1')插件选项(vartable_disable),但当我在管理员/访客之间切换时从不打开它('0')
      【解决方案3】:

      我找到了函数“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;
      }
      

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

        • 嗨,Arshid。实际上,我尝试根据角色来停用此功能。问题是,在那之后不可能重新激活基于角色的插件(我猜插件激活是在启动时完成的)。无论如何,我想根据角色“静音”或暂停插件。
        • 如果我不能动态更改其他插件的选项,我想禁用特定用户角色的插件
        • 为什么要加两个等号'$vartable_disabled == 1'
        • 抱歉,这是一个错误但没有改变任何东西,我可以动态访问(我的插件的)这个变量吗?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多