Here’s the code I used to automatically install three custom widgets to a new wordpress sidebar when a new wordpress theme is installed:
// widget code here if(!get_option('dtbaker_done_installed_widgets',false)){ $add_to_sidebar = 'sidebar-home'; $widget_name = 'homepagewidget'; $sidebar_options = get_option('sidebars_widgets'); if(!isset($sidebar_options[$add_to_sidebar])){ $sidebar_options[$add_to_sidebar] = array('_multiwidget'=>1); } $homepagewidget = get_option('widget_'.$widget_name); if(!is_array($homepagewidget))$homepagewidget = array(); $count = count($homepagewidget)+1; // add first widget to sidebar: $sidebar_options[$add_to_sidebar][] = $widget_name.'-'.$count; $homepagewidget[$count] = array( 'title' => 'Like this template?', 'text' => 'Purchase this theme on ThemeForest here.', 'link_page' => '', 'link_url' => 'http://themeforest.net/?ref=dtbaker', 'image' => 'wp-content/themes/world-wide-webdesign/images/icon_tag.png', ); $count++; // add second widget to sidebar: $sidebar_options[$add_to_sidebar][] = $widget_name.'-'.$count; $homepagewidget[$count] = array( 'title' => 'Testimonials', 'text' => 'Nunc auctor bibendum eros. Dnahs Nunc auctor bibendum eros.', 'link_page' => '', 'link_url' => '', 'image' => 'wp-content/themes/world-wide-webdesign/images/icon_chat.png', ); $count++; // add third widget to sidebar: $sidebar_options[$add_to_sidebar][] = $widget_name.'-'.$count; $homepagewidget[$count] = array( 'title' => 'Contact Us', 'text' => 'Nunc auctor bibendum eros. Dnahs Nunc auctor bibendum eros.', 'link_page' => '', 'link_url' => '', 'image' => 'wp-content/themes/world-wide-webdesign/images/icon_contact.png', ); $count++; update_option('sidebars_widgets',$sidebar_options); update_option('widget_'.$widget_name,$homepagewidget); update_option('dtbaker_done_installed_widgets',true); }
The result, 3 custom “home page” widgets on the right sidebar here: http://tf.dtbaker.com.au/template/www_wordpress/