plugin_dir_path( string $file ): string

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


Parameters

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

Top ↑

Return

string the filesystem path of the directory that contains the plugin.


Top ↑

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.


Top ↑

Source

File: wp-includes/plugin.php. View all references

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


Top ↑

Changelog

Changelog
Version Description
2.8.0 Introduced.

Top ↑

User Contributed Notes

  1. Skip to note 1 content
    Contributed by Codex

    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;
    }
  2. Skip to note 3 content
    Contributed by Codex

    Get the directory of the current file:

    $dir = plugin_dir_path( __FILE__ );
    // Example: /home/user/var/www/wordpress/wp-content/plugins/my-plugin/
  3. Skip to note 5 content
    Contributed by Codex

    Conditional loading

    It is sometimes efficient to conditionally load files, e.g., admin-only (or even by specific admin screen):

    if ( is_admin() ) {
        include_once( plugin_dir_path( __FILE__ ) . 'includes/admin-functions.php' );
    } else {
        include_once( plugin_dir_path( __FILE__ ) . 'includes/front-end-functions.php' );
    }
  4. Skip to note 6 content
    Contributed by bharatthapa

    define( 'PREFIX_BASE_PATH', plugin_dir_path( __FILE__ ) );
    define( 'PREFIX_ASSETS_URL', plugins_url( '/assets', __FILE__ ) );

    use constant ‘PREFIX_BASE_PATH to include files in functions and files, e.g.,
    include( PREFIX_BASE_PATH . 'inc/init.php' );

    use constant: ‘PREFIX_ASSETS_URL’ to load assets via url (like; js, css, and images). e.g.,
    wp_register_style( 'prefix_library', PREFIX_ASSETS_URL . '/dir/lib.css' );

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