WordPress.org

Ready to get started?Download WordPress

Forums

Widget code not working (1 post)

  1. Robert Heller
    Member
    Posted 2 years ago #

    I just wrote a new widget, but it is not working. I have written a bunch of widgets, but this one does not work and I cannot figure out why. The widget is part of a custom theme I am working on. The theme itself works fine, as do the other widgets I wrote with this theme. I wanted to add an additional widget to the theme. What happens is this:

    The new theme installs and activates properly -- no errors.

    The new widget shows up on the widgets page, so far so good.

    I drag the widget to a side bar, that works just fine.

    I set the widgets parameters and then click save. That *seems* to work.

    I visit the site. The site displays fine, but the widget does not.

    (head scratching, hair pulling...)

    Using Firebug I discover something odd. The POST for this new widget has widget_number set to -1. *NONE OF THE OTHER WIDGETS* have this setting. Also, there is no RESPONSE -- all other widgets have a RESPONSE from admin_ajax.php. WHAT IS GOING ON HERE?

    Here is the code:

    class Deepwoods_GoogleAdSense extends WP_Widget {
        function __construct() {
          /* Widget settings. */
          $widget_ops = array( 'classname' => 'dwsgoogleadsense',
    			   'description' =>
    				__('Display Google Ad Sense Blocks',
    					'deepwoods') );
          $control_ops = array( 'width' => 300, 'height' => 350,
    			    'id_base' => 'deepwoods-adsense');
          /* Create the widget. */
          $this->WP_Widget( 'dwsgoogleadsense',
    			   __('Display Google Ad Sense Blocks', 'deepwoods'),
    			   $widget_ops, $control_ops );
        }
        /**
         * How to display the widget on the screen.
         */
        function widget( $args, $instance ) {
          extract( $args );
          /* Before widget (defined by themes). */
          echo $before_widget;
          $opts = get_option("deepsoft2_theme_options");
          ?><script type="text/javascript"><!--
    		google_ad_client = "<?php echo $opts['google_ad_client']; ?>";
    		google_ad_slot = "<?php echo $instance['google_ad_slot']; ?>";
    		google_ad_width = <?php echo $instance['google_ad_width']; ?>;
    		google_ad_height = <?php echo $instance['google_ad_height']; ?>;
    	//-->
    	</script>
    	<script type="text/javascript"
    		src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    	</script><?php
          /* After widget (defined by themes). */
          echo $after_widget;
        }
        static function shortcode ($atts, $content=null, $code="") {
          extract( shortcode_atts ( array(
    	'google_ad_slot' => '',
    	'google_ad_width' => 0,
    	'google_ad_height' => 0), $atts ) );
          $opts = get_option("deepsoft2_theme_options");
          $result  = '<script type="text/javascript"><!--
    			google_ad_client = "'.$opts['google_ad_client'].'";
    			google_ad_slot = "'.$google_ad_slot.'";
    			google_ad_width = '.$google_ad_width.';
    			google_ad_height = '.$google_ad_height.';
    		//-->
    		</script>
    		<script type="text/javascript"
    			src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    		</script>';
          return $result;
        }
        /**
         * Update the widget settings.
         */
        function update( $new_instance, $old_instance ) {
          $instance = $old_instance;
          $instance['google_ad_slot'] = $new_instance['google_ad_slot'];
          $instance['google_ad_width'] = $new_instance['google_ad_width'];
          $instance['google_ad_height'] = $new_instance['google_ad_height'];
          return $instance;
        }
        /**
         * Displays the widget settings controls on the widget panel.
         * Make use of the get_field_id() and get_field_name() function
         * when creating your form elements. This handles the confusing stuff.
         */
        function form( $instance ) {
          /* Set up some default widget settings. */
          $defaults = array( 'google_ad_slot' => "",
    			 'google_ad_width' => 0,
    			 'google_ad_height' => 0 );
          $instance = wp_parse_args( (array) $instance, $defaults ); ?>
          <p>
    	<label for="<?php echo $this->get_field_id( 'google_ad_slot' );
    	?>"><?php _e('Google Ad Slot','deepwoods'); ?></label>
    	<input id="<?php echo $this->get_field_id( 'google_ad_slot' ); ?>"
    	       value="<?php echo $instance['google_ad_slot']; ?>"
    	       name="<?php echo $this->get_field_name( 'google_ad_slot' ); ?>"
    	       style="width:100%;" />
          </p>
          <p>
    	<label for="<?php echo $this->get_field_id( 'google_ad_width' );
    	?>"><?php _e('Google Ad Width','deepwoods'); ?></label>
    	<input id="<?php echo $this->get_field_id( 'google_ad_width' ); ?>"
    	       value="<?php echo $instance['google_ad_width']; ?>"
    	       name="<?php echo $this->get_field_name( 'google_ad_width' ); ?>"
    	       style="width:100%;" />
          </p>
          <p>
    	<label for="<?php echo $this->get_field_id( 'google_ad_height' );
    	?>"><?php _e('Google Ad Height','deepwoods'); ?></label>
    	<input id="<?php echo $this->get_field_id( 'google_ad_height' ); ?>"
    	       value="<?php echo $instance['google_ad_height']; ?>"
    	       name="<?php echo $this->get_field_name( 'google_ad_height' ); ?>"
    	       style="width:100%;" />
          </p>
          <?php
        }
      }
    
      register_widget('Deepwoods_GoogleAdSense');
    
      add_shortcode('DWS_AdSense',array('Deepwoods_GoogleAdSense','shortcode'));

    What am I missing? I have been staring at the code for some time and I am not seeing an error. Maybe a fresh set of eyeballs will spot some silly mistake.

Topic Closed

This topic has been closed to new replies.

About this Topic