Wordpress wp-load.php
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13860577/
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
Wordpress wp-load.php
提问by Asaf
I'm trying to reverse-engineer a plugin : http://wordpress.org/extend/plugins/wordpress-social-login/
我正在尝试对插件进行逆向工程:http: //wordpress.org/extend/plugins/wordpress-social-login/
In a part of it, there's this line:
(I'm having a hard time understanding the first one, the rest are simply there for reference if they have something to do it.)
在它的一部分中,有这样一行:(
我很难理解第一条,其余的只是在那里做参考,如果他们有事情要做。)
require_once( dirname( dirname( dirname( dirname( __FILE__ )))) . '/wp-load.php' );
define( 'WORDPRESS_SOCIAL_LOGIN_PLUGIN_URL', plugins_url() . '/' . basename( dirname( __FILE__ ) ) );
define( 'WORDPRESS_SOCIAL_LOGIN_HYBRIDAUTH_ENDPOINT_URL', WORDPRESS_SOCIAL_LOGIN_PLUGIN_URL . '/hybridauth/' );
My question is... what exactly is in this wp-load.phpfile that it needs to be required by the code? By looking at it, all I understand is that it loads crucial core wordpress files for the site to be running correctly (functions.php, wp-settings.php, wp-config.phpetc...)
Doesn't the fact that the plugin runs already means wp-load.phpis loaded?
Also it's a complete waste of resources since it includes so many files that may include other files as well and it's like an endless loop of required files, each within another, which are being loaded twice.. (or even more if other plugins use this kind of method too)
我的问题是......这个wp-load.php文件中到底有什么代码需要它?通过观察它,所有我的理解是,它加载的网站的关键核心WordPress的文件被运行正常(functions.php,wp-settings.php,wp-config.php等...),
那岂不是插件已经运行手段的事实wp-load.php被加载?
这也是一种资源的完全浪费,因为它包含了很多可能还包含其他文件的文件,就像一个无限循环的所需文件,每个文件都在另一个文件中,被加载两次..(或者甚至更多,如果其他插件使用这个也是一种方法)
So what exactly does it do?
那么它究竟有什么作用呢?
P.S; All I found by Google-ing is HOW to include it correctly (since paths are change-able) - but that's not my problem/question.
PS; 我通过 Google-ing 发现的只是如何正确包含它(因为路径是可变的) - 但这不是我的问题/问题。
回答by Nikola Ivanov Nikolov
My question is... what exactly is in this
wp-load.phpfile that it needs to be required by the code?
我的问题是......这个
wp-load.php文件中到底有什么代码需要它?
All of the core WordPress functionality. This includes the theme files, all the files of active plugins, etc. BUTloading WordPress in this way doesn't parse the requested URL and doesn't run the WordPress query(by initializing the WP object, nor the WP_Query objects).
WordPress 的所有核心功能。这包括主题文件、活动插件的所有文件等。但是以这种方式加载 WordPress 不会解析请求的 URL 并且不会运行 WordPress 查询(通过初始化 WP 对象,也不是 WP_Query 对象)。
By looking at it, all I understand is that it loads crucial core wordpress files for the site to be running correctly (
functions.php,wp-settings.php,wp-config.phpetc...)
通过观察它,所有我的理解是,它加载关键核心WordPress的文件为网站运行正常(
functions.php,wp-settings.php,wp-config.php等...)
Yes, you've understood correctly.
是的,你理解正确。
Doesn't the fact that the plugin runs already means wp-load.php is loaded?
插件运行的事实是否意味着 wp-load.php 已加载?
If the plugin code was invoked by WordPress(for instance in order to display an admin page, or it was included by the initially loaded plugin file) - then yes, it means that wp-load.phphas already been loaded.
如果插件代码是由 WordPress 调用的(例如为了显示管理页面,或者它包含在最初加载的插件文件中) - 那么是的,这意味着wp-load.php已经加载了。
Sometimes though, plugins direct requests to single files(for instance http://example.com/wp-content/plugins/my-plugin/sample.php), instead of to some WordPress-powered page(for instance http://example.com/?my_plugin_action=sampleor http://example.com/wp-admin/admin-ajax.php).
但有时,插件会将请求定向到单个文件(例如http://example.com/wp-content/plugins/my-plugin/sample.php),而不是某些 WordPress 驱动的页面(例如http://example.com/?my_plugin_action=sample或http://example.com/wp-admin/admin-ajax.php)。
See how the first URL references a specific file in the my-pluginplugin directory and the second one goes to the home page of the site with a specific query argument added, or the third example, where the referenced file is admin-ajax.phpin the wp-admindirectory - this is a special file, which makes it easy for plugins to make AJAX request(this file also loads the WordPress core and fires some action hooks).
查看第一个 URL 如何引用my-plugin插件目录中的特定文件,第二个URL 如何转到添加了特定查询参数的站点主页,或者第三个示例,其中引用的文件admin-ajax.php位于wp-admin目录中 - 这是一个特殊的文件,这使得插件可以轻松地发出 AJAX 请求(该文件还加载 WordPress 核心并触发一些动作挂钩)。
In the case of the first reference, if the plugin wants to use some WordPress functionality(for referencing the database, manipulating posts, etc), it needs to load the WordPress core files by including wp-load.php.
在第一次引用的情况下,如果插件想要使用一些 WordPress 功能(用于引用数据库、操作帖子等),它需要通过包含wp-load.php.
Also it's a complete waste of resources since it includes so many files that may include other files as well and it's like an endless loop of required files, each within another, which are being loaded twice.. (or even more if other plugins use this kind of method too)
这也是一种资源的完全浪费,因为它包含了很多可能还包含其他文件的文件,就像一个无限循环的所需文件,每个文件都在另一个文件中,被加载两次..(如果其他插件使用这个,甚至更多也是一种方法)
Note the _oncepart in require_once(...- this tells PHP to include the file onlyif it hasn't been included already. Therefore no conflicts will occur, and not too much memory will be used by PHP. Although - if you are in a context where WordPress has already been started, you shouldn't call the requirefunction.
请注意_oncein 部分require_once(...- 这告诉 PHP仅在尚未包含文件时才包含该文件。因此不会发生冲突,PHP 不会使用太多内存。虽然 - 如果您处于 WordPress 已经启动的环境中,则不应调用该require函数。
So, basically the plugin author expects some requests to be made to the plugin file in which you found this code. Since the author wants to use WordPress functionality in this file, he invokes the wp-load.phpfile in order to load the core functions.
因此,基本上插件作者希望对您在其中找到此代码的插件文件提出一些请求。由于作者想在此文件中使用 WordPress 功能,因此他调用该wp-load.php文件以加载核心功能。
I assume, that this is done in order to reduce load on the server, although with a couple of hooks that run on the plugins_loadedaction hook and a custom $_GETparameter added to the home url, the result should still be pretty close.
我认为,这样做是为了减少服务器上的负载,尽管在plugins_loaded操作挂钩上运行了几个挂钩,并将自定义$_GET参数添加到主页 url,但结果应该仍然非常接近。
I personally prefer the second option, but like I said, including wp-load.phpwillprevent WordPress from running some complex stuff(URL parsing and database query/ies).
我个人更喜欢第二个选项,但就像我说的那样,包括wp-load.php会阻止 WordPress 运行一些复杂的东西(URL 解析和数据库查询)。
If there is still something, that you don't quite understand about that - post a comment here and I'll try to explain further.
如果仍有一些您不太了解的内容 - 请在此处发表评论,我会尝试进一步解释。
回答by tix3
From what I read they usually include wp-load in the plugins when database usage is needed, but this is a bad choice as it raises a lot of problems. You can see some relevant articles in here: http://ottodestruct.com/blog/2010/dont-include-wp-load-please/( if this link is ever deleted, See that page here)
从我读到的内容来看,当需要使用数据库时,它们通常会在插件中包含 wp-load,但这是一个糟糕的选择,因为它会引发很多问题。您可以在这里看到一些相关文章:http: //ottodestruct.com/blog/2010/dont-include-wp-load-please/(如果此链接被删除,请在此处查看该页面)
回答by Bainternet
wp-load.phpis responsible of bootstrapping the WordPress environment which make the plugin able to use the native WordPress Core function.
wp-load.php负责引导 WordPress 环境,使插件能够使用本机 WordPress 核心功能。
Now as for
现在至于
Doesn't the fact that the plugin runs already means wp-load.php is loaded?
插件运行的事实是否意味着 wp-load.php 已加载?
Not at all!
一点也不!
If you access a plugin file directly it doesn't mean that you have the whole WordPress environment and you are not able to use the native core functions unless you include wp-load.php.
如果您直接访问插件文件,并不意味着您拥有整个 WordPress 环境并且您无法使用本机核心功能,除非您包含wp-load.php.
回答by user3916019
This will include wp-load.php if not already loaded if the file is located anywhere, regardless of level, within the wp-content directory.
如果文件位于 wp-content 目录中的任何位置,无论级别如何,这将包括 wp-load.php 如果尚未加载。
if(!defined(ABSPATH)){
$pagePath = explode('/wp-content/', dirname(__FILE__));
include_once(str_replace('wp-content/' , '', $pagePath[0] . '/wp-load.php'));
}
回答by Prathik Rajendran M
Probably a double check.
可能是双重检查。
require_once() means that if it has already been loaded, then it will not load again.
require_once() 表示如果已经加载,则不会再次加载。
回答by Felipe Alameda A
wp-load.phpis one way to load WP from external scripts, allowing the use of WP functions among other features.
wp-load.php是从外部脚本加载 WP 的一种方式,允许在其他功能中使用 WP 功能。
But, as you say, that should not be necessary as it is a plugin. Nevertheless, you don't explain where did you find the code in your question, because wp-load.phpis indeed needed for front-end pages or scripts located in a directory different from the style sheet directory, for example, even when they are part of a plugin.
但是,正如您所说,这不是必需的,因为它是一个插件。尽管如此,您没有解释您在哪里找到问题中的代码,因为wp-load.php前端页面或脚本确实需要位于不同于样式表目录的目录中,例如,即使它们是插件的一部分.
Plugin pages in the admin area don't have to reload WP because it is already loaded, but front-end pages do have to load it.
管理区的插件页面不需要重新加载WP,因为它已经加载了,但前端页面必须加载它。
In short, there are several reasons to include wp-load.phpto have access to WP functions and variables.
简而言之,包括wp-load.php访问 WP 函数和变量有几个原因。

