[resolved] Plugin Javascript Trying to Reference Plugins Directory (4 posts)

  1. Adam Capriola
    Posted 4 years ago #

    I recently created my first official plugin (yay!) and realized I had a URL reference in my plugin's JS file that might not work for certain sites:

    <img src="/wp-content/plugins/my-plugin/images/ellipsis.gif">

    I have kind of an AJAX thing going on in the Javascript, and that URL is for a loader image. I call the JS via wp_enqueue_script. I understand that the URL will work for most people, but if their WordPress installation isn't at the base of their domain, then it will be a broken link.

    I know I could move the JS into one of my plugin's PHP files and then use plugin_dir_url( __FILE__ ) or plugins_url() to grab the plugin URL and substitute that for part of the link, but I'd like to keep the Javascript as a separate file so I can utilize wp_enqueue_script.

    Is there a way around this?

  2. ysr5
    Posted 4 years ago #

    what about generating the src with the baseurl?

    // should return the base url
    $baseUrl = get_bloginfo('url');
    $fullPath = $baseUrl."/wp-content/plugins/my-plugin...";

    (code not tested)


  3. Adam Capriola
    Posted 4 years ago #

  4. ysr5
    Posted 4 years ago #

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.