skip to Main Content

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


  1. 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:

    wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array('jquery'), '1.0.0', true );
    

    To read more about dependencies WordPress offers, here is the documentation:

    https://developer.wordpress.org/reference/functions/wp_enqueue_script/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search