1

Before anyone says this should be on the Wordpress Stackexchange, please read through as this is a JS query and not a Wordpress query. I am trying to get a path to the Wordpress theme in to a JS file so I don't have to include the full path, would usually use <?php bloginfo('template_url'); ?> in the template files but obviously we are working in a JS file here.

I have found a forum post that says to use the following before the JS file is called for:

<script type="text/javascript"><!--
var template_url = "<?php bloginfo('template_url'); ?>";
// -->
</script>

and then in the JS file put template_url but this just puts 'template_url' in the middle of the path.

The line this is to go in is:

$(this).prev().css({ backgroundImage : 'url(template_url/images/icons/menu-divider.png)' });

How can I please get the full path to be called in this line of JS?

Many thanks Adrian

18
  • @Quentin how is that a duplicate? The question title may be similar but the actual question is not. Commented Oct 9, 2015 at 13:49
  • Yes, totally different type of question?! Commented Oct 9, 2015 at 13:50
  • @Halcyon — The question is asking how to use the variable template_url in a string, and the attempt looks like: 'url(template_url/images/icons/menu-divider.png)' Commented Oct 9, 2015 at 13:50
  • I am asking how I am meant to put the variable 'template_url' in to the file to make it get the actual template URL as I don't know Javascript. Commented Oct 9, 2015 at 13:51
  • @Adrian, if you mean in the js file the answer is you can't. You can't use PHP in an external js file. Commented Oct 9, 2015 at 13:52

1 Answer 1

-1

Using this function returns what you're looking for get_template_directory_uri()

So, do this

<script type="text/javascript"><!--
var template_url = "<?php echo get_template_directory_uri(); ?>";
// -->
</script>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.