In a question that is related to, but not the same as, How to link to images in my plugin regardless of the plugin folder's name

  • plugin-folder/plugin-main-file.php
  • plugin-folder/images/ containing all images
  • plugin-folder/classes/ containing class definitions
  • plugin-folder/classes/class1-files containing additional files for class 1
  • plugin-folder/classes/class2-files containing additional files for class 2
  • etc

To access images, I can use relative addressing, such as plugins_url( '../images/image.png', dirname( __FILE__ ) ) but this requires a knowledge of the file structure, and a level of hard-coding that leads to potential maintenance issues.

Alternatively, I can define a global constant (eg) define('PLUGIN_ROOT', basename(dirname(__FILE__)) . '/' . basename(__FILE__)); in my main-file.

Ideally, there is a Wordpress function that would give me the plugin base name, from anywhere in my plugin (ie always return /plugin-folder/)... but I haven't found one. Is there such a beastie?


I am also aware of plugins_url() with no parameters... but this returns http://mydomain.ext/wp-content/plugins - ie without my plugin name!

4 Answers 4


I think you are looking for plugin_dir_url:

$url = plugin_dir_url(__FILE__);

$imageurl = $url.'images/someimage.png';

EDIT: Sorry I misread the question... that is only an answer to the linked question. You could check the parent directory recursively until you find the right one:

function base_plugin_dir($dirpath) {
    if (substr(dirname($dir),-7) == 'plugins') {return $dirpath;}
    else {$dirpath = base_plugin_dir($dirpath);}
    return $dirpath;

function base_plugin_dir_url($filepath) {
    $baseplugindir = base_plugin_dir(dirname($filepath));
    $url = plugin_dir_url(trailingslashit($baseplugindir));
    return $url;

Then you can get the URL using that function from whatever file:

$basepluginurl = base_plugin_dir_url(__FILE__);
$imageurl = $basepluginurl.'/images/wordpress.png';


Better yet just set a constant from your main plugin file to use it later:

$url = plugin_dir_url(__FILE__);

So as to use in say /plugin/my-plugin/class/class1.php

$imageurl = MY_UNIQUE_PLUGIN_URL.'/images/wordpress.png';
  • It seems DEFINE is the way-ahead... maybe this is a feature that needs adding, as I can't (surely) be the only one who wants this!
    – Andrew
    Apr 9, 2016 at 16:19

Sorry for the late answer, below is the function I use to retrieve the plugin directory name in the format requested by @Andrew (i.e. with a trailing slash as mentioned in the comment to Kevin)

 * Retrieve the Plugin Directory 
 * Returns the root directory for current plugin with a trailing slash
function current_plugin_dir() {
  $base = plugin_basename( __FILE__ );
  $slash = strpos($base,'/');
  $plugin_dir = $loc? substr( $base, 0, $loc + 1): $base;
  return $plugin_dir;

To remove the slash just leave out the '+ 1' in the $plugin_dir line.

  • 1
    Thanks. A late answer is no problem.
    – Andrew
    Dec 30, 2020 at 16:22


My bad, in that case I can think of this.

$x = WP_PLUGIN_URL.'/'.str_replace(basename( __FILE__),"",plugin_basename(__FILE__));

It should return


But straight forward class I don't think wordpress has one

Yes it is

from https://codex.wordpress.org/Function_Reference/plugin_basename

$x = plugin_basename( __FILE__ );
  • Thanks... I am aware of basename - but for file plugin-folder/classes/class1-files/class1-file1.php that returns plugin-folder/classes/class1-files/class1-file1.php not the desired plugin-folder/ URL
    – Andrew
    Apr 6, 2016 at 10:32

plugin_basename may be what you want. If your plugin file is located at /wp-content/plugins/my-plugin/my-plugin.php this would return:

$file = plugin_basename( __FILE__ ); // "my-plugin/my-plugin.php"

If you called the same thing from a sub-directory in your plugin, such as /wp-content/plugins/my-plugin/inc/my-plugin-include.php it would return:


If you want the current root directory for a plugin, then this custom function should do the trick:

 * Current Plugin Directory 
 * Returns the root directory for current plugin
function current_plugin_dir() {
    $basename = plugin_basename( __FILE__ );

    return str_replace( __FILE__, '', $basename );
  • Thanks Kevin... I am wanting my-plugin/
    – Andrew
    Apr 6, 2016 at 12:10
  • Got it. I just added a function for this to the bottom of my response. I think this should cover it for you Andrew, but let me know how it works out. Apr 10, 2016 at 22:34

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.