Spurious $GLOBALS name attribute caused 404 with widgetized theme
-
I had modified my custom theme to add widgetized sidebars, but as soon as I added the appropriate code, I got a series of 404 errors. After many days of tracing through the code, I found that I’d picked up a spurious $GLOBALS[‘name’] variable of value “Search Sidebar” that was throwing off the SQL request to the DB.
Here’s what I found….
Through my tracing I could see that a $GLOBALS[‘name’] was being added into the DB query when I used the widgetized theme, which wasn’t there with the old theme.
By tracing the flow through all the modules called by wp-blog-header.php I could see that nothing was set in $GLOBALS[‘name’] before my custom functions.php. However after the functions call, it was set to “Search Sidebar”.
I put some tracing statements into the code to see what was going on:
<?php /** * @package WordPress * @subpackage Kyoto-Green-3 */ $sidebars = array('Home Sidebar', 'Single Sidebar', 'Page Sidebar', 'List Sidebar', 'Gallery Sidebar', 'Library Sidebar', 'Search Sidebar'); foreach($sidebars as $name) { $dae_error = ">>>> 9a. Name: " . $name . " global name: " . $GLOBALS['name']; error_log($dae_error, 0); register_sidebar(array('name' => $name, 'before_widget' => '<div class="block">', 'after_widget' => '</div><div class="blockfooter"></div>', 'before_title' => '<h2>', 'after_title' => '</h2>')); $dae_error = ">>>> 9b. Name: " . $name . " global name: " . $GLOBALS['name']; error_log($dae_error, 0); }; ?>
My php error log showed:
[20-Aug-2009 07:40:13] >>>> 6yb. In wp-settings; global name: [20-Aug-2009 07:40:13] >>>> 6yc. In wp-settings; global name: [20-Aug-2009 07:40:13] >>>> 6yd. In wp-settings; global name: [20-Aug-2009 07:40:13] >>>> 9a. Name: Home Sidebar global name: Home Sidebar [20-Aug-2009 07:40:13] >>>> 9b. Name: Home Sidebar global name: Home Sidebar [20-Aug-2009 07:40:13] >>>> 9a. Name: Single Sidebar global name: Single Sidebar [20-Aug-2009 07:40:13] >>>> 9b. Name: Single Sidebar global name: Single Sidebar [20-Aug-2009 07:40:13] >>>> 9a. Name: Page Sidebar global name: Page Sidebar [20-Aug-2009 07:40:13] >>>> 9b. Name: Page Sidebar global name: Page Sidebar [20-Aug-2009 07:40:13] >>>> 9a. Name: List Sidebar global name: List Sidebar [20-Aug-2009 07:40:13] >>>> 9b. Name: List Sidebar global name: List Sidebar [20-Aug-2009 07:40:13] >>>> 9a. Name: Gallery Sidebar global name: Gallery Sidebar [20-Aug-2009 07:40:13] >>>> 9b. Name: Gallery Sidebar global name: Gallery Sidebar [20-Aug-2009 07:40:13] >>>> 9a. Name: Library Sidebar global name: Library Sidebar [20-Aug-2009 07:40:13] >>>> 9b. Name: Library Sidebar global name: Library Sidebar [20-Aug-2009 07:40:13] >>>> 9a. Name: Search Sidebar global name: Search Sidebar [20-Aug-2009 07:40:13] >>>> 9b. Name: Search Sidebar global name: Search Sidebar [20-Aug-2009 07:40:13] >>>> 6yy. In wp-settings; global name: Search Sidebar [20-Aug-2009 07:40:13] >>>> 6zz. In wp-settings; global name: Search Sidebar
So each iteration of the foreach loop was setting the $GLOBALS[‘name’] to the current $name value.
The fix was easy; I merely unset the variable at the bottom of the loop (I suppose I could have done it outside the loop):
<?php /** * @package WordPress * @subpackage Kyoto-Green-3 */ $sidebars = array('Home Sidebar', 'Single Sidebar', 'Page Sidebar', 'List Sidebar', 'Gallery Sidebar', 'Library Sidebar', 'Search Sidebar'); foreach($sidebars as $name) { register_sidebar(array('name' => $name, 'before_widget' => '<div class="block">', 'after_widget' => '</div><div class="blockfooter"></div>', 'before_title' => '<h2>', 'after_title' => '</h2>')); unset($GLOBALS['name']); }; ?>
I don’t know enough about how PHP processes variables and assigns GLOBAL variables, but this behaviour looks wrong to me.
Can anyone explain why this is happening and if there’s a better way to resolve the issue? Perhaps use other variable names that don’t clash with the wp_query ones?
- The topic ‘Spurious $GLOBALS name attribute caused 404 with widgetized theme’ is closed to new replies.