• Resolved Benny

    (@wordbob)


    I am using some external javascript files that I reference in the header.php file with something like this (with a valid IP address used):

    <script type=”text/javascript” src=”https://999.999.999.999/myscripts/concode.js”></script&gt;

    I know I can change them to us the domain name instead of the IP (better),but that also seems a little shortsighted. What’s the best way to create a dynamic site reference that pull the site location from the Permalinks for these?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator James Huff

    (@macmanx)

    Are the scripts on your server, or are they elsewhere?

    Thread Starter Benny

    (@wordbob)

    Whoops… should have included that info.

    Yes, they are on my server. Currently they are in a folder named “myscripts” under the root web (e.g. public_html) but they could move into the WordPress hierarchy if that simplifies things.

    Moderator James Huff

    (@macmanx)

    Ok, personally I like to store any JS included with the theme in a /js/ directory under the theme, because it might as well be with the theme if the theme is including it, and that’s what all of the WordPress default themes have done the past few years.

    So, that would be this:

    <script src="<?php echo esc_url( get_template_directory_uri() ); ?>/js/concode.js"></script>

    In the above, it’s fetching the directory for the currently active theme (which means it’s independent of whichever domain you’re using, and independent of HTTP vs. HTTPS) then pulling concode.js out of the /js/ directory in the theme’s directory, where the js is located at:

    /wp-content/themes/[current-theme]/js/concode.js

    Thread Starter Benny

    (@wordbob)

    Thanks, that will do it!

    Moderator James Huff

    (@macmanx)

    You’re welcome!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Referencing Javascript code location in header.php properly’ is closed to new replies.