Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@nathanielks
Created April 26, 2013 18:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nathanielks/5469306 to your computer and use it in GitHub Desktop.
Save nathanielks/5469306 to your computer and use it in GitHub Desktop.
These functions will programmatically add widgets to a specified sidebar. This can be useful when a theme is first installed to fill specific sidebars with widgets you're written for your theme. $add_to_sidebar takes the id of the sidebar you wish to add to. $widgets takes the id's of the widgets you wish to add.
<?php
function cur_add_widget_to_sidebar( $widget_name, $add_to_sidebar, &$sidebar_options ){
$widget = get_option('widget_'.$widget_name);
if(!is_array($widget))$widget = array();
$count = count($widget)+1;
$sidebar_options[$add_to_sidebar][] = $widget_name.'-'.$count;
$widget[$count] = array();
update_option('widget_'.$widget_name,$widget);
}
function cur_add_widgets_to_sidebar($add_to_sidebar = '', $widgets = array() ){
$sidebar_options = get_option('sidebars_widgets');
if(!isset($sidebar_options[$add_to_sidebar])){
$sidebar_options[$add_to_sidebar] = array('_multiwidget'=>1);
}
foreach( $widgets as $widget_name ) :
cur_add_widget_to_sidebar( $widget_name, $add_to_sidebar, $sidebar_options );
endforeach;
update_option('sidebars_widgets',$sidebar_options);
}
<?php
add_action('after_setup_theme', 'cur_setup_theme');
function cur_setup_theme(){
if(!get_option('cur_widgets_installed',false)){
$add_to_sidebar = 'primary-navigation';
// $widgets is an array of widget id's
$widgets = array(
'search',
);
cur_add_widgets_to_sidebar( $add_to_sidebar, $widgets );
update_option('cur_widgets_installed',true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment