Question

I read about 2 methods for initializing WordPress function outside of WordPress files so We can use these functions on any page or website outside the WordPress blog.

Which one of these 2 methods is the correct one? What are the use cases for each method if both are correct? What is the deference between using one method or the other?

Method 1:

<?php 
    define('WP_USE_THEMES', false);
    require('./wp-blog-header.php');
?>

Method 2:

<?php 
    define('WP_USE_THEMES', false);
    require('./wp-load.php');
?>
Was it helpful?

Solution

There's little difference between the files. When you view a WordPress page, the first file called is index.php. And it is, essentially, your "Method 1:"

define('WP_USE_THEMES', true);

/** Loads the WordPress Environment and Template */
require ('./wp-blog-header.php');

The blog header file (that queues up the rest of WordPress) loads wp-load.php directly and fires up WordPress itself. Here's most of wp-blog-header.php:

if ( !isset($wp_did_header) ) {

    $wp_did_header = true;

    require_once( dirname(__FILE__) . '/wp-load.php' );

    wp();

    require_once( ABSPATH . WPINC . '/template-loader.php' );

}

So the difference between your two methods is ... what's loaded.

Method 1 is exactly what WordPress does to load itself (with the exception of turning themes off). So if you need all of WordPress and want to fire all of the default hooks/actions, go with that route.

Method 2 is just a further step down the line. It loads all of WordPress, but doesn't call wp() or invoke the template loader (used by themes). Method 2 will be a little lighter-weight, but should give you the same functionality.

OTHER TIPS

Method 2 from your question:

<?php 
define( 'WP_USE_THEMES', false ); // Don't load theme support functionality
require( './wp-load.php' );

wp-load.php is the access to all functions of WordPress, that's all. The first line tells WordPress to load not the Theme files; maybe the files are necessary for your requirements, then remove the line.

wp-blog-header.php will attached a header status, it will return a http status code of 404

wp-load.php will not

Useful to note when using ajax as it checks the http status code

Sometimes loading the functions.php of the theme can cause you some trouble. It was breaking the html of my other page. So that's what I did and solved my problem:

define('STYLESHEETPATH', '');
define('TEMPLATEPATH', '');
require_once(RAIZ_WORDPRESS."/wp-load.php");

@ninja08

We can use xDebug php extension to analyze an script.

just enable ;xdebug.profiler_enable = 1 in your php.ini file by removing ; from first of line and after this restart apache server and run your wordpress site ...now a file created in tmp directory of your xampp server ..open this file with WincachGrind application.

now you can see a map of your script

WincacheGrind Simple Wordpress Analyze

You don't have to call the entire theme to use functions, just use the location for wp-load.php in wordpress directory.

<?php
require($_SERVER['DOCUMENT_ROOT'] . '/wordpress/wp-load.php');

?>
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top