How to call plugin path in JS?

Use wp_localize_script() to pass any kind of data to your loaded scripts, in this case we need plugins_url():

wp_enqueue_script('my-script', get_stylesheet_directory_uri() . '/js/my-script.js');
wp_localize_script('my-script', 'myScript', array(
    'pluginsUrl' => plugins_url(),
));

Now you will have access to myScript.pluginsUrl in your script file:

var href = myScript.pluginsUrl + '/path/to/resource';

Leave a Comment

error code: 523