Wordpress - How do I take the theme directory in a

2019-09-10 14:05发布

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/
                  ...main.php
                  ...folder/main.js

Now, main.js post to main.php and uses something like:

var comet = new vpb_start_comet('main.php');
comet.connect();

My problem's here. Main.php is not found because I should change it with the relative wordpress path, being in a subfolder of my theme. So how could I get_bloginfo('template_url').'/myfolder/main.php in my main.js file?

1条回答
叼着烟拽天下
2楼-- · 2019-09-10 14:25

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!

查看更多
登录 后发表回答