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

logikal16 logikal16 at gmail.com
Mon Feb 8 19:56:29 UTC 2010


<script type="text/javascript">
var mypath = "<?php echo WP_CONTENT_URL; ?>/img/somefile.gif";
// more javascript code
// you could also use the WP_PLUGIN_URL constant
</script>

I keep as much stuff in "wp-content" as possible (instead of the site
root). This isn't for everything (robots.txt, sitemap.xml.gz), but it
keeps all of the custom files together, making any updates/migrations
a bit simpler.

Matt

On Feb 8, 2:36 pm, Otto <o... at ottodestruct.com> wrote:
> On Mon, Feb 8, 2010 at 11:45 AM, Bjorn Wijers <burobj... 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
> _______________________________________________
> wp-hackers mailing list
> wp-hack... at lists.automattic.comhttp://lists.automattic.com/mailman/listinfo/wp-hackers


More information about the wp-hackers mailing list