php 如何在WordPress中获取当前插件目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3128873/
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-08-25 08:46:08  来源:igfitidea点击:

How to get the current plugin directory in WordPress?

phpwordpressplugins

提问by bog

I need to get the current plugin directory like

我需要获取当前的插件目录,例如

[wordpress_install_dir]/wp-content/plugins/plugin_name

(if getcwd()called from the plugin, it returns [wordpress_install_dir], the root of installation)

(如果getcwd()从插件调用,它返回[wordpress_install_dir],安装的根目录)

thanks for help

感谢帮助

回答by Tom Auger

Why not use the WordPress core function that's designed specifically for that purpose?

为什么不使用专为此目的设计的 WordPress 核心功能?

<?php plugin_dir_path( __FILE__ ); ?>

See Codex documentation here.

请参阅此处的Codex 文档。

You also have

你还有

<?php plugin_dir_url( __FILE__ ); ?>

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

如果您要查找的是 URI 而不是服务器路径。

See Codex documentation here.

请参阅此处的Codex 文档。

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.

IMO 始终最好使用核心中可用的最高级别方法,就是这样。它使您的代码更具前瞻性。

回答by TheDeadMedic

Looking at your own answer @Bog, I think you want;

看看你自己的答案@Bog,我想你想要;

$plugin_dir_path = dirname(__FILE__);

回答by brenjt

This will actually get the result you want:

这实际上会得到你想要的结果:

<?php plugin_dir_url(__FILE__); ?>

http://codex.wordpress.org/Function_Reference/plugin_dir_url

http://codex.wordpress.org/Function_Reference/plugin_dir_url

回答by stefanglase

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

要获取插件目录,您可以使用 Wordpress 功能plugin_basename($file)。因此,您将使用如下提取插件的文件夹和文件名:

$plugin_directory = plugin_basename(__FILE__); 

You can combine this with the URL or the server path of the plugin directory. Therefor you can use the constants WP_PLUGIN_URLto get the plugin directory url or WP_PLUGIN_DIRto get the server path. But as Mark Jaquithmentioned in a comment below this only works if the plugins resides in the Wordpress plugin directory.

您可以将其与插件目录的 URL 或服务器路径结合使用。因此,您可以使用常量WP_PLUGIN_URL来获取插件目录 url 或WP_PLUGIN_DIR获取服务器路径。但正如Mark Jaquith在下面的评论中提到的,这仅在插件驻留在 Wordpress 插件目录中时才有效。

Read more about it in the Wordpress codex.

Wordpress codex 中阅读有关它的更多信息。

回答by manish nautiyal

$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
  • WP_PLUGIN_URL – 插件目录的 url
  • WP_PLUGIN_DIR – 插件目录的服务器路径

This link may help: http://codex.wordpress.org/Determining_Plugin_and_Content_Directories.

此链接可能会有所帮助:http: //codex.wordpress.org/Determining_Plugin_and_Content_Directories

回答by Pennywise83

Try this:

尝试这个:

function PluginUrl() {

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

        //Try to find 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.

echo PluginUrl(); 将返回当前插件 url。

回答by Idham Perdameian

Since WP 2.6.0 you can use plugins_url()method.

从 WP 2.6.0 开始,您可以使用plugins_url()方法。

回答by Francisco Corrales Morales

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.phpor another file!

当我需要获取目录时,不仅是插件(plugin_dir_path),而是一个更通用的目录,您可以使用__DIR__,它将为您提供调用文件所在目录的路径。现在您可以从functions.php或其他文件中使用!

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

文件的目录。如果在包含中使用,则返回包含文件的目录。这相当于 dirname( __FILE__)。除非它是根目录,否则此目录名称没有尾部斜杠。1

回答by Gufran Hasan

If you want to get current directory path within a file for that you can magic constants __FILE__and __DIR__with plugin_dir_path()function as:

如果您想获取文件中的当前目录路径,您可以使用魔术常量__FILE____DIR__使用以下plugin_dir_path()功能:

$dir_path = plugin_dir_path( __FILE__ );

CurrentDirectory Path:

当前目录路径:

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

__FILE__magic constant returns current directory path.

__FILE__魔术常量返回当前目录路径。

If you want to one level up from the current directory. You should use __DIR__magic constant as:

如果您想从当前目录向上一级。您应该将__DIR__魔术常量用作:

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.

__DIR__魔术常量返回上一级目录路径。