非常粗糙,部分cribbed from the Codex, 但这应该让你开始:
class My_SC_Widget extends WP_Widget {
private $shortcodes = array(
\'sc1\' => \'[sc1]\',
\'sc2\' => \'[sc2]\',
\'sc3\' => \'[sc3]\',
);
function __construct() {
// Instantiate the parent object
parent::__construct( false, \'My SC Widget\' );
}
function widget( $args, $instance ) {
if (isset($instance[\'my_shortcode_widgets\']) && array_key_exists($instance[\'my_shortcode_widgets\'],$this->shortcodes)) {
echo do_shortcode($this->shortcodes[$instance[\'my_shortcode_widgets\']]);
}
}
function update( $new_instance, $instance ) {
// Save widget options
if (isset($new_instance[\'my_shortcode_widgets\']) && array_key_exists($new_instance[\'my_shortcode_widgets\'],$this->shortcodes)) {
$instance[\'my_shortcode_widgets\'] = $new_instance[\'my_shortcode_widgets\'];
}
return $instance;
}
function form( $instance ) {
echo \'<select name="\'.$this->get_field_name(\'my_shortcode_widgets\').\'">\';
foreach ($this->shortcodes as $k => $v) {
echo \'<option value="\'.$k.\'" \'.selected( $instance[\'my_shortcode_widgets\'], $k ).\'>\'.$k.\'</option>\';
}
echo \'</select>\';
}
}
function myplugin_register_widgets() {
register_widget( \'My_SC_Widget\' );
}
add_action( \'widgets_init\', \'myplugin_register_widgets\' );