Forum at me and my mac » AdRotate

Code for unlimited widgets (use at own risk)

(6 posts)
  1. topsys
    Member

    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

    Posted 1 month ago #
  2. Arnan
    Kung-fu Jedi

    @Topsys, thanks for your efforts.

    If this works fine for people, and i hope they let me, us, know. I'm interested in including it in the plugin as a feature. I have looked at this before but couldn't quite get it to work the way i wanted it. And since most people need only 2 widgets i took the lazy way.

    Woohoo! Check out http://meandmymac.net
    Get my RSS feed! http://meandmymac.net/feed/
    Posted 1 month ago #
  3. topsys
    Member

    Hi Arnan,

    I am glad that you liked my proposal.

    I haven't fully tested it, but my few tests worked and lets see what others might say. There is of course the problem that it would limit backwards compatibility, but since wordpress has had so much focus on security, it would be understandable to limit compatibility to wp 2.8+.

    I also see your point in regards to only having 2 widgets, but as more and more themes are created with multiple widget ready areas, and wordpress is used more and more as a cms, multi widget functionality would help people with limited programing knowledge in adding and changing ads in any aspect of their site.

    Furthermore it will make it easier for theme developers in preparing for ad ready areas, by letting them use wordpress's built in sidebar functionality for widgets.

    At least these are my reason for adding multi widget functionality to adrotate.

    I as well hope that more users will share their opinion on the matter.

    Cheers,
    olaf

    Posted 1 month ago #
  4. Arnan
    Kung-fu Jedi

    I don't think setting a minimum required WP version is a bad thing and 2.8 is going back far enough.

    People should upgrade to the latest/near latest wp.

    Woohoo! Check out http://meandmymac.net
    Get my RSS feed! http://meandmymac.net/feed/
    Posted 1 month ago #
  5. marcbev
    Member

    Just wanted to let you know I am running WP2.9.1 with the Suffusion Theme ( 8 widget areas)and I tried modifying the files as indicated above and it's working fine for me. I am currently using an adrotate widget in the header, footer, right and left sidebars. All is fine and banners are being rotated. Now, this is a brand new site with very little traffic, but I will post back if I have any problems.
    Just saying,
    Marc

    Posted 1 month ago #
  6. Arnan
    Kung-fu Jedi

    Cheers, my tests are positive also. However, i have no "real" site to test it on. Meandmymac.net doesn't use widgets.
    As the code seems solid, for now i've included it for the next release.

    Woohoo! Check out http://meandmymac.net
    Get my RSS feed! http://meandmymac.net/feed/
    Posted 1 month ago #

RSS feed for this topic

Reply

You must log in to post.