0

I am trying to enqueue an edited js file from my child theme and dequeue the original one from the parent. It should be simple, however, the parent theme is calling a function where all enqueues are made. I managed to enqueue it but not to dequeue. In addition, the original enqueue is followed by wp_localize_script() function.

If I copy the whole function to my child it works, but I am looking for a cleaner and better way to achieve that.

Here is how the original code is set up (Parent Theme):

In function.php this function is called

        add_action('wp_enqueue_scripts', 'wpestate_scripts'); 

The wpestate_scripts function is found in another file, css_js_include.php

function wpestate_scripts() { 

// A bunch of files being enqueued and some variables being assigned

 wp_enqueue_script('wpestate_property', trailingslashit( get_stylesheet_directory_uri() ).'js/property.js',array('jquery','wpestate_control'), '1.0', true);   
        wp_localize_script('wpestate_property', 'property_vars', 
            array( 
                // Variables being localized
              )
        );

 }

I have already added wp_dequeue_script('wpestate_property') and wp_deregister_script('wpestate_property') to my child function.php. And it did not work.

Any help is appreciated.

stemon
  • 599
  • 1
  • 5
  • 23

1 Answers1

2

You need to make sure the function you're calling is fired after the script is enqueued by the parent. Typically this is done by adding a high integer value to the $priority argument for add_action().

add_action( 'wp_enqueue_scripts', 'modify_wpestate_scripts', 99 );
function modify_wpestate_scripts() {
    wp_dequeue_script('wpestate_property');

    // Enqueue your custom script instead
    wp_enqueue_script( 'custom-wpestate_property', 'custom-wpep.js', [], '1.0', true );
    wp_localize_script('custom-wpestate_property', 'property_vars', 
        array( 
            // Variables being localized
          )
    );
}

This adds it to the same action hook (wp_enqueue_scripts) as the parent function, but the priorty is set to 99 so it runs later (the default priority is 10).

Xhynk
  • 13,513
  • 8
  • 32
  • 69
  • Great @Xhynk. How should I deal with the `wp_localize_script()` fucntion and vars? – stemon Aug 30 '19 at 21:01
  • I've updated the code - you can just enqueue your custom script under a custom handle and localize it – Xhynk Aug 30 '19 at 21:18
  • Great. I was expecting a way to avoid the copy and paste of `wp_localize_script()`. Is there a way? Thanks. – stemon Aug 30 '19 at 21:20
  • 1
    I don't think there's a way to "copy" programatically. Unless the parent theme stored the localization as a variable that you could access (it doesn't look it like, based on your example). Unfortunately since they're enqueued and localized at the same priorty there's not a way to intercept that localization either. You could _try_ changing the handle on your custom script to `wpestate_property`, but I don't think it will work – Xhynk Aug 30 '19 at 22:05