Chris
unread,Jan 25, 2012, 10:58:42 AM1/25/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Gantry Framework Developers
Hi,
I'm the author of a WordPress plugin and had a customer that was
having trouble with Gantry. Basically, adding widgets via my plugin
was causing errors to be thrown by Gantry.
Warning: Invalid argument supplied for foreach() in /home/laundro/
public_html/wp-content/plugins/gantry/core/renderers/
gantrywidgetsrenderer.class.php on line 154
After digging through the Gantry code for a while, this is what I've
found:
From what I can tell, Gantry augments the WordPress widgets with a
"widget_map" (not sure exactly its purpose, but I don't think it's a
core WordPress feature). Then Gantry adds a filter to the widgets,
and it assumes this widget_map exists, attempting to access it in an
array. I'm guessing the widget_map doesn't exist because this is a
widget area added by a plugin rather than Gantry. However, the filter
is applied to all widgets and doesn't check to see if the widget_map
exists. Since it doesn't, it throws an error. Since the filter
throws an error, it returns null, and the parameters it filters get
cleared, so this actually causes 2 errors, and the second prevents the
widget from being displayed.
The (potential) solution:
Add a check to the Gantry function to ignore widgets without the
widget_map parameter:
if( !isset( $params[0]['widget_map'] )) return $params;
This would be placed in plugins/gantry/core/renderers/
gantrywidgetsrenderer.class.php, around line 148, so you end up with
this as the filterWidget() function:
function filterWidget($params) {
if( !isset( $params[0]['widget_map'] )) return $params;
global $gantry;
$widget_id = $params[0]['widget_id'];
$layout = $params[0]['layout'];
// find the widget and its position
foreach ($params[0]['widget_map'] as $pos => $position_info){
$position_widgets = $position_info['widgets'];
if (!array_key_exists($widget_id,$position_widgets))
continue;
$params[0]['position'] = $pos;
$params[0]['end'] = end(array_keys($position_widgets));
$params[0]['start'] =
reset(array_keys($position_widgets));
break;
}
$params = $gantry->renderLayout('widget_'.$layout, $params);
return $params;
}
The only alternative I could find would be to temporarily remove the
filter in any other plugin in order to avoid the error:
if(function_exists('dynamic_sidebar')){
//Gantry-specific
remove_filter('dynamic_sidebar_params',
array('GantryWidgetsRenderer', 'filterWidget'));
ob_start();
echo '<ul id="mysidebar">';
dynamic_sidebar($name);
echo '</ul>';
//Gantry-specific
add_filter('dynamic_sidebar_params', array('GantryWidgetsRenderer',
'filterWidget'));
return ob_get_clean();
}
Of course that's just a hack and clearly not optimal. A filter
shouldn't throw errors because a non-standard field is missing when
calling a standard WordPress function.
Hope you'll either add that fix or a comparable one to your next
release! It doesn't seem like something that can be solved gracefully
from outside your framework.
Thanks!
Chris