Skip to content Skip to sidebar Skip to footer

Wordpress - How Do I Take The Theme Directory In A Js File?

I have an external script to integrate in a Wordpress theme. This script has different mixed resources in it like js, php files, etc. It's something like: /mytheme/myscript/

Solution 1:

If your folder/main.js is being included via the wp_enqueue_script() function ( which it probably should be, as this is the proper method with WP to load scripts ), then you can easily add the theme directory uri as a variable using the `localize_script()' function like so:

wp_localize_script( 'script_handle', 'themeDirURI', get_template_directory_uri() ) ;

You would put this code after the wp_enqueue for the main.js script. This will make the theme directory available as a javascript variable named themeDirURI, and the value will be something like: `http://www.example.com/wp-content/themes/theme-name/'

Hope this helps!

Post a Comment for "Wordpress - How Do I Take The Theme Directory In A Js File?"