Wordpress: How can I select the plugins directory in a javascript file?

I need to access the plugins directory in the client javascript file. I know that we have functions to get the path to the plugin directories using the php plugins_url() function.

However, I need to get this in my js file, where I need to put some images.

Any ideas?

PS: My js file is saved as a javascript file, and therefore I can not use php tags in it

+4
source share
2 answers

Use <?php echo plugins_url(); ?> <?php echo plugins_url(); ?> where you want to get the url in js file.

For instance:

 var pluginUrl = '<?php echo plugins_url(); ?>' ; 
+5
source

Actually it's pretty simple ... In functions.php

wp_enqueue_script ('main', get_template_directory_uri (). '/js/main.js', array ('jquery'), '20151215', true);

$ translation_array = array ('templateUrl' => get_stylesheet_directory_uri ()); // after wp_enqueue_script

wp_localize_script ('main', 'jsVars', $ translation_array);

then in the js file:

 var sitepath=jsVars.templateUrl+"/"; 
0
source

Source: https://habr.com/ru/post/1434225/


All Articles