from my header file i can create template path easily for any .js file:
<?php $templateDirPath = get_bloginfo( 'template_directory' ) . '/'; ?>
<script type="text/javascript" src="<?php echo $templateDirPath; ?>js/scripts.js"></script>
but inside the ‘scripts.js’ file has some include .js files (same directory) like below:
include(‘js/mathUtils.js’);
include(‘js/superfish.js’);
include(‘js/switcher.js’);
include(‘js/jquery.mousewhe
i just want to know is there any way to make template path for those included .js files inside ‘scripts.js’ file ? im newbie in wordpress.
found idea for below code but dnt know how to implement:
<script type="text/javascript">
var templateUrl = '<?= get_bloginfo( 'template_directory' ) . '/'; ?>';
</script>
Use
wp_enqueue_script()
, hooked in via an appropriate action hook callback.For example:
You need to do a bit of research on the proper way to enqueue JS within WordPress. Your approach is totally outside the defined method.
Take a look at the WordPress codex for the wp_enqueue_script call.
The solution you found is almost correct but that ASP-like
<?=
may not be terribly reliable, especially pre-PHP.5.4.0.That will shove your script into the footer of your front-end page assuming your theme uses the
wp_footer
function. You can put that in yourfunctions.php
or in a plugin.You could use
wp_enqueue_script
but as far as I am aware that requires you to load a file, which is overkill for this, and not just a string. If someone can demonstrate how to load a string withwp_enqueue_script
and avoid the overhead of loading a whole file I would love to be proven wrong on this.There are other ways to do this as well, like
wp_localize_script
( http://pippinsplugins.com/use-wp_localize_script-it-is-awesome/ ), but that requires enqueue-ing which I consider overkill here.Edit: If you are already enqueue-ing a script, any script, you can leverage that to print your variable.