I’m working on a WordPress plugin. I can add my three custom widgets and a dynamic sidebar to the interface. I’m trying to set it up to load some default widgets into the sidebar if the user hasn’t set up any.
function rs_sidebar_setup() {
// register sidebar
$rs_sidebar_opts = array(
'name' => __('Social Media Updates'),
'id' => 'social-updates',
'description' => __('Displays recent posts from social media sites'),
'before_widget' => '',
'after_widget' => ''
);
register_sidebar($rs_sidebar_opts);
}
add_action('widgets_init', 'rs_sidebar_setup');
if ( !function_exists('rs_default_widgets') ) :
function rs_default_widgets() {
// get active widgets from database
$widgets = get_option('sidebars_widgets');
//if(empty($widgets['social-updates'])) {
// add new widgets to our sidebar if the sidebar is empty
//$widgets['sidebar-social-updates'] = array('calendar-3', 'rapidsocialfacebookwidget-1', 'rapidsocialfwitterwidget-1', 'rapidsociallinkedinwidget-1');
$widgets['social-updates'] = array('calendar-3');
update_option('sidebars_widgets', $widgets);
//}
print_r(get_option('sidebars_widgets'));
}
add_action('widgets_init', 'rs_default_widgets', 11);
endif;
In case it was a problem with my custom widgets, I tried using built-in widgets – still no good. The print_r
statement confirms that the database is changed, but I still don’t see a calendar widget in my Social Media Updates sidebar.
I tried using the same get_option
/update-option
technique with a sidebar built into the Twenty Eleven theme, and it worked, so I’m thinking it might be a problem with the load order, but I don’t know. Any thoughts?
For anyone who stumbles on this, there is a solution and very detailed writeup on the programmers SE here:
https://wordpress.stackexchange.com/questions/26557/programmatically-add-widgets-to-sidebars