Skip to content

Instantly share code, notes, and snippets.

@markoheijnen
Last active October 7, 2015 05:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markoheijnen/3110599 to your computer and use it in GitHub Desktop.
Save markoheijnen/3110599 to your computer and use it in GitHub Desktop.
Register widgets to a sidebar by code
<?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();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment