开发者

How can I get the current plugin directory in WordPress?

I need to get the current plugin directory like:

[wordpress_install_dir]/wp-content/plugins/plugin_name

(If getcwd() is called from the plugin, it returns [wordpress_install_dir], t开发者_如何转开发he root of installation.)


Use the WordPress core function that's designed specifically for that purpose:

<?php plugin_dir_path( __FILE__ ); ?>

See the Codex documentation here.

You also have

<?php plugin_dir_url( __FILE__ ); ?>

if what you're looking for is a URI as opposed to a server path.

See the Codex documentation here.

IMO, it's always best to use the highest-level method that's available in core, and this is it. It makes your code more future-proof.


Looking at the OP's own answer, I think the OP wants;

$plugin_dir_path = dirname(__FILE__);


This will actually get the result you want:

<?php plugin_dir_url(__FILE__); ?>

plugin_dir_url(string $file)


To get the plugin directory you can use the WordPress function plugin_basename($file). So you would use it as follows to extract the folder and filename of the plugin:

$plugin_directory = plugin_basename(__FILE__);

You can combine this with the URL or the server path of the plugin directory. Therefore you can use the constants WP_PLUGIN_URL to get the plugin directory URL or WP_PLUGIN_DIR to get the server path. But as Mark Jaquith mentioned in a comment below this only works if the plugins resides in the WordPress plugin directory.

Read more about it in the WordPress codex.


$full_path = WP_PLUGIN_URL . '/'. str_replace( basename( __FILE__ ), "", plugin_basename(__FILE__) );
  • WP_PLUGIN_URL – the URL of the plugins directory
  • WP_PLUGIN_DIR – the server path to the plugins directory

This page may help: Determining Plugin and Content Directories


Try this:

function PluginUrl() {

    // Try to use the WordPress API if possible, introduced in WordPress 2.6
    if (function_exists('plugins_url'))
        return trailingslashit(plugins_url(basename(dirname(__FILE__))));

    // Try to find it manually... can't work if wp-content was renamed or is redirected
    $path = dirname(__FILE__);
    $path = str_replace("\\", "/", $path);
    $path = trailingslashit(get_bloginfo('wpurl')) . trailingslashit(substr($path, strpos($path, "wp-content/")));
    return $path;
}

echo PluginUrl(); will return the current plugin URL.


Since WordPress 2.6.0, you can use the plugins_url() method.


When I need to get the directory, not only for the plugins (plugin_dir_path), but a more generic one, you can use __DIR__. It will give you the path of the directory of the file where is called. Now you can used from functions.php or another file!

Description:

The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(__FILE__). This directory name does not have a trailing slash unless it is the root directory. [1]1


If you want to get the current directory path within a file, you can use the magic constants __FILE__ and __DIR__ with the plugin_dir_path() function as:

$dir_path = plugin_dir_path( __FILE__ );

CurrentDirectory Path:

/home/user/var/www/wordpress_site/wp-content/plugins/custom-plugin/

__FILE__ magic constant returns the current directory path.

If you want to one level up from the current directory, you should use the __DIR__ magic constant as:

Current Path:

/home/user/var/www/wordpress_site/wp-content/plugins/custom-plugin/

$dir = plugin_dir_path( __DIR__ );

One level up path:

 /home/user/var/www/wordpress_site/wp-content/plugins/

__DIR__ magic constant returns one level up directory path.


Within the plugin's main PHP file, this only works in admin:

$plugin_data = get_plugin_data( __FILE__ );
$plugin_name = $plugin_data['Name'];


Define a constant or a function in the plugin's main file with the __DIR__ magic constants. In PHP, the __DIR__ is used to obtain the current code working directory. If you call the constant or function from other places, it will return your plugin directory path. Take a look at the example below of a constant that holds your current plugin directory path.

define( 'YOURPLUGIN_PATH', __DIR__ );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