[wp-trac] [WordPress Trac] #19672: WP cannot be loaded through function because of variable scope
WordPress Trac
wp-trac at lists.automattic.com
Tue Dec 27 03:18:30 UTC 2011
#19672: WP cannot be loaded through function because of variable scope
--------------------------+-----------------------------
Reporter: uuf6429 | Owner:
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: General | Version:
Severity: normal | Keywords:
--------------------------+-----------------------------
WP uses variable scope extensively to deliver some global variables like
$wp and $wp_rewrite.
These variables are set inside wp-settings.php and assumed that the file
has been included from global context.
Because of this assumption, wordpress cannot be loaded from a localized
scope (such as a function).
Something like this fails:
{{{
function loadWP(){
include 'wordpress/index.php';
}
loadWP(); // fails
include 'wordpress/index.php'; // works (assuming current file was
included globally)
}}}
The first error reported is "Fatal Error: Call to a member function
add_rewrite_tag() on a non-object", but this gets worse when more things
dependent on these global variables simply fail.
SOLUTION
The solution is to specifically tell PHP these variables are globals. This
should be done in wp-settings.php by adding the following:
{{{ global $wp_rewrite; }}}
Before this line (line 240 in WP 3.3)
{{{ $wp_rewrite = new WP_Rewrite(); }}}
Presumably, instead you would want to have a single line at the beginning
of the file like so:
{{{ global $wp_the_query, $wp_query, $wp_rewrite, $wp; }}}
Chris.
--
Ticket URL: <http://core.trac.wordpress.org/ticket/19672>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software
More information about the wp-trac
mailing list