Wordpress 获取插件目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20780422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:26:31  来源:igfitidea点击:

Wordpress get plugin directory

wordpresswordpress-plugin

提问by Cindy93

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

是否有任何函数可以返回我的插件在 WordPress 中的完整路径?

Example is

例子是

path/wp-contents/plugins/myplugin

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

我试过,plugin_dir_path(__FILE__)但返回当前目录。

采纳答案by Rikesh

Yeah as per description of plugin_dir_pathit 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_path 的描述,它将为您提供当前的插件文件路径。但根据你在这里问的问题,不幸的是,你可以做类似下面的事情,没有直接的方法,

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

回答by Philipp

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

我建议使用 WordPress 内部常量来解决这种情况:

$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:

另一个有效的替代方法是从更复杂/令人困惑的 URL 计算路径。我不会使用此代码:

$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

在这种情况下,我的意见是:使用常量 WP_PLUGIN_DIR

回答by Vivek Tamrakar

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

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

插件路径代码

echo  WP_PLUGIN_DIR.'/plugin-name';

回答by Rio Adetya Rizky

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

plugin_dir_path( __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/"

这意味着如果你在“your_plugin_dir/sub_dir/file.php”中调用这个函数 将返回“your_plugin_dir/sub_dir/”

if you want to get the ROOTof your plugin directory, just change __FILE__to __DIR__

如果你想获得你的插件目录的目录,只需更改__FILE____DIR__

plugin_dir_path( __DIR__ )

回答by Jbro

My solution has been to use inside plugin_dir_path DIR

我的解决方案是在 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

以上应该为您提供服务器上插件文件夹的绝对路径,然后您可以使用插件文件夹中的任何文件加载变量

回答by decoder88

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(__FILE__)将始终返回当前路径(调用它的文件所在的位置)。
如果您想要根,请使用以下代码:

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'

回答by M07

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

您可以在主 PHP 文件中定义一个变量。它应该在你的插件文件夹的根目录下。该文件应该在这里:.../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.

我希望它会帮助某人。

回答by Brosig

As noted on the section Common Usageof Plugins Urlfunction reference page, that's what worked for me:

由于上一节所述Common Usage插件地址功能参考页,这是对我工作:

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:

如果您在嵌套在插件目录子目录中的文件中使用 plugins_url() 函数,则应使用 PHP 的 dirname() 函数:

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.phpinside includes, so the complete path to my file was:

调用该函数的文件是 my functions.phpinside includes,所以我的文件的完整路径是:

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

回答by SherylHohman

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

plugins_dir_path()是用词不当。
它将始终返回当前文件的路径。

Link to the codex:
https://developer.wordpress.org/reference/functions/plugin_dir_path/#more-information

链接到代码: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.

它是 trailingslashit( dirname( $file ) ); 的包装器。

名称的“插件”部分具有误导性——它可以用于任何文件,并且不会返回插件的目录,除非您在插件的基本目录中的文件中调用它。

It is actually synonym for the trailingslashit()function.

它实际上是trailingslashit()函数的同义词。

IFthe current file is in the pluginsdirectory, thenyes, 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 notin the plugins directory, and thus it will does NOTreturn the path to the plugins directory. It doesalways return the path to the current file, but without a trailing slash.

如果当前文件在plugins目录中,是,该函数返回当前文件的路径。
但是,如果您从任何其他目录中的文件调用此函数,则当前文件不在插件目录中,因此它不会返回插件目录的路径。它总是路径返回到当前文件,但没有结尾的斜线。

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

如果是这种情况,此代码将向您展示如何创建当前文件的 URL: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__) ) . '" > '; ?>

如果您在嵌套在插件目录子目录中的文件中使用 plugins_url() 函数,则应使用 PHP 的 dirname() 函数:

<?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:

这些解决方案与调用函数的文件所在的位置无关。如果您的文件位于 plugins 文件夹或其子目录中,则上述选项将起作用。否则,您将需要采取以下措施:

WP_PLUGIN_DIR

WP_PLUGIN_DIR

WordPress doeshave a constant defined, for the Plugins' folder:

WordPress确实为插件文件夹定义了一个常量:

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

法典: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)

常数

WordPress 在确定内容和插件目录的路径时使用以下常量。这些不应由插件或主题直接使用,但为了完整性在此处列出。

WP_CONTENT_DIR // 没有尾部斜杠,只有完整路径
WP_CONTENT_URL // 完整 url
WP_PLUGIN_DIR // 完整路径,没有尾部斜杠
WP_PLUGIN_URL // 完整 url,没有尾部斜杠

// 在 MS 中默认可用,未在单站点安装中设置
// 可以在单站点安装中使用(照常:风险自担)
上传 //(如果设置,则上传文件夹,相对于 ABSPATH)(例如: /wp-内容/上传)



Or, If you can guarantee that the plugins folder is located in the normal place for a WordPress install, i_a's answer aboveanswer 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):

或者,如果您可以保证插件文件夹位于 WordPress 安装的正常位置,则无论您的文件(您想从中调用此函数的目录)在哪个目录中,i_a 上面的回答都将起作用。请参阅他在这个线程中的更完整的帖子,但为了没有“仅链接的答案”,我将在这里包括它本质上,它归结为使用以下内容(并将其转换为函数):

$plugins_root = WP_CONTENT_DIR . '/plugins';   

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

或者 M07 的帖子,也在这个帖子里,在这里:https: //stackoverflow.com/a/28525164/5411817

回答by i_a

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

这是一个解决方案,当您不在插件根目录中时:

As of now with 4.7.5, WordPress does not havea 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.

截至目前有4.7.5,WordPress的没有一个get_plugins_root()函数像有一个get_theme_root()函数。这可能是因为您真的不需要修改主题中的插件,并且插件根目录永远不会改变。

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

但是,如果您需要在开发主题时以编程方式影响插件,它会很有用。

Simply, as WP does for the theme root:

简单地说,就像 WP 对主题根所做的那样:

$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?

或者,如果您需要一个功能,为什么不像 WordPress 为主题根做的那样做呢?

Just make a copy of the function get_theme_root()from wp-includes/theme.phpand 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...

只需从wp-includes/theme.php复制函数get_theme_root()并将其粘贴到主题的 functions.php 文件中,将该函数重命名为 get_plugins_root(),简化它,并将“theme”更改为“plugins”功能...

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/";