Wordpress - Change plugin option in function.php - 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 : https://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
activate_plugins(
$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?
Cheers 🙂

deactivate_plugins is need. base plugin name. But you are passing variables.
https://codex.wordpress.org/Function_Reference/deactivate_plugins

I found a partial solution with the function "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 );
}
}
With that my plugin option switch to '1' (disabled).. but the thing is, it doesn't matter the role as it always go only for the first line..
I'm a bit lost, I don't understand why it works in one way but not the other.
My plugin fonction is:
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;
}

I think there's a little misunderstanding of a point. A plugin can't be activated for an user and deactivated for another one (in the pure concept of Plugin Activation/Deactivation). It is globally activated or globally deactivated.
Said this, depending on the way that your plugin is programmed, its functions could be disabled to certain users or groups. Instead keeping trying the deactivation method you can, for example, find the parts where your plugin admits to be filtered and take advantage of that. If there is not filters, you can try:
$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
}
In addition, alike you can hide all the html related to that option (checkboxes, menu elements and others).

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()
) );
return;
}
Thanks everyone for your time/help.

Related

Plugin with option to disable itself on certain products

I'm creating a plugin that makes a few changes to the product pages in woocommerce and i'd like to add a checkbox if the user wants to disable it for certain products.
I managed to insert the checkbox on the product edit page, but it's possible to verify if it's checked before loading the plugin?
I tried like this, but nothing happens:
global $product;
if ($product) {
$customAttributes = get_post_meta( $product->get_id(), 'custom_attributes', true );
if ( isset($customAttributes['disabled']) && $customAttributes['disabled'] == 1 ) {
return;
}
}
I searched a lot and tried other things, but i couldn't find a solution.
I always create a plugin and put it in the mu-plugins folder so you can do the check before any plugin is loaded.
Could you try the option_active_plugins hook?
add_filter( 'option_active_plugins', function( $plugins ){
global $product;
if ( $product ) {
$customAttributes = get_post_meta( $product->get_id(), 'custom_attributes', true );
if ( isset( $customAttributes['disabled'] ) && $customAttributes['disabled'] == 1 ) {
$unload_plugins[] = "my-plugins/my-plugin.php";
$plugins = array_diff( $plugins, $unload_plugins );
}
}
return $plugins;
} );
If needed, maybe wrap it in a wp or init action, but get_post_meta should work there.

How to remove Wordpress' Gutenberg and use Classic Editor just in the front page?

I am creating a new WordPress Theme and would like to use the Classic Editor specifically on the front page. To achieve this, I plan to modify the functions.php file with a few lines of code instead of relying on a plugin.
I would like to maintain the Gutenberg editor for other pages and posts.
Despite trying different methods, I have not found a solution that worked. Or I am able to remove the Gutenberg Editor from all the pages, or not remove it at all.
That's my most recent try.
Does someone know what can I do?
Thanks a lot!!!
function use_classic_editor_on_front_page( $use_block_editor, $post_type ) {
if ( ! is_admin() && is_front_page() && 'page' === $post_type ) {
return false;
}
return $use_block_editor;
}
add_filter( 'use_block_editor_for_post_type', 'use_classic_editor_on_front_page', 10, 2 );
hope this helps,
add_filter( 'use_block_editor_for_post_type', 'use_classic_editor_on_front_page' );
function use_classic_editor_on_front_page( $use_block_editor ) {
if ( 'page' === get_option( 'show_on_front' ) && get_option( 'page_on_front' ) && get_the_ID() == get_option( 'page_on_front' ) ) {
$use_block_editor = false;
}
return $use_block_editor;
}

WORDPRESS astra theme Different header menu for log in and log out user

