WordPress.org

Ready to get started?Download WordPress

Forums

Widget Logic
How do you return count of widgets, while considering widget_logic? (2 posts)

  1. Josh Davenport
    Member
    Posted 3 years ago #

    Using the following code you can get a count of how many widgets are in a certain area

    $the_sidebars = wp_get_sidebars_widgets();
    $widget_count = count($the_sidebars['primary-aside']);

    However, this doesn't take into account whether the widgets that are being counted actually appear on the current page due to widget logic.

    So $widget_count always equals however many widgets there are in the area primary-aside regardless to whether it's actually being displayed or not.

    How can I return a count that takes into account widget_logic on the widgets?

  2. Dadde
    Member
    Posted 3 years ago #

    Replace and add this functions in widget_logic.php:

    // the redirection comes here
    function widget_logic_redirected_callback()
    {	global $wp_registered_widgets, $wp_reset_query_is_done;
    
    	$params=func_get_args();											// get all the passed params
    	$id=array_pop($params);												// take off the widget ID
    	$callback=$wp_registered_widgets[$id]['callback_wl_redirect'];		// find the real callback
    	$wl_options = get_option('widget_logic');							// do we want the widget?
    	$wl_value=($wl_options[$id])?stripslashes($wl_options[$id]):"true";
    	$wl_value=(stristr($wl_value, "return"))?$wl_value:"return (".$wl_value.");";
    	global $widget_logic_id;
    
    	// before we execute the condtional code, perhaps we want to wp_reset_query...
    	if ($wl_options['widget_logic-options-wp_reset_query']=='checked' && !$wp_reset_query_is_done)
    	{	wp_reset_query(); $wp_reset_query_is_done=true;	}
    
    	$wl_value=(eval($wl_value) && is_callable($callback));
    	if ( $wl_value )
    	{	if ($wl_options['widget_logic-options-filter']!='checked')
    			call_user_func_array($callback, $params);					// if so callback with original params!
    		else
    		{	ob_start();
    			call_user_func_array($callback, $params);					// if so callback with original params!
    			$widget_content = ob_get_contents();
    			ob_end_clean();
    
    			if(($content = apply_filters( 'widget_content', $widget_content, $id))=="")
    			{
    				$widget_logic_id[] = $id;
    			}
    			else
    				echo $content;
    		}
    	}
    	else
    	{
    		$widget_logic_id[] = $id;
    	}
    }
    
    add_filter('sidebars_widgets', 'widget_logic_remove_widgets');
    
    function widget_logic_remove_widgets($sidebars='')
    {
    	global $widget_logic_id;
    	if(count($widget_logic_id)!=0)
    	{
    		foreach($sidebars as $sidebar_name => $widgets)
    		{
    			$i=0;
    			foreach($widgets as $widgets_name)
    			{
    				if(in_array($widgets_name, $widget_logic_id))
    				{
    					unset($sidebars[$sidebar_name][$i]);
    				}
    				$i++;
    			}
    		}
    	}
    	return $sidebars;
    }

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic