[wp-trac] [WordPress Trac] #34995: WP_Widget::widget not called

WordPress Trac noreply at wordpress.org
Fri Dec 11 01:28:59 UTC 2015


#34995: WP_Widget::widget not called
--------------------------+------------------------------
 Reporter:  btwatts       |       Owner:
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  Awaiting Review
Component:  Widgets       |     Version:  4.4
 Severity:  normal        |  Resolution:
 Keywords:                |     Focuses:
--------------------------+------------------------------
Description changed by westonruter:

Old description:

> I have what I believe is a proper WP_Widget subclass that uses new style
> constructor.
>
> I started with the WPBeginner example: http://www.wpbeginner.com/wp-
> tutorials/how-to-create-a-custom-wordpress-widget/
>
> And worked through: http://stackoverflow.com/questions/23921729/how-to-
> create-a-custom-wordpress-widget-with-widget-options/34171737
>
> I also referenced the codex which suggests that everything I've got
> implemented is correct.
>
> When I run this code on WordPress 4.3 it works fine.  But when I upgrade
> to WordPress 4.4 the __constructor is correctly called, but
> WP_Widget::wiget is never called.
>
> I haven't debugged down into the core to see what changed.  I was hoping
> someone else has seen a problem similar and that we have a solution
> coming soon.
>
> The website that I am depending on this code for is
> http://www.excellhobby.com/
>
> I did a similar test on a brand new site that produced the same results
> (no plugins installed to conflict with the WP_Widget code).  ie., Install
> WordPress 4.3, install the plugin and theme that has modifications to
> allow topleftwidget.  With WordPress 4.3 it worked flawlessly.  But when
> I upgraded to WordPress 4.4 it stopped working.  All other variables are
> the same (same system, same PHP version, etc).
>
> Here is the code for my plugin which is slightly modified from the
> example:
>

> <?php
> /*
> Plugin Name: Byron Image Widget
> */
>
> //
> https://codex.wordpress.org/Widgetizing_Themes#How_to_Register_a_Widget_Area
>
> function biw_register_widget_area() {
>
> register_sidebar( array(
>                 'name' => __( 'Top Left', 'biw_text_domain'),
>                 'id' => 'topleftwidget',
>                 'description' => __( 'Top Left Widget',
> 'biw_text_domain'),
>                 'before_widget' => '<aside id="%1$s" class="widget
> %2$s">',
>                 'after_widget' => '</aside>',
>                 'before_title' => '<h3 class="widget-title">',
>                 'after_title' => '</h3>',
>         ) );
> }
>

> // Creating the widget
> class biw_topleft_widget extends WP_Widget {
>
>         /**
>          *  Register widget with WordPress.
>          */
>         function __construct() {
>                 parent::__construct(
>                         'biw_topleft_widget',                   // Base
> ID of widget
>                         __('Byron Image Widget', 'biw_text_domain'), //
> Widget name will appear in UI
>                         array( 'description' => __( 'Insert Images In
> Widget Areas', 'biw_text_domain' ), )     // Widget description
>                 );
>                 // This appears echo '<br/>Constructor Testing<br/>';
>         }
>
>         /**
>          * Front-end display of widget.
>          *
>          * @see WP_Widget::widget()
>          *
>          * @param array $args     Widget arguments.
>          * @param array $instance Saved values from database.
>          *
>          * Note:
>          * Creating widget front-end
>          * This is where the action happens
>          */
>         public function widget( $args, $instance ) {
>                 echo $args['before_widget'];
>
>                 //if ( ! empty( $instance['title'] ) ) {
>                 //      echo $args['before_title'] . apply_filters(
> 'widget_title', $instance['title'] ) . $args['after_title'];
>                 //}
>
>                 //echo '<br/>THIS IS THE TOP LEFT WIDGET<br/>';
>
>                 // This is where you run the code and display the output
>                 echo '<div align="center">';
>                 //Home Page
>                 if (is_front_page()) {
>                         echo '<img width="300" height="190"
> src="//excellhobby.com/wp-content/uploads/2015/04/Excell_Service_crop-
> 300x190.png">';
>                         echo '<br/>';
>                         echo __( 'Excellent Service and Support!',
> 'biw_text_domain' );
>                 } else
>                 if (is_page("about")) {
>                         //About Page
>                         echo '<img width="300" height="225"
> src="//excellhobby.com/wp-
> content/uploads/2014/07/Excell_Service_01-300x225.jpg">';
>                         echo '<br/>';
>                         echo __( 'Excelling at Service and Support!',
> 'biw_text_domain' );
>                 } else
>                 if (is_page("contact")) {
>                         //Contact Page
>                         echo '<img width="297" height="200"
> src="//excellhobby.com/wp-content/uploads/2015/04/Chris_2-297x300.png">';
>                         echo '<br/>';
>                         echo __( 'Contact Chris!', 'biw_text_domain' );
>                 } else
>                 if (is_page("about/current-projects")) {
>                         //CURRENT PROJECTS Page
>                         echo '<img width="300" height="225"
> src="//excellhobby.com/wp-
> content/uploads/2015/03/100_21601-300x225.jpg">';
>                         echo '<br/>';
>                         echo __( 'Get R/C Today!', 'biw_text_domain' );
>                 } else
>                 if (is_page("blog")) {
>                         //Blog Page
>                         echo '<img width="300" height="225"
> src="//excellhobby.com/wp-
> content/uploads/2014/07/Chris_Flying_09-300x225.jpg">';
>                         echo '<br/>';
>                         echo __( 'Keep In Touch.', 'biw_text_domain' );
>                 } else
> //              if (is_page("shop")) /* For some reason "shop" isn't
> matching.... */
>                 {
>                         //Shop Page
>                         echo '<img width="300" height="215"
> src="//excellhobby.com/wp-content/uploads/2015/07/Store_02_small.png">';
>                         echo '<br/>';
>                         echo __( 'You Deserve It.', 'biw_text_domain' );
>                 }
>                 echo '</div>';
>
>                 echo $args['after_widget'];
>         }
>
>         /**
>          * Back-end widget form.
>          *
>          * @see WP_Widget::form()
>          *
>          * @param array $instance Previously saved values from database.
>          *
>          * Note:
>          * This form doesn't do much because the args are hard coded in
> the plugin.
>          *
>          */
>         public function form( $instance ) {
>                 $title = ! empty( $instance['title'] ) ?
> $instance['title'] : __( 'New title', 'biw_text_domain' );
>                 // Widget admin form
> ?>
> <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 esc_attr( $title ); ?>" />
> </p>
> <?php
>         }
>
>         /**
>          * Sanitize widget form values before saving.
>          *
>          * @param array $new_instance Values just sent to be saved.
>          * @param array $old_instance Previously saved values from
> database.
>          *
>          * @return array Updated safe values to be saved.
>          */
>         public function update( $new_instance, $old_instance ) {
>                 $instance = array();
>                 $instance['title'] = ( ! empty( $new_instance['title'] )
> ) ? strip_tags( $new_instance['title'] ) : '';
>                 return $instance;
>         }
> } // Class biw_topleft_widget ends here
>
> // Register and load the widget
> function biw_load_widget() {
>         biw_register_widget_area();
>         register_widget( 'biw_topleft_widget' );
> }
> add_action( 'widgets_init', 'biw_load_widget' );
>
> ?>

New description:

 I have what I believe is a proper WP_Widget subclass that uses new style
 constructor.

 I started with the WPBeginner example: http://www.wpbeginner.com/wp-
 tutorials/how-to-create-a-custom-wordpress-widget/

 And worked through: http://stackoverflow.com/questions/23921729/how-to-
 create-a-custom-wordpress-widget-with-widget-options/34171737

 I also referenced the codex which suggests that everything I've got
 implemented is correct.

 When I run this code on WordPress 4.3 it works fine.  But when I upgrade
 to WordPress 4.4 the __constructor is correctly called, but
 WP_Widget::wiget is never called.

 I haven't debugged down into the core to see what changed.  I was hoping
 someone else has seen a problem similar and that we have a solution coming
 soon.

 The website that I am depending on this code for is
 http://www.excellhobby.com/

 I did a similar test on a brand new site that produced the same results
 (no plugins installed to conflict with the WP_Widget code).  ie., Install
 WordPress 4.3, install the plugin and theme that has modifications to
 allow topleftwidget.  With WordPress 4.3 it worked flawlessly.  But when I
 upgraded to WordPress 4.4 it stopped working.  All other variables are the
 same (same system, same PHP version, etc).

 Here is the code for my plugin which is slightly modified from the
 example:

 {{{#!php
 <?php
 /*
 Plugin Name: Byron Image Widget
 */

 //
 https://codex.wordpress.org/Widgetizing_Themes#How_to_Register_a_Widget_Area

 function biw_register_widget_area() {

 register_sidebar( array(
                 'name' => __( 'Top Left', 'biw_text_domain'),
                 'id' => 'topleftwidget',
                 'description' => __( 'Top Left Widget',
 'biw_text_domain'),
                 'before_widget' => '<aside id="%1$s" class="widget
 %2$s">',
                 'after_widget' => '</aside>',
                 'before_title' => '<h3 class="widget-title">',
                 'after_title' => '</h3>',
         ) );
 }


 // Creating the widget
 class biw_topleft_widget extends WP_Widget {

         /**
          *  Register widget with WordPress.
          */
         function __construct() {
                 parent::__construct(
                         'biw_topleft_widget',                   // Base ID
 of widget
                         __('Byron Image Widget', 'biw_text_domain'), //
 Widget name will appear in UI
                         array( 'description' => __( 'Insert Images In
 Widget Areas', 'biw_text_domain' ), )     // Widget description
                 );
                 // This appears echo '<br/>Constructor Testing<br/>';
         }

         /**
          * Front-end display of widget.
          *
          * @see WP_Widget::widget()
          *
          * @param array $args     Widget arguments.
          * @param array $instance Saved values from database.
          *
          * Note:
          * Creating widget front-end
          * This is where the action happens
          */
         public function widget( $args, $instance ) {
                 echo $args['before_widget'];

                 //if ( ! empty( $instance['title'] ) ) {
                 //      echo $args['before_title'] . apply_filters(
 'widget_title', $instance['title'] ) . $args['after_title'];
                 //}

                 //echo '<br/>THIS IS THE TOP LEFT WIDGET<br/>';

                 // This is where you run the code and display the output
                 echo '<div align="center">';
                 //Home Page
                 if (is_front_page()) {
                         echo '<img width="300" height="190"
 src="//excellhobby.com/wp-content/uploads/2015/04/Excell_Service_crop-
 300x190.png">';
                         echo '<br/>';
                         echo __( 'Excellent Service and Support!',
 'biw_text_domain' );
                 } else
                 if (is_page("about")) {
                         //About Page
                         echo '<img width="300" height="225"
 src="//excellhobby.com/wp-
 content/uploads/2014/07/Excell_Service_01-300x225.jpg">';
                         echo '<br/>';
                         echo __( 'Excelling at Service and Support!',
 'biw_text_domain' );
                 } else
                 if (is_page("contact")) {
                         //Contact Page
                         echo '<img width="297" height="200"
 src="//excellhobby.com/wp-content/uploads/2015/04/Chris_2-297x300.png">';
                         echo '<br/>';
                         echo __( 'Contact Chris!', 'biw_text_domain' );
                 } else
                 if (is_page("about/current-projects")) {
                         //CURRENT PROJECTS Page
                         echo '<img width="300" height="225"
 src="//excellhobby.com/wp-
 content/uploads/2015/03/100_21601-300x225.jpg">';
                         echo '<br/>';
                         echo __( 'Get R/C Today!', 'biw_text_domain' );
                 } else
                 if (is_page("blog")) {
                         //Blog Page
                         echo '<img width="300" height="225"
 src="//excellhobby.com/wp-
 content/uploads/2014/07/Chris_Flying_09-300x225.jpg">';
                         echo '<br/>';
                         echo __( 'Keep In Touch.', 'biw_text_domain' );
                 } else
 //              if (is_page("shop")) /* For some reason "shop" isn't
 matching.... */
                 {
                         //Shop Page
                         echo '<img width="300" height="215"
 src="//excellhobby.com/wp-content/uploads/2015/07/Store_02_small.png">';
                         echo '<br/>';
                         echo __( 'You Deserve It.', 'biw_text_domain' );
                 }
                 echo '</div>';

                 echo $args['after_widget'];
         }

         /**
          * Back-end widget form.
          *
          * @see WP_Widget::form()
          *
          * @param array $instance Previously saved values from database.
          *
          * Note:
          * This form doesn't do much because the args are hard coded in
 the plugin.
          *
          */
         public function form( $instance ) {
                 $title = ! empty( $instance['title'] ) ?
 $instance['title'] : __( 'New title', 'biw_text_domain' );
                 // Widget admin form
 ?>
 <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 esc_attr( $title ); ?>" />
 </p>
 <?php
         }

         /**
          * Sanitize widget form values before saving.
          *
          * @param array $new_instance Values just sent to be saved.
          * @param array $old_instance Previously saved values from
 database.
          *
          * @return array Updated safe values to be saved.
          */
         public function update( $new_instance, $old_instance ) {
                 $instance = array();
                 $instance['title'] = ( ! empty( $new_instance['title'] ) )
 ? strip_tags( $new_instance['title'] ) : '';
                 return $instance;
         }
 } // Class biw_topleft_widget ends here

 // Register and load the widget
 function biw_load_widget() {
         biw_register_widget_area();
         register_widget( 'biw_topleft_widget' );
 }
 add_action( 'widgets_init', 'biw_load_widget' );

 ?>
 }}}

--

--
Ticket URL: <https://core.trac.wordpress.org/ticket/34995#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list