Remove function from Wordpress Plugin [duplicate] - php

This question already has answers here:
remove_action() not working in WordPress plugin
(4 answers)
Removing functions from Woocommerce hooks
(2 answers)
Closed 2 years ago.
The WooCommerce plugin is inserting a script into the footer of my site. I have been attempting to remove this, but nothing seems to be working.
The following action is used to add the script to the footer:
add_action( 'wp_footer', 'wc_print_js', 25 );
I have tried removing this by placing the following in the child theme function file:
add_action( 'after_setup_theme', 'remove_wc_plugin_function', 0 );
function remove_wc_plugin_function() {
remove_action( 'wp_footer', 'wc_print_js' );
}
Have also tried just replacing the whole wc-core-functions.php file in my child theme, and commenting out the relevant functions, but that also doesnt work. I assume the function does not exist when the child theme function file is run. I then attempted to remove all jquery from the home page, which also doesnt get remove the script.
add_action( 'wp_enqueue_scripts', 'my_deregister_javascript' );
function my_deregister_javascript() {
if ( is_front_page('home') ) {
wp_deregister_script( 'jquery' );
}
}
Have also tried just replacing the whole wc-core-functions.php file in my child theme, and commenting out the relevant functions, but that also doesnt work. How can get I get rid of this bloody script in the footer (or preferably just get rid of a specific function)?
TLDR: How to remove a Wordpress plugin function, the script that was inserted with that function?

https://developer.wordpress.org/reference/functions/remove_action/:
To remove a hook, the $function_to_remove and $priority arguments must match when the hook was added.
The function was added to the hook using
add_action( 'wp_footer', 'wc_print_js', 25 );
but you tried to remove it using only remove_action( 'wp_footer', 'wc_print_js' );, and then it will use the default value 10 for that third parameter.
So the correct way here to remove this function from the action again should be
remove_action( 'wp_footer', 'wc_print_js', 25 );

Related

dequeue function in wordpress is not working

I am trying to dequeue some scripts from non-relevant pages in wordpress. It is working fine with some (like contact-form-7) but not with other plugins. Is there any reasons these .js and .css files are still being loaded, despite being dequeued?
Code below is loaded via functions.php in child-theme. Relevant plugin is woo-variation-swatches.
add_action( 'wp_enqueue_scripts', 'custom_swatches_script_conditional_loading' );
function custom_swatches_script_conditional_loading(){
// Edit page IDs here
if(! is_page(39341) )
{
wp_dequeue_style('woo-variation-swatches'); // Dequeue CSS file.
wp_dequeue_style('woo-variation-swatches-theme-override'); // Dequeue CSS file.
wp_dequeue_style('woo-variation-swatches-tooltip'); // Dequeue CSS file.
wp_dequeue_script('woo-variation-swatches'); // Dequeue JS Script file.
}
}
You want to either hook it after wp_enqueue_scripts like wp_print_styles or simply try setting the priority of your add_action.
Looking at the plugin source code. It enqueue's the scripts with priority 15.
woo-variation-swatches.php line 103.
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ), 15 );
So really, anything priority higher than 15 should dequeue it like this :
add_action( 'wp_enqueue_scripts', 'custom_swatches_script_conditional_loading', 99 );

Remove structured data from Woocommerce single product pages

