• I created a custom plugin/widget following tutorial instruction. It shows up fine in control panel, but when I add it to the “Primary Widget Area”, it doesn’t show up. The rendering function “widget” is not being called. Is there any thing I need to do to enable it?

    class CustomWidget extends WP_Widget{
      /**
       * Declares our class.
       *
       */
      function CustomWidget(){
        $widget_ops = array('classname' => '_custom_widget', 'description' => __( "Custom Widget for Food") );
        $control_ops = array('width' => 250, 'height' => 400, 'id_base' => '-custom-widget');
        $this->WP_Widget('CustomWidget', ' Custom Widget', $widget_ops, $control_ops);
      }
    
      /**
       * Displays the Widget
       *
       */
      function widget($args, $instance){
          die;
        extract($args);
        $title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title']);
        $menu_order = $instance['menu_order'];
        $show_siblings = $instance['show_siblings'];
        $exclude = $instance['exclude'];
        echo $before_widget;
        echo $before_title;
        echo 'Title!!!';
        echo $after_title;
        echo 'Content!!!';
        echo $after_widget;
      }
    
      /**
       * Saves the widget's settings.
       *
       */
      function update($new_instance, $old_instance){
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        return $instance;
      }
    
      /**
       * Creates the edit form for the widget.
       *
       */
      function form($instance){
    
        //Defaults
        $instance = wp_parse_args( (array) $instance, array('title'=>'', 'lineOne'=>'Hello', 'lineTwo'=>'World') );
    
        $title = htmlspecialchars($instance['title']);
        $menu_order = htmlspecialchars($instance['menu_order']);
        $show_siblings = htmlspecialchars($instance['show_siblings']);
        $exclude = htmlspecialchars($instance['exclude']);
    
        echo 'No options yet<BR><BR>';
      }
    
    }
    
    add_action( 'widgets_init', create_function( '', 'register_widget("CustomWidget");' ) );
Viewing 6 replies - 1 through 6 (of 6 total)
  • There seems to be an errant die; at the beginning of the widget function.

    Thread Starter yongke

    (@yongke)

    Eh yea, I added it to see if it dies. It doesn’t. So the function isn’t being hit.

    I tracked the problem down to id-base in your control options. If your take out the hyphens ('id-base' => 'customwidget') or just leave out your custom control options all together:

    function CustomWidget() {
        $widget_ops = array( 'classname' => '_custom_widget', 'description' => 'Custom Widget for Food' );
    
        $this->WP_Widget( 'CustomWidget', ' Custom Widget', $widget_ops );
    }

    Then everything seems to be happy.

    Thread Starter yongke

    (@yongke)

    Thanks! I will check it out.

    Has anyone found a solution to this? It seems very bizarre but the widget() routine gets called AFTER the page had rendered in the browser. This seems to happen as a matter of course for widgets that work. widget() is called while the HTML is being returned to the browser and then once afterwards! For these widgets that don’t work there is one call made to widget() and it is after the page content has already been sent to the browser. Why do some widgets work and some not when, on the surface, there is no difference in the code structure?! Is this a WP bug and has it been reported?

    @os1:

    Since the OP hasn’t said anything in a couple weeks, I would assume his issue was resolved. The problem here was with what id_base was set to. It would be best to start another topic specific to your question(s).

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘"widget" function not being called’ is closed to new replies.