I need to access plugins directory in my customer javascript file. I know we have do have functions to retrieve plugins directory path using php function plugins_url()
.
However, I need this to retrieve in my js file where I have to put some images.
Any ideas??
PS: My js file is saved as a javascript file and therefore, I can’t use php tags in it
Use
<?php echo plugins_url(); ?>
where you want to get the url in the js file.For example:
It’s actually quite easy…
In functions.php
then in the js-file:
Edit: Just in case it’s not understood completely:
The translation array is created and contains a key value pair
The array is then injected as jsVars (javascript variable) into the js scripts
So basically we (mis)use the translation array to inject variables to JS
with the wp_localize_script command.
off course instead of the templateUrl you can define all the params you want with PHP to inject their values into any js file you want.
In this example we inject the template Url into the main js script via jsVars, but it can be any variable into any js script
I’ve got the result just by assigning the real plugin url: