[wp-hackers] howto including full path in javascript file.

Otto otto at ottodestruct.com
Mon Feb 8 19:36:08 UTC 2010


On Mon, Feb 8, 2010 at 11:45 AM, Bjorn Wijers <burobjorn at gmail.com> wrote:
> I was wondering what other people use to include full paths in their
> theme javascript files and keep it dynamic (so no hardcoded paths!). For
> instance I would like to call some images in my javascript file and I
> need to use a full path. What would be the best practice for this?

Best way in a plugin:

add_action('wp_print_scripts','myscript');
function myscript() {
?>
<script type="text/javascript">
   var my_plugin_url = <?php echo plugin_dir_url(__FILE__); ?>;
</script>
<?php
}

This will get put into the page before your enqueued scripts, so the
variable will be defined and available for them to use.

You can use this same basic approach to pass along other settings to
javascripts as well. In fact, the admin side of things does it for
you. Look at the source of an admin page, and notice the handy
userSettings array, as well as the ajaxurl variable.


-Otto
Sent from Memphis, TN, United States


More information about the wp-hackers mailing list