问题描述
我正在尝试对插件进行反向工程: http://wordpress.org/extend/plugins /wordpress-social-login/
I'm trying to reverse-engineer a plugin : 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/' );
我的问题是...代码中需要该wp-load.php
文件中的确切内容是什么?通过查看,我所了解的是它为网站正常运行加载了关键的核心wordpress文件(functions.php
,wp-settings.php
,wp-config.php
等...)
插件已经运行的事实是否意味着已加载wp-load.php
?
同样,这完全浪费资源,因为它包含了太多文件,可能还包含其他文件,并且就像一个循环不断的必需文件,每个文件在另一个文件中被加载两次..(或者,如果其他插件使用此文件,则加载更多)也是一种方法)
My question is... what exactly is in this wp-load.php
file 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.php
etc...)
Doesn't the fact that the plugin runs already means wp-load.php
is 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)
那它到底是做什么的?
P.S;我通过Google-ing发现的所有事情都是如何正确地包含它(因为路径是可以更改的)-但这不是我的问题/问题.
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.
推荐答案
所有核心WordPress功能.其中包括主题文件,活动插件的所有文件等.但以这种方式加载WordPress不会解析请求的URL,也不会运行WordPress查询(通过初始化WP对象,或WP_Query对象).
All of the core WordPress functionality. This includes the theme files, all the files of active plugins, etc. BUT loading 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).
是的,您已经正确理解.
Yes, you've understood correctly.
如果插件代码被WordPress调用(例如为了显示一个管理页面,或者它最初包含在最初加载的插件文件中)-那么,这意味着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.php
has already been loaded.
有时候,插件会将请求直接定向到单个文件(例如,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
).
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=sample
or http://example.com/wp-admin/admin-ajax.php
).
查看第一个URL如何引用my-plugin
插件目录中的特定文件,第二个URL如何访问添加了特定查询参数的网站主页,或者第三个示例,其中引用的文件为在wp-admin
目录中-这是一个特殊文件,可让插件轻松进行AJAX请求(此文件还加载WordPress核心并触发一些操作挂钩).
See how the first URL references a specific file in the my-plugin
plugin 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.php
in the wp-admin
directory - 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).
在第一次引用的情况下,如果插件要使用某些WordPress功能(用于引用数据库,处理帖子等),则需要通过包含wp-load.php
来加载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
.
请注意require_once(...
中的_once
部分-这告诉PHP如果尚未包含文件,则仅包含 文件.因此,不会发生冲突,PHP不会使用太多内存.虽然-如果您处于已启动WordPress的环境中,则不应调用require
函数.
Note the _once
part in require_once(...
- this tells PHP to include the file only if 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 require
function.
因此,基本上,插件作者希望对您在其中找到此代码的插件文件发出一些请求.由于作者希望在此文件中使用WordPress功能,因此他调用wp-load.php
文件以加载核心功能.
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.php
file in order to load the core functions.
我假设这样做是为了减少服务器上的负载,尽管在plugins_loaded
动作钩子上运行了两个钩子,并将自定义$_GET
参数添加到了主URL,但是结果应该仍然非常接近.
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_loaded
action hook and a custom $_GET
parameter added to the home url, the result should still be pretty close.
我个人更喜欢第二种选择,但是就像我说的那样,包括wp-load.php
将阻止WordPress运行某些复杂的内容(URL解析和数据库查询).
I personally prefer the second option, but like I said, including wp-load.php
will prevent WordPress from running some complex stuff(URL parsing and database query/ies).
如果还有什么,您对此不太了解-请在此处发表评论,我将尝试进一步解释.
If there is still something, that you don't quite understand about that - post a comment here and I'll try to explain further.
这篇关于WordPress的wp-load.php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!