plugin_dir_path ( string $file )

Get the filesystem directory path (with trailing slash) for the plugin __FILE__ passed in.

Return: (string) the filesystem path of the directory that contains the plugin.

Source file: wp-includes/plugin.php

View source


Parameters

$file

(string) (Required) The filename of the plugin (__FILE__).


More Information

It is a wrapper for trailingslashit( dirname( $file ) );.

The “plugin” part of the name is misleading – it can be used for any file, and will not return the directory of a plugin unless you call it within a file in the plugin’s base directory.


Used by


Source

function plugin_dir_path( $file ) {
	return trailingslashit( dirname( $file ) );
}


Changelog


User Contributed Notes

  1. Get the directory of the current file:

    $dir = plugin_dir_path( __FILE__ );
    // Example: /home/user/var/www/wordpress/wp-content/plugins/my-plugin/
    
    
  2. Including all PHP files from a plugin sub folder and avoiding adding a unnecessary global just to determine a path that is already available everywhere just using WP core functions.

    foreach ( glob( plugin_dir_path( __FILE__ ) . "subfolder/*.php" ) as $file ) {
        include_once $file;
    }
    
    

You must log in before being able to contribute a note.