Dequeue / Deregister script and replace it with a new plugin


I think I’ve read a dozen posts about dequeuing and deregistering scripts, but I haven’t been able to make it work properly in my situation.

My situation:
The “uListing” plugins are part of the “HomePress” real estate theme. There’s an issue with the AJAX search where, if you type in only a particular single digit and then pause, it freezes the whole page. I believe it depends on the results that are found by that single digit (the offending digits are different on different sites), but I was able to replicate the problem on the provider’s own demo site with 5 different digits. The problem is due to a loop that never completes, due to the results found. We can escape that loop with a simple return, and it works as desired.

But now I’m trying to replace their script with my own, inside another plugin. (So that it doesn’t get overwritten by theme or plugin updates.)

Their plugin loads the script like so:

wp_enqueue_script('stm-search-form-category', ULISTING_URL . '/assets/js/frontend/stm-search-form-category.js', array('vue'), ULISTING_VERSION, true);

My plugin attempts to override it this way:

function fixUlisting() {
    wp_dequeue_script( 'stm-search-form-category' );
    wp_deregister_script( 'stm-search-form-category' );
    wp_enqueue_script( 'stm-search-fix', '/wp-content/plugins/ulisting-search-fix/stm-search-fix.js' );
add_action( 'wp_enqueue_scripts', 'fixUlisting', 100 );

I believe these steps are in the right order(?). I believe I’m using priority (100) and location (true) correctly(?). I can see that my new script is loaded in the Network tab. I have tried including the original script’s dependency (vue). I have tried re-using the original handle, but resorted to using a different handle based on posts I read (neither works). I clear the server cache, use different version numbers (or blank, or null) including the ‘parent’ plugin’s original version number, then re-test in a fresh incognito browser window every time. Despite all of that, the original script is still controlling the AJAX search results in every instance.

What do I need to do to ensure that my script is used instead of the original? Any help would be greatly appreciated.

supernaut 10 months 2021-09-30T20:09:34-05:00 0 Answers 0 views 0

Leave an answer