Marc Blase

Remove Widget from WP Sidebar

add_filter('sidebars_widgets', 'hidemywidget');
function hidemywidget($all_widgets) {
    // Uncomment to get access to the identifiers we are trying to target
    //echo "<pre>";
    //print_r($all_widgets);
    //echo "</pre>";
    //comment the following out and see what happens!
    //return $all_widgets;
    //this should run on the homepage / frontpage only! you can use more conditions here
    if( is_front_page() || is_page() ){
		// match to your registered sidebar widget!!!
        foreach ($all_widgets['sidebar-1'] as $i => $inst){
            //check if the id for the widget to extract exists.
            $pos = strpos($inst, 'categories');
 
            if($pos !== false)
            {
                //remove the archives widget by unsetting it's id
                unset($all_widgets['sidebar-1'][$i]);
            }
        }
    }
//comment the following out and see what happens!
return $all_widgets;
}

To do: Target an array of widgets to exclude. Rewrite function to pass vars as arguments of function.

Published on April 13, 2012