Add widget more than once
-
I wanna add this widget in two sidebars, but he desapear when I use once. What can I do?
-
Instead of using wp_register_sidebar_widget that can only be used once in exactly 1 of the sidebars, try to use Register Widget. It will give you a lot of work to change this but please try to change it in the next version Kieran O’Shea.
I find out an alternative way to add the widget more than once. For example, this is the code to add the widget once:
// The widget to show the mini calendar function widget_init_events_calendar() { // Check for required functions if (!function_exists('wp_register_sidebar_widget')) return; function widget_events_calendar($args) { extract($args); $the_title = stripslashes(get_option('events_calendar_widget_title')); $the_cats = stripslashes(get_option('events_calendar_widget_cats')); $widget_title = empty($the_title) ? __('Calendar','calendar') : $the_title; $the_events = minical($the_cats); if ($the_events != '') { echo $before_widget; echo $before_title . $widget_title . $after_title; echo $the_events; echo $after_widget; } } function widget_events_calendar_control() { $widget_title = stripslashes(get_option('events_calendar_widget_title')); $widget_cats = stripslashes(get_option('events_calendar_widget_cats')); if (isset($_POST['events_calendar_widget_title']) || isset($_POST['events_calendar_widget_cats'])) { update_option('events_calendar_widget_title',strip_tags($_POST['events_calendar_widget_title'])); update_option('events_calendar_widget_cats',strip_tags($_POST['events_calendar_widget_cats'])); } ?> <p> <label for="events_calendar_widget_title"><?php _e('Title','calendar'); ?>:<br /> <input class="widefat" type="text" id="events_calendar_widget_title" name="events_calendar_widget_title" value="<?php echo $widget_title; ?>"/></label> <label for="events_calendar_widget_cats"><?php _e('Comma separated category id list','calendar'); ?>:<br /> <input class="widefat" type="text" id="events_calendar_widget_cats" name="events_calendar_widget_cats" value="<?php echo $widget_cats; ?>"/></label> </p> <?php } //this widget can only be used once in exactly 1 of the sidebars wp_register_sidebar_widget('events_calendar',__('Calendar Events HomePage','calendar'),'widget_events_calendar',array('description'=>'A calendar of your events in the HomePage.')); wp_register_widget_control('events_calendar','events_calendar','widget_events_calendar_control'); }
So what you have to do is to reapeat the same code but changing the name of the functions and the name that is in the wp_register_sidebar_widget and wp_register_widget_control, for example:
// The widget to show todays events in the sidebar function widget_init_calendar_today() { // Check for required functions if (!function_exists('wp_register_sidebar_widget')) return; function widget_calendar_today($args) { extract($args); $the_title = stripslashes(get_option('events_calendar_widget_title')); $the_cats = stripslashes(get_option('events_calendar_widget_cats')); $widget_title = empty($the_title) ? __('Calendar','calendar') : $the_title; $the_events = minical($the_cats); if ($the_events != '') { echo $before_widget; echo $before_title . $widget_title . $after_title; echo $the_events; echo $after_widget; } } function widget_calendar_today_control() { $widget_title = stripslashes(get_option('events_calendar_widget_title')); $widget_cats = stripslashes(get_option('events_calendar_widget_cats')); if (isset($_POST['events_calendar_widget_title']) || isset($_POST['events_calendar_widget_cats'])) { update_option('events_calendar_widget_title',strip_tags($_POST['events_calendar_widget_title'])); update_option('events_calendar_widget_cats',strip_tags($_POST['events_calendar_widget_cats'])); } ?> <p> <label for="events_calendar_widget_title"><?php _e('Title','calendar'); ?>:<br /> <input class="widefat" type="text" id="events_calendar_widget_title" name="events_calendar_widget_title" value="<?php echo $widget_title; ?>"/></label> <label for="events_calendar_widget_cats"><?php _e('Comma separated category id list','calendar'); ?>:<br /> <input class="widefat" type="text" id="events_calendar_widget_cats" name="events_calendar_widget_cats" value="<?php echo $widget_cats; ?>"/></label> </p> <?php } wp_register_sidebar_widget('todays_events_calendar',__('Calendar Events Main','calendar'),'widget_calendar_today',array('description'=>'A list of your events today')); wp_register_widget_control('todays_events_calendar','todays_events_calendar','widget_calendar_today_control'); }
This is not the best way to do things, but it works. I expeact the next realease of plugin cames with multiple widget.
- The topic ‘Add widget more than once’ is closed to new replies.