Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Can you please let me know how I can dynamically register/link to my js files in wordpress? I have two js files as below code but I do not know how to map to them uring the bloginfo() as it is taking parameters!

 <!--[if lt IE 9]>
      <script src="assets/js/html5shiv.js"></script>
      <script src="assets/js/respond.min.js"></script>
 <![endif]-->

Thanks

share|improve this question
    
Is your assets folder inside your custom theme or a plugin? –  webaware Oct 30 '13 at 1:30
add comment

3 Answers

We can use wp_enqueue_scripts action to hook js.

If it is inside theme folder then,

<?php

    function my_scripts_method() {
wp_enqueue_script(
    'custom-script',
    get_stylesheet_directory_uri() . '/js/custom_script.js',
    array( 'jquery' )
);
}

add_action( 'wp_enqueue_scripts', 'my_scripts_method' );

?>
// If it's for plugin,If it's for plugin, 
//use plugins_url( '/js/newscript.js' , __FILE__ )  as path.

For ref wp_enqueue_script

share|improve this answer
add comment

I´m using this code script src="?php bloginfo('template_directory'); ?>/html5/html5shiv-printshiv.js">
you only need to add the line bloginfo('template_directory') to your code to header.php
script src="?php bloginfo('template_directory'); ?>/assets/js/html5shiv.js">
I had to avoid some "<" in order to place the code.

As the code describes template directory all your JS files should be on the folders /assets/js/ within the folder template.

share|improve this answer
add comment

If you are going to use your custom js code then what you have to do is first register your custom script then enqueue it.

Example

function link_my_js()
{
 wp_register_script('desired_name',_template_directory().'/assets/js/custom-script.js');
 //now enqueue it
 wp_enqueue_script('desired_name');
//desired_name(called handle) is what you have given in wp_register_script above
}

now add this in a action hook

add_action('wp_enqueue_scripts','link_my_js');

Place the codes in functions.php

If you want to use this in plugin give correct path on registering the script use plugins_url() to get the directory path then continue with yours and use it inside the plugin.

If you want to link general main jquery just use the handle name jquery alone you don't have to register it, because WordPress provides preloaded scripts in the core you can check it Here Default Scripts Included and Registered by WordPress at the bottom. Feel free to ask doubts on this:)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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