Generally we use the wp-equeue-script to add scripts to the page inside the header or the body section, but how about if I want to insert a script file at a certain location (like at the middle of the page)?

Furthermore, is it possible to generate dynamic scripts?

For example http://xx.com/xx.js?query=xxx which will return different scripts based no the query?


To answer your first question, you can always add a <script> tag inside your template files wherever you like, would that solve it? Or alternatively add a function <?php add_my_script(); ?> somewhere in your template files which you put in functions.php and does some more checks to determine if you want a script there on that location and add it.

However, do you really need it at a certain location in your page? It should not make a difference where you put the script, because you can wrap your functionality inside a function, and only run it when you call the function.

To answer your second question, yes you can, it's been answered before. https://stackoverflow.com/questions/3943198/parse-a-javascript-file-through-php

So don't call a .js file, call a .php file that renders your script. just make sure the to use a header("Content-Type: application/javascript"); and whatever logic you want there

  • Why I need to insert the script at the middle of the page is that I need to render the some contain as soon as possible (do not want to render after page ready). And for the second question, I have though that, but which php file can be used? Use plugin or something else? – hguser Oct 19 '16 at 23:38
  • You could add a custom page template (developer.wordpress.org/themes/template-files-section/…) in your theme, add a page with that template. – morksinaanab Oct 20 '16 at 0:16

When you use wp_enqueue_script(), you are enqueueing your file in the header or the footer, depending on the fifth parameter passed in the function.

A clean way to bring variables into a js file depending on your page's query : wp_localize_script() to bring localization or special request available into a js file.

 // Register the script
 wp_register_script( 'some_handle', 'path/to/myscript.js' );

// Localize the script with new data
$translation_array = array(
   'some_string' => __( 'Some string to translate', 'plugin-domain' ),
   'a_value' => '10'
wp_localize_script( 'some_handle', 'object_name', $translation_array );

// Enqueued script with localized data.
wp_enqueue_script( 'some_handle' );

in the js script, you'll be able to retrieve the variables

  // alerts 'Some string to translate'
  alert( object_name.some_string);

Read more on the codex wp_localize_script

If you want to load different js file depending on the query, you have to add php statement into the function call with wp_enqueue_scripts action (beware of the ending S).

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.