I’m trying to write a theme function for my WordPress website.
Basically I would like to include the jQuery library if the the URL has ?download
Because it’s the only page where jQuery script exist.. So I’m aiming to remove the library on the header for other pages (making the website loads faster is the ultimate goal here).
URL: /windows/post-name/?download
Is the following code correct?
{
if (isset($_GET['download'])) {
wp_enqueue_script( 'jquery-lib', get_template_directory_uri() . '/js/jquery.min.js', array(), false, false );
}
2
Answers
WordPress already comes bundled with jQuery. When you have a script that needs jQuery as a dependency, you pass it into
wp_enqueue_script()
.Here is an example of enqueing a script that depends on jQuery:
To read more about dependencies WordPress offers, here is the documentation:
https://developer.wordpress.org/reference/functions/wp_enqueue_script/
If you are sure that no need for jQuery on other pages you can use hook to disable jquery except ?download pages by hook ( for example in functions.php theme file ):