0

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?

Khufu
  • 55
  • 7
  • 1
    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.”_ – 04FS Oct 07 '20 at 11:58
  • @04FS Which I have done above? – Khufu Oct 07 '20 at 12:00
  • 2
    No, you have not. The priority given when this was added was `25`, you did not specify any in your remove_action call, so it uses the default of `10` there. – 04FS Oct 07 '20 at 12:01
  • @04FS very easy fix, thank you very much! – Khufu Oct 07 '20 at 12:08

1 Answers1

3

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 );
04FS
  • 5,660
  • 2
  • 10
  • 21