1

I'm, passing the plugin url parameter to my .js file using:

wp_enqueue_script('nova-payflow-plugin');
wp_localize_script('my-payflow-plugin', 'pluginValues', array(
    'pluginUrl' => plugins_url(__DIR__),
));

In my script, I retrieve what should be a url to my plugin folder, but it looks like this:

http://mysubdomain.mydomain.local/wp-content/plugins/C:/xampp/htdocs/mywebfolder/wp-content/plugins/my-plugin

On the Linux production server it does a similar thing:

https://mysubdomain.mydomain.online/wp-content/plugins/homepages/40/d107997204/htdocs/mywebfolder/wp-content/plugins/my-payflow

Note that in the plugins url, a fully qualified Windows path appears appended immediately after wp-content/plugins/

Why does plugins_url add the absolute file path, but more importantly, I don't want it appended to the url string.

Here is how I'm enqueuing the scripts, if it matters to the question:

add_action('wp_enqueue_scripts', 'npf_styles_and_scripts');
function npf_styles_and_scripts() {
    wp_register_style('nova-payflow-plugin', plugins_url('css/npf.css',__FILE__ ));
    wp_enqueue_style('nova-payflow-plugin');
    wp_register_script( 'nova-payflow-plugin', plugins_url('js/npf.js',__FILE__ ));
    wp_enqueue_script('nova-payflow-plugin');
    wp_localize_script('nova-payflow-plugin', 'pluginValues', array(
    'pluginUrl' => plugins_url(),
    ));
}
8
  • you don't use the function conveniently, read the documentation : codex.wordpress.org/Function_Reference/plugins_url
    – Kaperto
    Commented Dec 19, 2019 at 22:41
  • Can you share with us how you are registering the my-payflow-plugin script? I can only see the enquing
    – Tom J Nowell
    Commented Dec 19, 2019 at 23:02
  • 1
    thanks, how are you testing the value of pluginUrl? I hope you're not making AJAX calls to a PHP file in that folder, major security issues if you are, amongst other reasons, there are far, far better ways to do AJAX in WP that are safer faster and easier.
    – Tom J Nowell
    Commented Dec 20, 2019 at 1:47
  • 1
    😱 that's extreme bad practice, is there a reason you aren't making your AJAX calls to a REST endpoint using the built in WP functions? You could sidestep all of this and make everything simpler/easier/faster
    – Tom J Nowell
    Commented Dec 20, 2019 at 15:14
  • 1
    I take it these are images etc that are referenced via relative URLs in javascript?
    – Tom J Nowell
    Commented Dec 20, 2019 at 20:44

1 Answer 1

0

Firstly. it doesn't look like you're using the $src for your wp_enqueue_script Then from the Codex I found the following similar code:

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

Perhaps try the following:

wp_enqueue_script('nova-payflow-plugin', get_stylesheet_directory_uri() . '/js/my-script.js');
wp_localize_script('nova-payflow-plugin', 'pluginValues', array(
    'pluginUrl' => plugins_url(),
));

But change the $src value above which is:

/js/my-script.js

2
  • 1
    You don't have to specify anything other than the first parameter when enquing if you've already registered it. Those additional parameters are there so you can register and enqueue at the same time, e.g. to enqueue jQuery you do wp_enqueue_script( 'jquery' );
    – Tom J Nowell
    Commented Dec 19, 2019 at 23:01
  • 1
    I don't know if I misstated the problem. The problem is not enqueuing the script. My script works. My problem is that plugins_url returns a "url", and appended to the url is a file path: http://mysubdomain.mydomain.local/wp-content/plugins/C:/xampp/htdocs/mywebfolder/wp-content/plugins/my-plugin
    – TARKUS
    Commented Dec 19, 2019 at 23:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.