0

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.

4
  • Are you certain you're dequeueing the correct script? Do you see the original script in the network tab? Oct 1, 2021 at 7:01
  • I do see the original script in the Network tab. (Should you still see it there if it's successfully dequeued? That I don't know.)
    – supernaut
    Oct 4, 2021 at 22:04
  • If it had been successfully dequeued it would not be there. Your issue is likely that you are using the wrong handle to dequeue it, or you are dequeueing it before it was originally enqueued. What hook and priority is the original script enqueued in? Oct 5, 2021 at 3:23
  • Other than looking at the original wp_enqueue_script above, how can I determine that? I'm using the same handle, and there doesn't seem to be a priority assigned.
    – supernaut
    Oct 6, 2021 at 11:47

1 Answer 1

1

Please try below code -

function fixUlisting() {
    wp_deregister_script( 'stm-search-form-category' );
    wp_dequeue_script( 'stm-search-form-category' );
    
    wp_enqueue_script( 'stm-search-fix', plugins_url().'/ulisting-search-fix/stm-search-fix.js' );
}
add_action( 'wp_enqueue_scripts', 'fixUlisting', 100 );
1
  • Thank you for taking a look. I should have mentioned that I also tried changing the order of dequeue/deregister -- not sure which is correct. I just tried this again, because it's probably just some combination of the variables above that I've missed. Unfortunately, this isn't working for me. Any other ideas?
    – supernaut
    Oct 1, 2021 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.