I have this scenario where I need to provide various single-use widgets.
They're all displayed the same way, so I don't feel the need for a different display functions for each widget. However, if I use the same callback on all wp_register_sidebar_widget
instances, only the last one is active.
I'm forced to have multiple identical functions, differing only the name. How can I improve it?
add_action( 'widgets_init', 'register_single_use_widgets' );
function register_single_use_widgets() {
$id = 'first';
$name = 'First Widget';
$widget_ops = array( 'description' => 'This is the first widget.' );
wp_register_sidebar_widget( $id, $name, 'single_widget_display', $widget_ops );
$id = 'second';
$name = 'Second Widget';
$widget_ops = array( 'description' => 'This is the second widget.' );
wp_register_sidebar_widget( $id, $name, 'single_widget_display2', $widget_ops );
$id = 'third';
$name = 'Third Widget';
$widget_ops = array( 'description' => 'This is the third widget.' );
wp_register_sidebar_widget( $id, $name, 'single_widget_display3', $widget_ops );
}
function single_widget_display( $args ) {
$name = $args['widget_name'];
echo "<p> $name </p>";
}
function single_widget_display2( $args ) {
$name = $args['widget_name'];
echo "<p> $name </p>";
}
function single_widget_display3( $args ) {
$name = $args['widget_name'];
echo "<p> $name </p>";
}
wp_register_sidebar_widget
is a very old function, and not how normal widgets are registered, where are your classes implementingWP_Widget
?