Skip to content

Instantly share code, notes, and snippets.

@mo3aser
Created March 19, 2016 18:34
Show Gist options
  • Save mo3aser/285b6aed25e924fe96ca to your computer and use it in GitHub Desktop.
Save mo3aser/285b6aed25e924fe96ca to your computer and use it in GitHub Desktop.
function tie_addWidgetToSidebar($sidebarSlug, $widgetSlug, $countMod, $widgetSettings = array()){
$sidebarOptions = get_option('sidebars_widgets');
if(!isset($sidebarOptions[$sidebarSlug])){
$sidebarOptions[$sidebarSlug] = array('_multiwidget' => 1);
}
$newWidget = get_option('widget_'.$widgetSlug);
if(!is_array($newWidget))$newWidget = array();
$count = count($newWidget)+1+$countMod;
$sidebarOptions[$sidebarSlug][] = $widgetSlug.'-'.$count;
$newWidget[$count] = $widgetSettings;
update_option('sidebars_widgets', $sidebarOptions);
update_option('widget_'.$widgetSlug, $newWidget);
}
//empty the widget field
update_option('sidebars_widgets', '');
// add a new widget
tie_addWidgetToSidebar( sidebar name , widget name , $widget_settings() );
tie_addWidgetToSidebar( 'primary-widget-area' , 'facebook-widget', 0, array('title' => 'Find us on Facebook', 'page_url' => 'https://www.facebook.com/TieLabs'));
tie_addWidgetToSidebar( 'shop-widget-area' , 'woocommerce_layered_nav', 0, array('title' => 'Filter by'));
tie_addWidgetToSidebar( 'third-footer-widget-area' , 'posts-list-widget', 0, array('title' => 'Latest Posts', 'no_of_posts' => 5, 'thumb' => true , 'posts_order' => 'latest'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment