bkseen bkseen - 1 year ago 90
PHP Question

Wordpress - Change plugin option in function.php

For my website I’m using the plugin Woocommerce Variations to Table Grid, but I would like to restrict this one only for some roles ‘Administrator’ and ‘wholesaler’. (my website is for wholesalers and ‘normal’ customer)

Anyway I was thinking to just desactivate the plugin by checking the user role so I tried the following solution : http://wordpress.stackexchange.com/questions/159085/deactivate-plugin-for-a-specific-user-group
Doesn’t work.

I’ve got a variable in my plugin called $vartable_disabled which is a boolean which “Disable globally” the plugin.

So I am thinking to do something in my functions.php like:

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
$vartable_disabled == 1
$vartable_position == 'side'

But for sure I’m doing something wrong, I tried plenty of different thing the whole day, impossible to figure it out.

Anyone can help?


Answer Source

It finally didn't work as expected because to change an option you need kind of "administrator" rights, so for the other roles like "customer" it didn't change the option as expected.

Anyway, instead I worked with the plugin developer (thanks Spyros) and the solution has been to hook directly in the plugin (functionnality now added to the new plugin version ;))

The following code has been added:

    //  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()
    ) );

Thanks everyone for your time/help.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download