WordPress.org

Ready to get started?Download WordPress

Codex

Function Reference/plugin dir path

Contents

Description

Gets the filesystem directory path (with trailing slash) for the file passed in.

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.

Usage

<?php $path plugin_dir_path$file ); ?>

Parameters

$file
(string) (required) A full file path (e.g. __FILE__).
Default: None

Return Values

(string) 
The absolute path of the directory that contains the file, with trailing slash.

Examples

Get the directory of the current file:

$dir = plugin_dir_path( __FILE__ );

// Example: /home/user/var/www/wordpress/wp-content/plugins/my-plugin/

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;

Change Log

Since: 2.8

Source File

plugin_dir_path() is located in wp-includes/plugin.php

Related

This can be fairly reliably used to find the local path to the plugins directory by using a string manipulation to remove just the base directory of the plugin (equivalent to the non-existent plugins_dir_path()).


Plugin paths: plugins_url(), plugin_dir_url(), plugin_dir_path(), plugin_basename()