Wordpress get plugin directory

2020-05-15 13:03发布

问题:

Is there any function that would return the full path of my plugin in WordPress?

Example is

path/wp-contents/plugins/myplugin

I have tried plugin_dir_path(__FILE__) but returns the current dir.

回答1:

Yeah as per description of plugin_dir_path it will give you current plugin file path. But as per what you asking here you can do something like below unfortunately no direct way,

$plugin_dir = ABSPATH . 'wp-content/plugins/plugin-folder/';


回答2:

I would suggest to use a WordPress internal constant to solve this case:

$my_plugin = WP_PLUGIN_DIR . '/my-plugin';

if ( is_dir( $my_plugin ) ) {
    // plugin directory found!
}

Alternative

The other valid alternative is to compute the path from the URL which is more complex/confusing. I would not use this code:

$plugins_url = plugins_url();
$base_url = get_option( 'siteurl' );
$plugins_dir = str_replace( $base_url, ABSPATH, $plugins_url );
// Now $plugins_dir is same as the WP_PLUGIN_DIR constant.

$my_plugin = $plugins_dir . '/my-plugin';

My opinion in this case is: Use the constant WP_PLUGIN_DIR



回答3:

Code For Plugin Root Path

$dir = plugin_dir_path( __FILE__ );
// Example: /home/user/var/www/wordpress/wp-content/plugins/my-plugin/

Code for plugin path

echo  WP_PLUGIN_DIR.'/plugin-name';


回答4:

plugin_dir_path( __FILE__ ) will give you plugin path of current file.

this is mean if you call this function like that inside "your_plugin_dir/sub_dir/file.php" will return "your_plugin_dir/sub_dir/"

if you want to get the ROOT of your plugin directory, just change __FILE__ to __DIR__

plugin_dir_path( __DIR__ )


回答5:

My solution has been to use inside plugin_dir_path DIR

plugin_dir_path( __DIR__ ) . $path_inside_plugin_folder;

The above should give you the absolute path to your plugin folder on the server, then you can load your variable with any file within your plugin folder



回答6:

As noted on the section Common Usage of Plugins Url function reference page, that's what worked for me:

If you are using the plugins_url() function in a file that is nested inside a subdirectory of your plugin directory, you should use PHP's dirname() function:

echo plugins_url('', dirname(__FILE__) );

The output for this was:

http://domain/app/wp-content/plugins/my-plugin

The file that called the function was my functions.php inside includes, so the complete path to my file was:

http://domain/app/wp-content/plugins/my-plugin/includes/functions.php


回答7:

Kinda late to this party, but just in case some else stumbles upon this.

plugin_dir_path(__FILE__) will always return the current path (where the file calling it is located).
If you want the root, use the code below:

plugin_dir_path( dirname( __FILE__ ) )

You can then define a constant:

define( 'YOUR_PLUGIN_DIR', plugin_dir_path( dirname( __FILE__ ) ) );
require_once YOUR_PLUGIN_PATH . 'includes/admin-page.php'
require_once YOUR_PLUGIN_PATH . 'admin/classes.php'


回答8:

You can define a variable into your main Php file. It should be at the root of your plugin folder. The file should be here : .../wp-content/plugins/plugin-folder/my-plugin.php

You can add into the file this line.

    define( 'MYPLUGIN__PLUGIN_DIR_PATH', plugins_url('', __FILE__ ) );

After you can use your new variable anywhere into your plugin.

    public function Test() 
    {   
            $folder2 = MYPLUGIN__PLUGIN_DIR_PATH . '/folder1/folder2/';
           // $folder2 = .../wp-content/plugins/plugin-folder/folder1/folder2/
    }

I hope it will help someone.



回答9:

Here is a solution, when you are not inside the plugin root:

As of now with 4.7.5, WordPress does not have a get_plugins_root() function like there is a get_theme_root() function. This is probably because you really shouldn't ever need to modify plugins from your theme, and the plugins root directory never changes.

However, it can be useful if you need to programmatically affect plug-ins while developing themes.

Simply, as WP does for the theme root:

$plugin_root = WP_CONTENT_DIR . '/plugins';

Or, if you need a function, why not just do it the same way WordPress does it for the theme root?

Just make a copy of the function get_theme_root() from wp-includes/theme.php and paste it into your theme's functions.php file, rename the function to get_plugins_root(), simplify it, and change 'theme' to 'plugins' in the function...

get_plugins_root() {

    $plugins_root = WP_CONTENT_DIR . '/plugins';

    /**
     * Filters the absolute path to the plugins directory.
     *
     * @since now
     *
     * @param string $plugins_root Absolute path to plugins directory.
     */
    return apply_filters( 'plugins_root', $plugins_root );
}

With the path, you can now add the plug-ins folder name that you wish to affect.

$the_plugin_root = get_plugins_root()."/the-plugin-name/";


回答10:

plugins_dir_path() is a misnomer.
It will always return the path to the current file.

Link to the codex:
https://developer.wordpress.org/reference/functions/plugin_dir_path/#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.

It is actually synonym for the trailingslashit() function.

IF the current file is in the plugins directory, then yes, the function returns the path of the current file.
However, if you call this function from a file inside any other directory, then current file is not in the plugins directory, and thus it will does NOT return the path to the plugins directory. It does always return the path to the current file, but without a trailing slash.

Most of the answers here are incorrect, or are only "sometimes" correct. Those answers will only work as they say IF your file happens to already be located in the plugins directory! In all other cases they will give you a misleading result: the path to your current file.


It is more likely that your file is in a *subdirectory *of the plugins folder.

If this is the case, this codex shows you how to create a URL to the current file: https://codex.wordpress.org/Function_Reference/plugins_url

If you are using the plugins_url() function in a file that is nested inside a subdirectory of your plugin directory, you should use PHP's dirname() function:

<?php echo '<img src="' . plugins_url( 'images/wordpress.png', dirname(__FILE__) ) . '" > '; ?>

You would then need to remove your file name to get the path. Or use ``

ANSWER:

These solutions solutions are independant of where the file of your calling function is located. If your file is located in the plugins folder, or a subdirectory of it, then the above options would work. Otherwise, you'll need to resort to something along the lines of:

WP_PLUGIN_DIR

WordPress does have a constant defined, for the Plugins' folder:

Codex: https://codex.wordpress.org/Determining_Plugin_and_Content_Directories

Constants

WordPress makes use of the following constants when determining the path to the content and plugin directories. These should not be used directly by plugins or themes, but are listed here for completeness.

WP_CONTENT_DIR // no trailing slash, full paths only
WP_CONTENT_URL // full url
WP_PLUGIN_DIR // full path, no trailing slash
WP_PLUGIN_URL // full url, no trailing slash

// Available per default in MS, not set in single site install
// Can be used in single site installs (as usual: at your own risk)
UPLOADS // (If set, uploads folder, relative to ABSPATH) (for e.g.: /wp-content/uploads)


Or, If you can guarantee that the plugins folder is located in the normal place for a WordPress install, i_a's answer above answer above will work, no matter what directory your file (that you want to call this function from) is in. Please see his more complete post in this thread, but so as to not have a "link only answer", I'll include here that it Essentially, it boils down to using the following (and turning it into a function):

$plugins_root = WP_CONTENT_DIR . '/plugins';   

Or M07's post, also in this thread, here: https://stackoverflow.com/a/28525164/5411817



回答11:

Unfortunately, most of the answers here seem to forget about one important thing.

In addition to the plugins dir, plugins might be also in the Must-use plugins (mu-plugins) directory

Because of that, we need to check multiple directories.
An example function to do this:

function get_plugin_dir_path($pluginFolderName){

    if ( defined( 'WPMU_PLUGIN_DIR' ) && file_exists( trailingslashit( WPMU_PLUGIN_DIR ) . $pluginFolderName ) ) {
        return trailingslashit( WPMU_PLUGIN_DIR ) . $pluginFolderName;
    } elseif ( defined( 'WP_PLUGIN_DIR' ) && file_exists( trailingslashit( WP_PLUGIN_DIR ) . $pluginFolderName ) ) {
        return trailingslashit( WP_PLUGIN_DIR ) . $pluginFolderName;
    }
    return false;

}


回答12:

I will suggest following code. if you are accessing this function from any subfolder.

plugins_url( 'images/logo.png' , dirname(__FILE__ ));


回答13:

You can gain it by using this method

plugin_dir_url( __DIR__ )


回答14:

As mentioned by @tsl143 the function plugins_url() is what you want, along with the __FILE__ magic constant. If you call it from a file inside your plugin folder it will refer to that folder.

Example:

For the directory:

echo plugins_url( '' , __FILE__ ); 
//outputs: http://www.example.com/wp-content/plugins/my-plugin    

For a file or subdirectory in your plugin directory:

echo plugins_url( 'images/logo.png' , __FILE__ ); 
//outputs: http://www.example.com/wp-content/plugins/my-plugin/images/logo.png