Hi Arnan,
first let me start by saying thank you for your awesome plugin.
The following piece of code is for those who would like to be able use an unlimited number of widgets and not be limited by the 2 default widgets that come with the plugin. Maybe in the future, this could be a starting point for implementing unlimited widgets in your plugin.
Note for people who might use this code
- this only works for wordpress 2.8+
- this has been limitedly tested, so might not work in some situations, use at own risk.
- this is not an official rewrite, so there will most likely not be any support from the plugin owner, use at own risk.
- MAKE A BACKUP OF THE FOLDER, SHOULD ANYTHING GO WRONG
- USE AT OWN RISK (just for those who didn't get that)
You will need to edit 2 files, adrotate-widget.php and adrotate.php.
adrotate-widget.php
Delete the 2 blocks containing adrotate_widget_init_1 and adrotate_widget_init_2 and add the following code instead,
/*-------------------------------------------------------------
Name: adrotate_widget_init
Purpose: Widget for the sidebar
Receive: -none-
Return: -none-
-------------------------------------------------------------*/
class WP_Widget_AdRotate extends WP_Widget {
function WP_Widget_AdRotate() {
$widget_ops = array('classname' => 'adrotate_widget', 'description' => "Add banners in the sidebar." );
$this->WP_Widget('adrotate', __('AdRotate'), $widget_ops);
}
function widget( $args, $instance ) {
extract( $args );
$title = apply_filters('widget_title', empty( $instance['title'] ) ? '' : $instance['title']);
echo $before_widget;
if ( $title )
echo $before_title . $title . $after_title;
echo adrotate_banner($instance['group'], $instance['banner'], $instance['block'], $instance['column'], false);
echo $after_widget;
}
function update( $new_instance, $old_instance ) {
$new_instance['title'] = strip_tags($new_instance['title']);
$new_instance['group'] = strip_tags($new_instance['group']);
$new_instance['block'] = strip_tags($new_instance['block']);
$new_instance['column'] = strip_tags($new_instance['column']);
$new_instance['banner'] = strip_tags($new_instance['banner']);
$instance=wp_parse_args($new_instance,$old_instance);
return $instance;
}
function form( $instance ) {
//Defaults
$defaults = array();
$instance = wp_parse_args( (array) $instance, $defaults );
extract($instance);
$title = esc_attr( $title );
$group = esc_attr( $group );
$block = esc_attr( $block );
$column = esc_attr( $column );
$banner = esc_attr( $banner );
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
<br />
<small><?php _e( 'HTML will be stripped out.' ); ?></small>
</p>
<p>
<label for="<?php echo $this->get_field_id('group'); ?>"><?php _e( 'Group:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('group'); ?>" name="<?php echo $this->get_field_name('group'); ?>" type="text" value="<?php echo $group; ?>" />
<br />
<small><?php _e( 'Group IDs. If multiple, separate them with commas (ie. 2,3,12,5).' ); ?></small>
</p>
<p>
<label for="<?php echo $this->get_field_id('banner'); ?>"><?php _e( 'Banner (Optional):' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('banner'); ?>" name="<?php echo $this->get_field_name('banner'); ?>" type="text" value="<?php echo $banner; ?>" />
<br />
<small><?php _e( 'Leave empty for multiple groups or when using a block! Do NOT enter multiple numbers here!' ); ?></small>
</p>
<p>
<label for="<?php echo $this->get_field_id('block'); ?>"><?php _e( 'Block (Optional):' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('block'); ?>" name="<?php echo $this->get_field_name('block'); ?>" type="text" value="<?php echo $block; ?>" />
<br />
<small><?php _e( 'Sets the amount of banners in a block.' ); ?></small>
</p>
<p>
<label for="<?php echo $this->get_field_id('column'); ?>"><?php _e( 'Columns (Optional):' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('column'); ?>" name="<?php echo $this->get_field_name('column'); ?>" type="text" value="<?php echo $column; ?>" />
<br />
<small><?php _e( 'Define how many columns your ad-block has.' ); ?></small>
</p>
<?php
}
}
function wp_adrotate_init() {
register_widget('WP_Widget_AdRotate');
}
add_action('widgets_init', 'wp_adrotate_init');
adrotate.php
Delete the following 2 lines,
Line 27 add_action('widgets_init', 'adrotate_widget_init_1');
Line 28 add_action('widgets_init', 'adrotate_widget_init_2');
That should do it.
If anyone has suggestions to improve the code, please share. These should not be feature requests.
Cheers,
olaf