public
Last active

Register widgets to a sidebar by code

  • Download Gist
gistfile1.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
<?php
 
class Rockstars_Widget {
private $custom_settings = array();
 
public function register_default_widget( $sidebar, $class_name, $settings ) {
global $_wp_sidebars_widgets, $wp_widget_factory;
 
if( empty( $_wp_sidebars_widgets[ $sidebar ] ) ) {
if( isset( $wp_widget_factory->widgets[ $class_name ] ) ) {
$class = $wp_widget_factory->widgets[ $class_name ];
 
if( ! isset( $this->custom_settings[ $class->option_name ] ) ) {
$this->custom_settings[ $class->option_name ] = array();
add_filter( 'option_' . $class->option_name, array( $this, '_register_default_widgets_settings' ) );
}
$id = 900 + count( $this->custom_settings[ $class->option_name ] );
$this->custom_settings[ $class->option_name ][ $id ] = $settings;
$_wp_sidebars_widgets[ $sidebar ][] = $class->id_base . '-' . $id;
 
return true;
}
}
 
return false;
}
 
function _register_default_widgets_settings( $value ) {
$filter = substr( current_filter(), 7);
 
if( isset( $this->custom_settings[ $filter ] ) )
$value = $value + $this->custom_settings[ $filter ];
 
return $value;
}
}
 
$GLOBALS['rockstars_widget'] = new Rockstars_Widget();
 
?>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.