如何以正确的方式在插件中包含PHP文件
-
-
如果收到该消息,说明您做错了.从这些文件开始运行功能之前,请确保包括所有文件you're doing something wrong if you get that message. Make sure you include any files before you start running functions from these files
- 0
- 2011-01-21
- onetrickpony
-
多数民众赞成在没有,电话都在我包括的文件内!thats no it, the calls are within the files i'm including!
- 0
- 2011-01-21
- Bainternet
-
大声笑,现在我在上面的代码中看到了" WP_PLUGIN_URL" :)lol, now I see `WP_PLUGIN_URL` in your code above :)
- 0
- 2011-01-21
- onetrickpony
-
简单地说,您只能通过文件路径而不是URI包含()文件.Put very simply you can only include() files via a filepath and not a URI.
- 3
- 2011-01-21
- editor
-
这份Codex文章(可能是在您问了问题之后写的)很有帮助:http://codex.wordpress.org/Determining_Plugin_and_Content_DirectoriesThis Codex article (probably written after you asked your question) is quite helpful: http://codex.wordpress.org/Determining_Plugin_and_Content_Directories
- 1
- 2014-03-12
- henrywright
-
您是在用作AJAX端点或表单处理程序的PHP文件中执行此操作吗?您绝对不要直接调用WordPress主题或插件中的PHP文件.此外,如果您遇到了巨大的安全性问题和糟糕的性能,则包括URL也将不起作用Are you doing this in a PHP file that's being used as an AJAX endpoint, or a form handler? You should **never** make direct calls to PHP files inside WordPress themes or plugins. Also including URLs doesn't work, if it did you'd have a massive security problem, and terrible performance
- 0
- 2016-05-13
- Tom J Nowell
-
8 个回答
- 投票数
-
- 2011-01-21
首先,感谢所有回答的人,
我的问题是用不带WordPress的方式调用包含文件的完整URL.发生这种情况是因为正如我在问题上所说,我是从主插件文件中调用它们的.因此修复程序最终使用:
include_once('/ipn/paypal-ipn.php');
我在 WordPress支持. 再次感谢您的回答!
First , thank you to everyone who answered,
My problem was calling the included files with full url that way they don't go through WordPress. and that happened because as i stated on the question i was calling them from the main plugin file. so the fix ended up using:
include_once('/ipn/paypal-ipn.php');
i read about at the WordPress support. and again thanks for answering!
-
您能否重新考虑将这个答案(https://wordpress.stackexchange.com/a/32002/123092)标记为已接受?Can you please reconsider to mark this answer (https://wordpress.stackexchange.com/a/32002/123092) as accepted one?
- 0
- 2017-09-18
- I am the Most Stupid Person
-
- 2011-10-24
这次聚会迟到了,但这是" WordPress"的方式:使用
plugin_dir_path( __FILE__ )
,例如:<?php include( plugin_dir_path( __FILE__ ) . 'ipn/paypal-ipn.php'); ?>
请注意,函数确实返回文件路径的尾随斜杠.
Coming in late to this party, but here's the "WordPress" way: use
plugin_dir_path( __FILE__ )
, e.g.:<?php include( plugin_dir_path( __FILE__ ) . 'ipn/paypal-ipn.php'); ?>
Note that the function does return the trailing slash for the filepath.
-
请注意,通过使用__FILE__,它将相对于您从中调用它的当前文件进行输出,因此,如果您的include语句是从插件文件结构内的子目录中完成的,则也将获得该子目录.Note that by using `__FILE__` it will output relative to the current file you call it from, so if your `include` statement is done from a subdirectory inside your plugin file structure you'll get the subdirectory back too.
- 3
- 2018-01-30
- squarecandy
-
替代方法-如果需要*相对路径,则是`require_once(plugin_dir_path(__ DIR __).'/myfile.inc');`.The alternative - if you *NEED* relative paths, is `require_once(plugin_dir_path(__DIR__).'/myfile.inc');`
- 2
- 2019-10-04
- FoggyDay
-
- 2011-01-21
我浏览了以前创建的几个插件,以了解在插件中包含额外文件的不同方式,我注意到可以使用两种方法,可能还有更多方法.
定义您的插件目录
您的插件内部具有以下定义,以定义当前插件位置.
示例代码:
define( 'PLUGIN_DIR', dirname(__FILE__).'/' );
只是简单地包含或要求
您可以简单地使用;通过参考以下示例代码中的位置,在插件文件夹中包含,include_once,require或require_once.以下示例将基于您的根插件目录中的文件,包括位于您的插件文件夹内的文件夹中的另一个文件.
示例代码:
include "classes/plugin-core.php";
I looked through a couple of plugins that I previously created to see the diferent ways that I have included extra files inside of plugins and I noticed there are two methods you can use, there are probably more.
Define your plugin directory
Inside of your plugin have the following definition to define the current plugin location.
Example code:
define( 'PLUGIN_DIR', dirname(__FILE__).'/' );
Just a straight up include or require
You can simply use; include, include_once, require or require_once inside of your plugin folder by referencing the location like in the below example code. The below example will be based on a file in your root plugin directory including another file from within a folder inside of your plugin folder.
Example code:
include "classes/plugin-core.php";
-
相对包括可能带来各种令人讨厌的意外问题.relative includes can bring all kinds of nasty unexpected issues.
- 0
- 2017-12-01
- Mark Kaplun
-
- 2011-01-21
我最终放弃了包含的WordPress结构,并使用以下内容:
require_once(dirname(__FILE__) . '/filename.php);
我认为这不会真正解决您的问题,这似乎是范围问题,但这是我使用的代码.
关于include和require之间的区别:
如果找不到该文件,include将引发警告
如果找不到该文件,require会抛出一个致命错误.include_once和require_once将不再包含/请求文件/代码(如果已经包含/需要)(请注意,据我所知,这仅适用于特定目录中的特定文件).
I end up forgoing the WordPress constructs for includes and use the following:
require_once(dirname(__FILE__) . '/filename.php);
I don't think it will actually solve your issue, which seems to be a scope issue, but it is the code I use.
As for the difference between include and require:
include will throw a warning if the file is not found
require will throw a fatal error if the file is not foundinclude_once and require_once will not include/require the file/code again if it has already been included/required (note that as far as I can tell, this is only for a specific file in a specific directory).
-
- 2011-01-21
包含
include()语句包括并评估指定的文件.
包含一次
include_once()语句包括并评估指定的 执行过程中的文件 脚本.这是类似的行为 include()语句,只有 区别在于,如果代码来自 一个文件已经被包含了,它 不会再包含在内.作为 顾名思义,它将包括在内 只是一次.
要求
require()和include()在所有方面都相同,除了它们的方式不同 处理失败.他们都产生了 警告,但require()会导致 致命错误.换句话说,不要 犹豫是否需要使用require() 丢失的文件以停止处理 页面.
需要一次
require_once()语句包括并评估指定的 执行过程中的文件 脚本.这是类似的行为 require()语句,只有 区别在于,如果代码来自 一个文件已经被包含了,它 不会再包含在内.
上面的信息来自PHP文档,没有一个正确的信息,将取决于代码的需要,我对诸如函数之类的重要内容确实需要require(),但是对页脚或主题文件等主题文件我使用include_once或include循环,因为我可以处理警告并告诉发生错误的用户/访问者,而不仅仅是致命错误
Include
The include() statement includes and evaluates the specified file.
Include Once
The include_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include() statement, with the only difference being that if the code from a file has already been included, it will not be included again. As the name suggests, it will be included just once.
Require
require() and include() are identical in every way except how they handle failure. They both produce a Warning, but require() results in a Fatal Error. In other words, don’t hesitate to use require() if you want a missing file to halt processing of the page.
Require Once
The require_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the require() statement, with the only difference being that if the code from a file has already been included, it will not be included again.
The info above is from the PHP documentation, the thing is there is not a correct one, will depend on the need of the code, I do require() on important stuff like functions, but on theme files like footer or the loop I use include_once or include because i can handle the warning and say to the user/visitor that happend an error instead of just a fatal_error
-
正如@mtekk所说,我建议您使用tis结构: require_once(dirname(__ FILE__).'/filename.php);As the @mtekk say I would recomend you to use tis structure: require_once(dirname(__FILE__) . '/filename.php);
- 0
- 2011-01-21
- Webord
-
- 2011-01-21
您好 @בנייתאתרים:
当WordPress加载时,它会定义
add_action()
函数之前,它会尝试加载任何插件.您遇到错误的事实表明我在做奇怪的事情或您的WordPress安装有问题.您要让谁下载您的"插件" ?您是使用
include*()
还是require*()
加载它,也许是在您的wp-config.php
文件中?>Hi @בניית אתרים:
When WordPress is loading it defines the
add_action()
function before it attempts to load any plugins The fact you are getting the error tells me you are doing something strange or that something is wrong with your WordPress install.Who are you getting your "plugin" to load? Are you using an
include*()
orrequire*()
to load it, maybe in yourwp-config.php
file? -
- 2016-05-13
include( plugin_dir_path( __FILE__ ) . 'ipn/paypal-ipn.php');
或
define( 'PLUGIN_ROOT_DIR', plugin_dir_path( __FILE__ ) ); include( PLUGIN_ROOT_DIR . 'ipn/paypal-ipn.php');
或
$plugin_dir_path = plugin_dir_path( __FILE__ ); include( $plugin_dir_path . 'ipn/paypal-ipn.php');
注意:将.css&amp;插件中的.js文件
admin_enqueue_scripts
使用plugin_dir_url( __FILE__ )
$plugin_dir_uri = plugin_dir_url( __FILE__ ); wp_enqueue_style( 'plugin-style', $plugin_dir_uri . 'css/plugin-style.css');
include( plugin_dir_path( __FILE__ ) . 'ipn/paypal-ipn.php');
or
define( 'PLUGIN_ROOT_DIR', plugin_dir_path( __FILE__ ) ); include( PLUGIN_ROOT_DIR . 'ipn/paypal-ipn.php');
or
$plugin_dir_path = plugin_dir_path( __FILE__ ); include( $plugin_dir_path . 'ipn/paypal-ipn.php');
Note : to enqueu .css & .js files
admin_enqueue_scripts
inside plugin useplugin_dir_url( __FILE__ )
$plugin_dir_uri = plugin_dir_url( __FILE__ ); wp_enqueue_style( 'plugin-style', $plugin_dir_uri . 'css/plugin-style.css');
-
- 2014-05-29
无论何时在工作目录中创建一个新文件,都必须每次都包含它.但是尝试一种方法来扫描您的Directroy并自动附加它,不仅限于php文件,这有助于在两侧(后端,前端)正确地包含php,js和css文件.
http://kvcodes.com/2014/05/wordpress-theme-development-include-files-automatically/
Whenever you create a new file inside your working directory, you have to include it everytime. But try a method to scan your directroy and attach it automatically, not only the php files, Which helps to include php, js and css fiules properly on the both sides( backend, front end).
http://kvcodes.com/2014/05/wordpress-theme-development-include-files-automatically/
-
请从提供给您答案的链接中添加相关信息.将链接用于信用目的.请[编辑]您的问题Please add relevant information from the link provided to your answer. Use the link for credit purposes. Please [edit] your question
- 1
- 2014-05-29
- Pieter Goosen
我的问题是,当在主插件文件中包含如下所示的PHP文件时:
在该文件上,我调用了WordPress函数,例如:
我得到:
现在如果您说"使用
if(**function_exists**('add_action')){
",那它就不起作用了.问题:
include
,include_once
,require
和我何时使用巫婆有什么区别?