Support » Developing with WordPress » Best way to get a plugin url

  • this seems kind of silly, but i couldn’t find a satisfactory solution.

    from whereever subdirectory the current class is, i want to get the base-directory of the plugin. this:

    right now i do this, and it works:
    $this->plugin_baseurl=plugins_url().”/”.explode(“/”, plugin_basename( __file__ ))[0];

    but it seems that there should be a more straight forward way to do this?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Perhaps the other args for plugins_url?

    You can also do dirname( plugin_basename( __FILE__ ) ) instead of the explode.



    Perhaps you can add something like the following as the first line in the main plugin script:

    define('MY_PLUGIN_URL', WP_PLUGIN_URL . '/' . substr(strstr(__DIR__, DIRECTORY_SEPARATOR), 1)));

    You could then use MY_PLUGIN_URL whenever you needed the URL to your base plugin directory. (Not tested!)

    plugins_url(), maybe, but i cant figure out how. i was looking at the code.
    dirname( plugin_basename( __FILE__ ) ) returns the plugin folder + subdirectory where __file__ is in.

    thats a possibility. but as far as i know it’s not “good form” to declare constants.

    i’m looking for the recommended way on how to get the current plugin directory, no matter how deep and where in subdirectories i am.

    Then maybe do what plugin_basename is doing: go direct to the global and it looks like the key to the array is the plugin dir.



    thats a possibility. but as far as i know it’s not “good form” to declare constants.

    You really need to take a look at the wp-includes/default_constants.php file. 🙂

    If you don’t want to store the value as a constant, then store it in whatever way you want…

    @diondesigns as far as i know it’s not good form if you want to develop with OO principles in mind. i tend to use them too though.
    this is a fully paid, rather large project. but at the same time it’s going to be a bit of reference work for me, thats why i try to go “by the book”. i’m using wppb and the use of constants like this is discouraged (and in my time as C# dev, it was discouraged too).

    and what confuses me: there is a function for almost everything in WP. so i have a feeling im just not using them right or dont find the right function. why those functions all feel like they have to attach the path __file__ is in?? 🙂

    still looking for the best solution 🙂

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Best way to get a plugin url’ is closed to new replies.