Disable public function in WooCommerce plugin - php

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…

Related

Remove plugin information from the plugins page wordpress

I want to remove the information about an installed plugin from the WordPress dashboard plugins page. I have written the following code, but it doesn't work!
please guide me?
add_filter( 'all_plugin', 'remove_plugins');
function remove_plugins($plugins)
{
if(is_plugin_active('/woocommerce-checkout-manager/woocommerce-checkout-manager.php')) {
unset( $plugins['woocommerce-checkout-manager.php'] );
}
return $plugins;
}
I added this code to my template function file but it still doesn't work.
Use the filter below to delete the information of the plugin installed in WordPress and the WordPress plugins page.
Note that in the first value, put the folder and the main file of the plugin, and in the second value, only the main file of the plugin without adding the folder.
add_filter(
'all_plugins',
function ( $plugins ) {
$shouldHide = ! array_key_exists( 'show_all', $_GET );
if ( $shouldHide ) {
$hiddenPlugins = [
'woocommerce-checkout-manager/woocommerce-checkout-manager.php',
'woocommerce-checkout-manager.php',
];
foreach ( $hiddenPlugins as $hiddenPlugin ) {
unset( $plugins[ $hiddenPlugin ] );
}
}
return $plugins;
}
);

PHP WordPress - Advanced Custom Fields - Applying Filters in Pro version

I am building a WP plugin and have integrated ACF (free version) as per the documentation on their website: https://www.advancedcustomfields.com/resources/including-acf-in-a-plugin-theme/
I also use a series of filters to load the ACF fields based on the location setting:
$this->loader->add_filter( 'acf/location/rule_types', $plugin_admin, 'acf_location_rules_types' );
$this->loader->add_filter( 'acf/location/rule_values/cpt', $plugin_admin, 'acf_location_rules_values_cpt' );
$this->loader->add_filter( 'acf/location/rule_match/cpt', $plugin_admin, 'acf_location_rules_match_cpt', 10, 3 );
This code is based on this simple plugin: https://github.com/lukechapman/custom-post-template-support-for-acf/blob/master/acf-custom-post-template-support.php
Everything has worked absolutely perfectly, but since I have upgraded to ACF pro this has completely stopped working.
I can't seem to find any resources specific to adding filters for ACF pro, only the basic version. I've debugged it and can confirm paths are correct, no errors in logs, and I also added some debug code to the first callback function acf_location_rules_types() and this is not being called at all.
The script that loads the filters is working, but it's like the filter options no longer exist in ACF Pro.
Where am I going wrong with this?
EDIT
I've also tried a very simple implementation like this which is also not being called:
add_filter('acf/location/rule_types', function ($rules) {
error_log('acf/location/rule_types debug');
return false;
});
<?php
// functions.php
add_filter('acf/location/rule_types', 'acf_location_rules_types');
function acf_location_rules_types( $choices ) {
$choices['Post']['post-type-has-taxonomy'] = __('Post Type has Taxonomy');
return $choices;
}
add_filter( 'acf/location/rule_values/post-type-has-taxonomy', 'acf_location_rules_values_has_taxonomy' );
function acf_location_rules_values_has_taxonomy( $choices ) {
return array_merge($choices, get_taxonomies());
}
add_filter('acf/location/rule_match/post-type-has-taxonomy', 'acf_location_rules_match_has_taxonomy', 10, 3);
function acf_location_rules_match_has_taxonomy( $match, $rule, $options )
{
if ($rule['operator'] == '==') {
$match = is_object_in_taxonomy($options['post_type'], $rule['value']);
} elseif ($rule['operator'] == '!=') {
$match = !is_object_in_taxonomy($options['post_type'], $rule['value']);
}
return $match;
}

Wordpress dequeue scripts without handle?