I am trying to remove the structured data that Woocommerce adds to the product pages.
I did some research and found that WC_Structured_Data::generate_product_data() generates the structured data markup. It's hooked in the woocommerce_single_product_summary action hook in the woocommerce/templates/content-single-product.php template file.
I tried by adding the following code to the functions.php
remove_action( 'woocommerce_single_product_summary', 'WC_Structured_Data::generate_product_data()', 60 );
So structured data wouldn't be added by Woocommerce, but it doesn't work…
Am I doing something wrong? Is there another way to do what I am trying to achieve?
Instead, you can use dedicated filter hook 'woocommerce_structured_data_product' that is located in WC_Structured_Data for generate_product_data() method nulling the structured data output in single product pages:
add_filter( 'woocommerce_structured_data_product', 'structured_data_product_nulled', 10, 2 );
function structured_data_product_nulled( $markup, $product ){
if( is_product() ) {
$markup = '';
}
return $markup;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
I suspect people want to remove the default tabs and they come here after they see the Woocommerce content-single-product.php template. In this template you see that generate_product_data() is hooked with priority 60.
After inspecting the hooks that run on woocommerce_single_product_summary.
You can easily remove the tabs with:
remove_action( 'woocommerce_single_product_summary', 'woocommerce_output_product_data_tabs', 60 );
I think Woocommerce forgot to mention this add_action.
This is how you can remove hooks associated with instantiated object method. You have to find the variable that holds the new Object instance.
In this case the main WooCommerce object is accessible as $GLOBALS['woocommerce'] and it has public property $structured_data which holds an instance of the WC_Structured_Data object.
Hence, to remove the hook in the question you can write this code:
remove_action( 'woocommerce_before_main_content', array( $GLOBALS['woocommerce']->structured_data, 'generate_website_data' ), 30 );
Add to functions.php:
add_action('wp_loaded', function() {
remove_action('woocommerce_single_product_summary', [$GLOBALS['woocommerce']->structured_data, 'generate_product_data'], 60);
});
Unhooks WC_Structured_Data::generate_product_data(). Will not waste resources on generating product data first for no reason and then "nulling" that same generated data a moment later using a filter.

Remove action fails to remove default WooCommerce hooked function

I am trying to move the product tabs from woocommerce_after_single_product_summary to woocommerce_before_single_product_summary. Essentially, I want to replace the image of the product on the single product with a tab that has the image gallery, but all the other tabs show up in that same container. I have issues trying to get the container around the tabs to act like the container around the image, but what's got me stumped is how to remove the tabs from the woocommerce_after_single_product_summary section. I can remove "related products" no problem, from the same section, but nothing I've done has budged the tabs.
This works fine to remove the image:
remove_action ('woocommerce_before_single_product_summary',
'woocommerce_show_product_images',20);
This works fine to add the tabs in its place (Other than the container issue):
add_action( 'woocommerce_before_single_product_summary',
'woocommerce_output_product_data_tabs', 20 );
But this never seems to get rid of the tabs:
remove_action( 'woocommerce_after_single_product_summary',
'woocommerce_output_product_data_tabs', 10 );
I've checked that 10 is the correct number (which I found does make a difference) but it IS correct. I traced the add_action to
woocommerce/includes/wc-template-hooks.php:
/**
* After Single Products Summary Div.
*
* #see woocommerce_output_product_data_tabs()
* #see woocommerce_upsell_display()
* #see woocommerce_output_related_products()
*/
add_action( 'woocommerce_after_single_product_summary',
'woocommerce_output_product_data_tabs', 10 );
add_action( 'woocommerce_after_single_product_summary',
'woocommerce_upsell_display', 15 );
add_action( 'woocommerce_after_single_product_summary',
'woocommerce_output_related_products', 20 );
I can remove related products with:
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
I've tried the suggestion (in another topic) that the hook might need a "template redirect" but that didn't work either. What I tried was:
// Trying template redirect
function esb_product_category_filter_changes()
{remove_action('woocommerce_after_single_product_summary',
'woocommerce_output_product_data_tabs', 10);
}
do_action('template_redirect','esb_product_category_filter_changes');
Any ideas? I've searched all the possibly relevant plugins, turned a bunch off, and still the same behavior. I can put the tabs anywhere I want, but I can't get rid of them.
I have tested on Storefront theme:
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_product_data_tabs', 10 );
And it works. So your issue can be due to your theme or some plugins that are making some customizations on this.
How to check it: With the following hooked function you will get the raw data output for woocommerce_after_single_product_summary hook (all callbacks, with priorities and arguments). This way you will be able to see if woocommerce_output_related_products is hooked in it and what is the priority.
This testing function will output the data for admins only in archives and product pages:
add_action( 'woocommerce_before_main_content', function(){
// Only for admin user role
if( ! current_user_can('edit_products')) return;
global $wp_filter;
echo '<pre>';
print_r( $wp_filter['woocommerce_after_single_product_summary'] );
echo '</pre>';
}, 50 );
Code goes in function.php file of the active child theme (or active theme).
Now you can get the correct priority to set in the remove_action() (and remove this testing code).
You could also try the following code (without any guaranty):
add_action( 'woocommerce_after_single_product_summary', 'removing_product_tabs', 2 );
function removing_product_tabs(){
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_product_data_tabs', 10 );
}
Code goes in function.php file of the active child theme (or active theme).

remove_action() not working in Woocommerce

i'm trying to change the order of my product page in woocommerce but i have a few problems with the remove_action().
I activated the child theme and in my functions.php i'm tryin to remove the add-to-cart button to add it back on another position.
I tried to put my remove_action() into a function that gets executed right after my parent theme loads.
function change_order() {
add_action('woocommerce_single_product_summary','woocommerce_template_single_add_to_cart', 15 );
remove_action('woocommerce_single_product_summary','woocommerce_template_single_add_to_cart', 30 );
}
add_action( 'after_setup_theme', 'change_order' );
The add_action gets excuted but the remove_action() isn't working...
I tried the same with 'init' instead of 'after_setup_theme' but it is still not working...
Does anybody know a solution for my problem ?
Use a different single product hook
add_action('woocommerce_single_product_summary','change_order' );
function change_order() {
add_action('woocommerce_product_meta_end','woocommerce_template_single_add_to_cart', 15 );
remove_action('woocommerce_single_product_summary','woocommerce_template_single_add_to_cart', 30 );
}
Works and tested.

Hiding breadcrumbs on cart page in woocommerce with storefront theme

I'm trying to hide breadcrumbs with a child theme of storefront. This is the code I have in functions.php however, the conditional does not fire when on the cart page. Removing the conditional causes the breadcrumbs to be hidden
add_action('init', 'remove_shop_breadcrumbs' );
function remove_shop_breadcrumbs()
{
if ( is_cart())
{
remove_action( 'storefront_content_top', 'woocommerce_breadcrumb', 10 );
}
}
From everything I can read, this is correct, does storefront replace this conditional with its own code, hence causing this to fail?
You're code is firing too early. The init hook is triggered before the queries have been run therefore is_cart() won't work. Use the wp action instead.
Change this:
add_action('init', 'remove_shop_breadcrumbs' );
To this:
add_action( 'wp', 'remove_shop_breadcrumbs' );

Categories