0

There is an Edit at the end of this question.

I want to to include a file, in a script tag, from the js sub directory of the theme's directory. I want to do so in the theme's index, search, single, archive, etc. php files.

The URL of the theme I'm using is: http://www.example.com/welg/wp-content/themes/mytheme

The script tag would be:

<script type="text/javascript" src="/welg/wp-content/themes/themename/js/filename.js">

I tried the get_theme_root() function but I get nonsense. Well, at least nonsense in terms of a directory usable for my purposes.

What is be returned is a path that starts pretty much at the top of the hosting server's directory structure.

/hermes/bosoraweb081/b1107/myd.sugarcatsimon/public_html/pdmeoff/pdroot/welg/wp-content/themes 

That output is useless for any purpose except, perhaps direct access, via the operating system, on the server itself by someone at the server's console.

The return value I expected from get_theme_root() was:

/welg/wp-content/themes/themedir (with or without a trailing slash)

I tried all the "get" theme functions I could find and the site_url and home_url

Here's the ouput from all of them (I added h's to the front of the http:// text to get past my limit of only being able to post two links. Just ignore the extra h's.

Here's the output from the various functions:

get_theme_root_uri()-- http://www.example.com/welg/wp-content/themes

get_theme_root() -- /hermes/bosoraweb081/b1107/myd.sugarcatsimon/public_html/pdmeoff/pdroot/welg/wp-content/themes

get_theme_roots() -- /themes

site_url -- http://www.example.com/welg

home_url http://www.example.com/welg

Am I missing something? Is there another function which will return the path, relative to the site root or WP root, for the currently used theme's directory?

I guess I could use the output from get_them_url(), make my link a fully qualified link, not a relative link, and suffix the name of the theme and the /js/filename.js but I'd like a better solution which does not rely on knowing any of the directory names, such as the current theme's name/directory.

One other thing - I habitually use www.example.com as the domain in examples I post online, and most sites tell you to use it, but I had never really thought it through.

I just realized that there my be an actual site with that domain name. So, I entered www.example.com into the browser address bar and I got - well, try it yourself. It is very interesting.

EDIT

In response to the person who wrote the answer saying that I should enqueue and register scripts -

What does it mean in WP Land when the words enqueue and register are used?

What are the benefits, as regards WP, of enqueuing and registering functions as you show it being done?

Just because those who wrote WP do something in a particular way does not mean it can only be done that way or it is the best way.

I would really like to know the principles of enqueuing and registering functions as apply to WP.

I am new to WP but most certainly new to computing. I've been in computing since 1973, some 42+ years.

In the PC world, it is far too often the case that words which have been in use for decades, with specific agreed upon meanings, are used for new meanings.

There is also a propensity for the invention of new terms for meanings that are already covered by existing terminology.

Quite often, what an "old timer" calls a rose is actually not a rose, in the PC world. When terms with established meanings are used in computing (or any technical area) for other purposes, communication suffers.

Poor communications breeds poor design and code.

Thanks for the reply and I really would like to understand those words in relation to WP.

0

Use get_stylesheet_directory_uri or get_template_directory_uri to return the URI of the theme directory. The former will return the child theme directory, in the event a child theme is being used. The latter will always be the main or parent theme in the case where a child theme is active.

Also, note that "path" typically refers to what you get back from get_theme_root(). It's the location local to the server, and is not the same thing as a URI / URL.

  • Thanks - that's it. I may be putting my foot in it, but being a computer professional of 42+ years, I find WP to be, well, well -- not written to the standards I would expect. Just one example: there are two functions which creates links to previous and next pages or posts . The names are the same except for one letter - they are the_post_navigation() and the_posts_navigation() - ONE freaking letter different! I can't find polite words to express my astonishment that someone would do that. On another matter -- Is it possible to put a blank line in a comment post on this forum? Again, thanks. – SimonT Nov 11 '15 at 5:24
  • Something else - when I see "path" I think in terms of the parts of a URL - protocol, host, pathname, query, etc. As for root", I think of the root directory accessible via FTP on the server handling the web site. I can't imagine why anyone would have a need returned by get_theme_root() Again, thanks. – SimonT Nov 11 '15 at 5:25
  • Yeah, there's plenty of downright awful stuff in the codebase. It evolved from something quite small without much of a plan early on. One of the primary goals has been to minimize breaking of themes and plugins with core updates, so we're left with a lot of cruft that'll take a while to get rid of. I'm not sure if you can add blank lines in comments. It seems to strip out soft returns, though interestingly it maintains them and can be seen when I edit the comment! – Milo Nov 11 '15 at 5:38
0

You should always enqueue or register your scripts and styles , not call them directly. Look in codex for more info https://codex.wordpress.org/Function_Reference/wp_enqueue_script

Now to answer your question, you have a theme called themename which has a js directory containing the file filename.js. Add the following code in your functions.php

function theme_js(){

wp_enqueue_script('theme_js',get_template_directory_uri() . '/js/filename.js', array(), '', true);}

add_action( 'wp_enqueue_scripts', 'theme_js' );

Inside the empty array you can put 'jquery' or any other dependencies your .js file may have. It is also a better practice to load all js files in footer area. Instead of true you can set the last parameter to false and then the file will be loaded in the header.

If putting this code in your functions.php dose not work also remeber to put

<?php wp_footer(); ?>

in your footer.php before the closing body tag.

Hope this helps.

  • Why should one enqueue or register scripts and styles rather than call them directly. What does WP mean by enqueue and resigering. – SimonT Nov 11 '15 at 20:39
  • That previous comment is incomplete, I forgot and pressed enter to put in some white space and then when I edited the full comment to fit in the allowed space, I took a telephone call and when I submitted the edited comment I was told that comments can only be edited for 5 minutes. My full comment will not fit here so I'm going to put as an "Edit" of my question please look in a few minutes, it is currently 1457 CST. – SimonT Nov 11 '15 at 20:58
  • By including your JavaScript like that, you run the risk of having conflicts with your JavaScript across your installation (think, multiple plugins trying to load the same scripts, or different versions of that script). Furthermore, your JavaScript will load on every page, even if you don't need it to. This will cause an unnecessarily longer load time for you pages, and can interfere with other JavaScript, like from a plugin or within the dashboard... and that's a No No in WP development. – AndyWanna_KnowItAll Nov 14 '15 at 6:20
  • My site is mine and thus I know when there might be conflicts I have been doing a lot digging into WP and I pretty much now know about possible conflicts.I don't intend to use any plugins but Akismet. As to longer load time - are you serious - you can't get much slower than WP is already. I've been in computing for 42+ years, and understand far more than you even suspect. There is nothing that you said that I haven't considered. WP is not some super package. In fact, as I dig deeper, I find it poorly written and poorly documented. I do thank you, however, for taking the time to reply to me. – SimonT Nov 15 '15 at 7:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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