0

I trying to insert a code into wp_footer whether the widget is present or not in the sidebar.

How to avoid the unnecessary code in the footer?

Add the code only if widget is present the sidebar.

 class CustomWidget extends WP_Widget {

   function CustomWidget() { 
     add_action( 'wp_footer', array($this,'footer'), 10, 1 );   
   }

   function widget($args, $instance) { }

   function update( $new_instance, $old_instance ) {}

   function form($instance) {}

   function footer {
      $all_widgets = $this->get_settings();

      foreach ($all_widgets as $key => $wpdcjqverticalmegamenu){

       $widget_id = $this->id_base . '-' . $key;

      if(is_active_widget(false, $widget_id, $this->id_base)){

      }     

   }

}

1 Answer 1

0

The constructor of widget is called when the widget is registered, so your add_action is called on (probably) every request. You can avoid that simply put the add_action inside widget() function, that is called only when the widget is printed so there is no need to check: just output what you want.

Also, if you need some context inside the footer method you can use a closure inside the widget method where you have easy access to $args and $instance variables (the first contain information about the sidebar and the widget id, the second contain informations on the fields setted for speficic widget):

class CustomWidget extends WP_Widget {

   function __construct() { 
      parent::__construct( 'my-widget-id', 'My Widget Name' );
   }

   function widget( $args, $instance ) {

    add_action( 'wp_footer', function() use ( $args, $instance ) {

      echo '<h1>' . $args['widget_id'] . '</h1>';
      echo '<pre>'; print_r( $instance ); echo '</pre>';
      echo '<pre>'; print_r( $args); echo '</pre>';
      echo '<hr>';

    }); 

   }

   function update( $new_instance, $old_instance ) {}

   function form($instance) {}

}

that makes the closure all the times the widget is added to page, i.e. if the widget is added 3 times, the closure will run 3 times.

If you want to prevent that use a static flag:

class CustomWidget extends WP_Widget {

   static $added = 0;

   function __construct() { 
      parent::__construct( 'my-widget-id', 'My Widget Name' );
   }

   function widget( $args, $instance ) {

    add_action( 'wp_footer', function() use ( $args, $instance ) {

      if ( static::$added !== 0 ) return; // run only once;
      static::$added++;

      echo '<h1>' . $args['widget_id'] . '</h1>';
      echo '<pre>'; print_r( $instance ); echo '</pre>';
      echo '<pre>'; print_r( $args); echo '</pre>';
      echo '<hr>';

    }); 

   }

   function update( $new_instance, $old_instance ) {}

   function form($instance) {}

}

PS: PHP 4 died a long time ago. Please use PHP 5 constructors...

8
  • Is it possible to save all the added widgets id in an array() instead of count. How to get the widget base id? Commented Mar 28, 2014 at 12:12
  • Why do you want to save that in an array? @BLPraveen
    – gmazzap
    Commented Mar 28, 2014 at 12:14
  • I have modified the footer function. you can check the current function which gets all the widget matching the baseid. Hence I asked if I can get the widget base ids stored in the array Commented Mar 28, 2014 at 12:19
  • I need to check type in the instance based on that I select differnt codes to insert Commented Mar 28, 2014 at 12:35
  • Is it possible to get the widget id? Commented Mar 28, 2014 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.