Skip to content

Instantly share code, notes, and snippets.

@arioch1984
Forked from nathanielks/add-to-sidebar.php
Last active August 29, 2015 14:15
Show Gist options
  • Save arioch1984/14e5549f5f26ac862adb to your computer and use it in GitHub Desktop.
Save arioch1984/14e5549f5f26ac862adb to your computer and use it in GitHub Desktop.
WP - Add to Sidebar
<?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