Wordpress get plugin directory

2020-05-15 13:00发布

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.

14条回答
再贱就再见
2楼-- · 2020-05-15 13:12

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    
查看更多
对你真心纯属浪费
3楼-- · 2020-05-15 13:13

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__ )
查看更多
Anthone
4楼-- · 2020-05-15 13:14

You can gain it by using this method

plugin_dir_url( __DIR__ )
查看更多
ゆ 、 Hurt°
5楼-- · 2020-05-15 13:15

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/";
查看更多
戒情不戒烟
6楼-- · 2020-05-15 13:20

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
查看更多
Animai°情兽
7楼-- · 2020-05-15 13:21

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'
查看更多
登录 后发表回答