Last active
October 7, 2015 05:08
-
-
Save markoheijnen/3110599 to your computer and use it in GitHub Desktop.
Register widgets to a sidebar by code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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