the question is just on the title, but well here's the problem i tried this code on my function.php which at first works fine, but suddenly i realized that the menus on footer changed to be the same as the header menus.
here is the code :
function wpc_wp_nav_menu_args( $args = '' ) {
if( is_user_logged_in() ) {
$args['menu'] = 'logged-out';
} else {
$args['menu'] = 'Main Menu';
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'wpc_wp_nav_menu_args' );
What i want is to have a different header menus for user when they log in and log out, but the footer menus stays the same. i am currently learning how to add the conditional logic on the functions.php atm, but if there are other ways please let me know, thx in advance
Probably a bit late for a reply but I just did this in the Astra theme today, so thought I would share the know-how for others who might need it. It can easily be adjusted for any WordPress theme actually:
You can check the location of the menu and only change the menu in the location(s) you decide (in this example 'primary' and 'mobile_menu' which is what Astra theme calls the the main nav locations for desktop and mobile) :
function custom_wp_nav_menu_args( $args = '' ) {
if( $args['theme_location'] == 'primary' || $args['theme_location'] == 'mobile_menu' ) {
if( ! is_user_logged_in() ) {
$args['menu'] = 'logged-out';
}
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'custom_wp_nav_menu_args' );
If you dont want to use the explicit position of the menu (in my case the nav was in a widget in the footer and not a set theme location), you can also do the same thing by using the id, name or slug of the menu. For example if you want to change the menu 'test-menu' for 'test-menu-logged-out' if the user is logged out, you can use this:
function custom_2_wp_nav_menu_args ( $args = '' ) {
$menu_slug = $args['menu']->slug;
if ( ! is_user_logged_in() && $menu_slug == 'test-menu' ){
$args['menu'] = 'test-menu-logged-out';
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'custom_2_wp_nav_menu_args' );

Disable public function in WooCommerce plugin

I am using the Woocommerce Admin Custom Order Fields plugin which is causing issues when searching orders in the backend. When I run a slow query on the admin order search function it searches through these custom fields and adds 10secs or so to the search.
I have found the function that does it with the plugin and I'm trying to work out the best way to disable the custom fields being included in the search.
When I comment out this code the search is quick, a couple of seconds. I want to add an override or disable it somehow in my functions.php
public function add_search_fields( $search_fields ) {
foreach ( wc_admin_custom_order_fields()->get_order_fields() as $order_field ) {
if ( 'date' === $order_field->type ) {
array_push( $search_fields, $order_field->get_meta_key() . '_formatted' );
} else {
array_push( $search_fields, $order_field->get_meta_key() );
}
}
return $search_fields;
}
Can anyone give me some pointers on how to stop this executing without editing the plugin files directly?
Cheers
Nik
Don't comment all the function code, but just the active code inside the function, like:
public function add_search_fields( $search_fields ) {
/*
foreach ( wc_admin_custom_order_fields()->get_order_fields() as $order_field ) {
/* if ( 'date' === $order_field->type ) {
array_push( $search_fields, $order_field->get_meta_key() . '_formatted' );
} else {
array_push( $search_fields, $order_field->get_meta_key() );
}
}
*/
return $search_fields;
}
Now this function will not have any effect, as it's active code is commented.
Now overwriting any core plugin code is really something to avoid… There are always different ways to change things, like using available hooks and other things may be more complicated…

How to run a function on theme activation only in WordPress?

I want to add a capability to one of the default roles in WordPress. The add_cap article advises people to do this sort of thing on theme activation because the setting is saved to the database:
NB: This setting is saved to the database, so it might be better to run this on theme/plugin activation
The function I intend to use is:
function add_theme_caps() {
$role = get_role( 'author' );
$role->add_cap( 'edit_others_posts' );
}
add_action( 'admin_init', 'add_theme_caps');
As you can see, I'm currently hooking to admin_init, which causes the function to be run each time the admin area is accessed. How can I run the function on theme activation only?
You can use after_switch_theme . It will affect only your theme, of course.
http://codex.wordpress.org/Plugin_API/Action_Reference/after_switch_theme
I feel you should try something like this
if ( is_admin() && isset($_GET['activated'] ) && $pagenow == "themes.php" ) {
$role = get_role( 'author' );
$role->add_cap( 'edit_others_posts' );
}

Categories