I'm currently using Foobox lightbox (free) plugin, and apparently the plugins' files are loaded on every page regardless of whether it's used or not. However I would like to change this, but I can't seem to find any handles to dequeue the scripts through.
It seems that the scripts are called by the code beneath, which to me at least, doesn't show any handles. I've tried using remove_action(...) to "counter" them as well as various inputs in wp_dequeue_script() to try and target the files directly - e.g. wp_dequeue_script('foobox.free.min.js')
class Foobox_Free extends Foo_Plugin_Base_v2_1 {
const JS = 'foobox.free.min.js';
const CSS = 'foobox.free.min.css';
const CSS_NOIE7 = 'foobox.noie7.min.css';
const FOOBOX_URL = 'http://fooplugins.com/plugins/foobox/?utm_source=fooboxfreeplugin&utm_medium=fooboxfreeprolink&utm_campaign=foobox_free_pro_tab';
const BECOME_AFFILIATE_URL = 'http://fooplugins.com/affiliate-program/';
private static $instance;
public static function get_instance() {
if ( ! isset( self::$instance ) && ! ( self::$instance instanceof Foobox_Free ) ) {
self::$instance = new Foobox_Free();
}
return self::$instance;
}
/**
* Initialize the plugin by setting localization, filters, and administration functions.
*/
private function __construct() {
//init FooPluginBase
$this->init( FOOBOXFREE_FILE, FOOBOXFREE_SLUG, FOOBOX_BASE_VERSION, 'FooBox FREE' );
if (is_admin()) {
add_action('admin_head', array($this, 'admin_inline_content'));
add_action('foobox-free-settings_custom_type_render', array($this, 'custom_admin_settings_render'));
new FooBox_Free_Settings();
add_action( FOOBOX_ACTION_ADMIN_MENU_RENDER_GETTING_STARTED, array( $this, 'render_page_getting_started' ) );
add_action( FOOBOX_ACTION_ADMIN_MENU_RENDER_SETTINGS, array( $this, 'render_page_settings' ) );
add_action( 'admin_notices', array( $this, 'admin_notice_foogallery_lightboxes' ) );
add_action( 'wp_ajax_foobox_foogallery_lightboxes_ignore_notice', array( $this, 'admin_notice_foogallery_lightboxes_ignore' ) );
add_action( 'wp_ajax_foobox_foogallery_lightboxes_update', array( $this, 'admin_notice_foogallery_lightboxes_update' ) );
add_action( 'admin_print_scripts', array( $this, 'admin_notice_foogallery_lightboxes_inline_js' ), 999 );
add_filter( 'foobox-free-has_settings_page', '__return_false' );
} else {
// Render JS to the front-end pages
add_action('wp_enqueue_scripts', array($this, 'frontend_print_scripts'), 20);
add_action('foobox-free_inline_scripts', array($this, 'inline_dynamic_js'));
// Render CSS to the front-end pages
add_action('wp_enqueue_scripts', array($this, 'frontend_print_styles'));
if ( $this->is_option_checked('disable_others') ) {
add_action('wp_footer', array($this, 'disable_other_lightboxes'), 200);
}
}
}
How, if possible, do I dequeue these scripts (and styles) without editing the plugin file directly?
Edit
Below I've added the things I've tried doing to remove the scripts (all added to functions.php):
add_action( 'wp_enqueue_scripts', 'remove_foobox_scripts', 100 );
function remove_foobox_scripts() {
if ( !is_page('my-page') ) {
wp_deregister_script( 'foobox.free.min.js' );
wp_dequeue_script( 'foobox.free.min.js' );
}
}
Also tried the below, which is just a straight copy from the foobox file:
remove_action('wp_enqueue_scripts', array($this, 'frontend_print_scripts'), 20);
remove_action('foobox-free_inline_scripts', array($this, 'inline_dynamic_js'));
remove_action('wp_enqueue_scripts', array($this, 'frontend_print_styles'));
Also tried the below, where the array( part is removed:
remove_action('wp_enqueue_scripts','frontend_print_scripts', 20);
remove_action('foobox-free_inline_scripts', 'inline_dynamic_js');
remove_action('wp_enqueue_scripts', 'frontend_print_styles');
The problem with the way you are trying to do this stems from the order in which things happen in Wordpress.
You are relying on the conditional tags like is_page('my-page') to determine whether or not to load the plugin. These conditional tags do not become available until Wordpress has parsed the URL for the current query, and at this point all the plugins and your theme have already been loaded. Even if you parse the URL yourself instead of using the conditional tags you cannot be sure your code will run before the plugins are loaded.
The solution is to add your code as an mu-plugin. These are loaded before normal plugins so you can use an option (option name) filter here to alter the plugins you want to be loaded.
Option filters pass an array to your function containing the values which are set for that option, so in this case you want to hook option_active_plugins.
You can find the values to use for by running print_r(get_option('active_plugins')); or look through the plugins folder of your wordpress install.
The following example is specific to your question, but you could modify it to make a more comprehensive set of rules, adding and removing multiple plugins on different pages based on many conditions.
My function checks you are not in wp-admin and then has 2 conditions. The first disables a normally active plugin on the specified pages, the second enables a normally disabled plugin on the specified pages.
<?php
add_filter( 'option_active_plugins', 'add_or_remove_plugins' );
function add_or_remove_plugins( $plugins ) {
if (strpos( $_SERVER['REQUEST_URI'], '/wp-admin/' ) !== 0) { // Disable in admin pages or admin plugin settings stop working properly
if (strpos( $_SERVER['REQUEST_URI'], '/remove-plugin-here/' ) === 0) { // Conditonal tags still unavailable so you have to parse urls yourself
$k = array_search( 'foobox-image-lightbox/foobox-free.php', $plugins ); // This will stop an active plugin from loading
if( false !== $k ){
unset( $plugins[$k] );
}
}
if (strpos( $_SERVER['REQUEST_URI'], '/add-plugin-here/' ) === 0) {
$plugins[] = 'foobox-image-lightbox/foobox-free.php'; // This will load the plugin along with all the active plugins
}
}
return $plugins;
}
?>
To install just change the values to suit, paste into a file and upload into your mu-plugins folder
EDIT
Looks like your inline js is added to wp_head during the constructor of the Foobox_Free class. You could try adding this to your functions.php:
add_action( 'wp_head', 'remove_dynamic_js' );
function remove_dynamic_js(){
$foo = Foobox_Free::getInstance();
remove_action('wp_head', array($foo, 'inline_dynamic_js'));
}
or if that doesn't work then maybe this:
add_action( 'wp_head', 'remove_dynamic_js' );
function remove_dynamic_js(){
remove_action('wp_head', array('Foobox_Free', 'inline_dynamic_js'));
}
The action is added inside a private function, so I don't know if either of those will actually work. Give it a shot. If not my first answer will as it stops the plugin from loading at all on the specified pages.
UPDATE
Well, I was close... Here's the code to remove the scripts, as supplied by the plugin author.
$foobox = Foobox_Free::get_instance();
remove_action('foobox-free_inline_scripts', array($foobox, 'inline_dynamic_js'));

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 : 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.

Remove main editor from wordpress edit page screen

Anyone know of a way to remove the main editor from the page edit screen? And not just with css. I've added a few other meta boxes with the tinymce and they collide with the main one.
I have a class that removes other meta boxes from the edit screen, but I cant get rid of the main editor this way. I've tried to add 'divpostrich' and 'divpost' to the array in the class (but with no luck):
class removeMetas{
public function __construct(){
add_action('do_meta_boxes', array($this, 'removeMetaBoxes'), 10, 3);
}
public function removeMetaBoxes($type, $context, $post){
/**
* usages
* remove_meta_box($id, $page, $context)
* add_meta_box($id, $title, $callback, $page, $context = 'advanced', $priority = 'default')
*/
$boxes = array( 'slugdiv', 'postexcerpt', 'passworddiv', 'categorydiv',
'tagsdiv', 'trackbacksdiv', 'commentstatusdiv', 'commentsdiv',
'authordiv', 'postcustom');
foreach ($boxes as $box){
foreach (array('link', 'post', 'page') as $page){
foreach (array('normal', 'advanced', 'side') as $context){
remove_meta_box($box, $type, $context);
}
}
}
}
}
$removeMetas = new removeMetas();
I have also tried removing the 'divpostrich' with jquery. But cant figure out where to put the js for it to work. When I remove the 'postdivrich' in the browser with firebug - my remaining tinymce fields work perfect.
Any ideas?
There is built in WP support for this so you don't have to fiddle directly with the globals and ensure forwards compatibility if they ever change how features are handled. The WP Core code does pretty much the exact same logic as #user622018 answer however
function remove_editor() {
remove_post_type_support('page', 'editor');
}
add_action('admin_init', 'remove_editor');
What you are looking for is the global $_wp_post_type_features array.
Below is a quick example of how it can be used
function reset_editor()
{
global $_wp_post_type_features;
$post_type="page";
$feature = "editor";
if ( !isset($_wp_post_type_features[$post_type]) )
{
}
elseif ( isset($_wp_post_type_features[$post_type][$feature]) )
unset($_wp_post_type_features[$post_type][$feature]);
}
add_action("init","reset_editor");
Add the following code to your functions.
function remove_editor_init() {
if ( is_admin() ) {
$post_id = 0;
if(isset($_GET['post'])) $post_id = $_GET['post'];
$template_file = get_post_meta($post_id, '_wp_page_template', TRUE);
if ($template_file == 'page-home.php') {
remove_post_type_support('page', 'editor');
}
}
}
add_action( 'init', 'remove_editor_init' );
Couldn't you just disable the TinyMCE editor, leaving the HTML editor, as your meta boxes are colliding with it? :)
To disable the editor you will need to edit your wp-config.php file and add this line to the top:
define('DISALLOW_FILE_EDIT', true);

Categories