0

I am trying to write a plugin and in this plugin, it needs to know its current position in the whole file system. I tried the code like below:

var Path = location.hostName + '/wp-content/plugins/myplugin/';

but when I use this plugin in a post named 'test', the Path turns out to be:

http://www.mywebsite.com/blog/test/undefined/wp-content/plugins/myplugin/

How can I correct this and make it like:

http://www.mywebsite.com/wp-content/plugins/myplugin/

I am not sure if I have put it clear. Please help...thank you very much.

Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78
John Mccandles
  • 1,181
  • 2
  • 16
  • 24

1 Answers1

0

You're looking for the plugin_dir_url() function:

<?php echo plugin_dir_url( __FILE__ ); ?>

Would echo:

http://www.mywebsite.com/wp-content/plugins/myplugin/
rnevius
  • 26,578
  • 10
  • 58
  • 86
  • thanks a lot! Sorry that I have another question. Is there a way to pass this value to Javascript? – John Mccandles May 04 '15 at 12:52
  • I just found this which might help: http://www.webtipblog.com/localize-scripts-in-wordpress-to-use-php-variables-in-javascript/ ... thank @rnevius for your help! – John Mccandles May 04 '15 at 12:59
  • Where is the JavaScript? Is it in a JS file, or directly in your PHP template?? – rnevius May 04 '15 at 13:01
  • Update your question with your code for enqueue script. – Rohil_PHPBeginner May 04 '15 at 13:04
  • [This may help you](http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript)...although I'm really curious about your use case here...It sounds odd to need this in JS. – rnevius May 04 '15 at 13:04