Why is it necessary to wp_deregister_script() [some] scripts
-
This is more of a theoretical question than a real use case, but important for me to understand how it works as related to a project I am working on, where dequeuing scripts is a core feature.
So I have been experimenting with
wp_deregister_script('')
method, andwp_dequeue_script()
.Trying this on several scripts I found a weird behaviour.
When I
wp_register_script('handle', $src, 'jquery', $ver, false)
my own script (lets say Select2, or any custom JS), and then laterwp_enqueue_scripts('handle')
it (once I need it), I can easily remove that script again bywp_dequeue_script('handle')
. No matter if this happens in wp_head, wp_footer or wp_print_scripts.Now, I wanted (really, this is only for testing and learning purposes) to dequeue/remove these scripts:
jquery-migrate-js
,jquery-core-js
But I can’t!
No matter when (since they are appearing in head, I first hooked towp_head
and then started playing with priority up to PHP_INT_MAX), the 2 scripts will keep being enqueued into the head.Only if I
wp_deregister_script( 'jquery' );
, then (without further ado) both scripts are removed. But I do not want to remove jQuery. I want to only dequeue the 2 aforementioned scripts.Can someone shed some light in this poor mind not understanding why I can easily and effectively dequeue a script previously registered and enqueued in one case, but not in the other case (the other case here being jQuery, but probably there are more such cases)
I can see several online postings about the issue, however apart of telling us “you have to deregister jQuery” they do not explain why we need to do this, and why we can simply dequeue in one case, but need to deregister a different script to remove it.
Both
jquery-migrate-js
,jquery-core-js
are valid script handles, I can see them in the $wp_scripts global, so I am really confused why dequeuing them does not dequeue them, but only deregistering their $deps (jQuery) does the job.To clarify: I am aware that dequeuing certain scripts is a bad idea, but I still want to know why I am seeing the issue I describe above.
- The topic ‘Why is it necessary to wp_deregister_script() [some] scripts’ is closed to new replies.