Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress wp-load.php

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/' ); 

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)

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.

like image 509
Asaf Avatar asked Dec 13 '12 13:12

Asaf


People also ask

What is WP load PHP?

wp-load. php is responsible of bootstrapping the WordPress environment which make the plugin able to use the native WordPress Core function.

How do I fix WordPress failed to open stream error?

Changing the permissions setup will fix the problem. After logging into your WordPress website via an FTP client, locate the root folder. Then right-click on it and choose File Permissions. All folders should have a permission of 744 or 755.

How do I download PHP from WordPress?

php file is usually located in the root folder of your website with other folders like /wp-content/. Simply right click on the file and then select download from the menu.


2 Answers

My question is... what exactly is in this wp-load.php file that it needs to be required by the code?

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).

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...)

Yes, you've understood correctly.

Doesn't the fact that the plugin runs already means wp-load.php is loaded?

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.

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).

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).

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.

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 _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.


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.

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.

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.

like image 195
Nikola Ivanov Nikolov Avatar answered Oct 10 '22 04:10

Nikola Ivanov Nikolov


wp-load.php is responsible of bootstrapping the WordPress environment which make the plugin able to use the native WordPress Core function.

Now as for

Doesn't the fact that the plugin runs already means wp-load.php is loaded?

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.

like image 24
Bainternet Avatar answered Oct 10 '22 05:10

Bainternet