Title: Hide empty widget
Last modified: August 21, 2016

---

# Hide empty widget

 *  Resolved [Stagger Lee](https://wordpress.org/support/users/stagger-lee/)
 * (@stagger-lee)
 * [12 years, 6 months ago](https://wordpress.org/support/topic/hide-empty-widget/)
 * First i thought it was my theme, but it is the same problem even with one of 
   the default WP themes.
 * I searched for 1-2 days and found nothing.
 * Situation is very simple.
    – Use Text widget with PHP support – Put some PHP 
   code inside to display dynamically some content from Post you are viewing – If
   there is no content widget display header, widget title and empty box beneath.–
   In source code there is absolutely nothing, not even non-braking empty space.
   Only few pure div classes with nothing inside.
 * Now, such problem i fixed with CSS3 and hide empty DIVs, but i need and have 
   to have widget title, and it is making it impossible to hide with CSS3.
 * Want to ask is it normal behaviour of WP ?
    And is there some code snippet to
   fix that ?
 * Can say that it has nothing to do with PHP code inside widget. Tested with pure
   default text widget and empty widget shows.

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Thread Starter [Stagger Lee](https://wordpress.org/support/users/stagger-lee/)
 * (@stagger-lee)
 * [12 years, 6 months ago](https://wordpress.org/support/topic/hide-empty-widget/#post-4378332)
 * This one is quick and easy solution. But depending of subpage load widget can
   shows sometimes for a brief time.
 *     ```
       $(function() {
           $('.textwidget').each(function() {
               if ($.trim($(this).html()) == '')
                   $(this).parents('#text-15').remove();
           });
       });
       ```
   
 *  Thread Starter [Stagger Lee](https://wordpress.org/support/users/stagger-lee/)
 * (@stagger-lee)
 * [12 years, 3 months ago](https://wordpress.org/support/topic/hide-empty-widget/#post-4378544)
 * Small update, it it can help someone. Put this code in functions.php and use 
   Discreet Text Widget instead of standard Text Widget.
 *     ```
       class HackadelicDiscreetTextWidget extends WP_Widget_Text
       {
       	function HackadelicDiscreetTextWidget() {
       		$widget_ops = array('classname' => 'discreet_text_widget', 'description' => __('Arbitrary text or HTML, only shown if not empty'));
       		$control_ops = array('width' => 400, 'height' => 350);
       		$this->WP_Widget('discrete_text', __('Discreet Text'), $widget_ops, $control_ops);
       	}
   
       	function widget( $args, $instance ) {
       		extract($args, EXTR_SKIP);
       		$text = apply_filters( 'widget_text', $instance['text'] );
       		if (empty($text)) return;
       		$title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title']);
       		echo $before_widget;
       		if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?>
       			<div class="textwidget"><?php echo $instance['filter'] ? wpautop($text) : $text; ?></div>
       		<?php
       		echo $after_widget;
       	}
       }
   
       add_action('widgets_init', create_function('', 'return register_widget("HackadelicDiscreetTextWidget");'));
       ```
   
 *  [Tony Hayes](https://wordpress.org/support/users/majick/)
 * (@majick)
 * [12 years, 2 months ago](https://wordpress.org/support/topic/hide-empty-widget/#post-4378546)
 * Awesome, just what I was looking for… thank you!
 * didn’t work with the create_function though, I just got an error of “class ‘WP_Widget_Text’
   not found.” … seems it is trying to extend the class before it exists this way…
 * so I just wrapped the class in a function and worked fine.
 *     ```
       function CreateHackadelicDiscreetTextWidget() {
       class HackadelicDiscreetTextWidget extends WP_Widget_Text
       {
       	function HackadelicDiscreetTextWidget() {
       		$widget_ops = array('classname' => 'discreet_text_widget', 'description' => __('Arbitrary text or HTML, only shown if not empty'));
       		$control_ops = array('width' => 400, 'height' => 350);
       		$this->WP_Widget('discrete_text', __('Discreet Text'), $widget_ops, $control_ops);
       	}
   
       	function widget( $args, $instance ) {
       		extract($args, EXTR_SKIP);
       		$text = apply_filters( 'widget_text', $instance['text'] );
       		if (empty($text)) return;
       		$title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title']);
       		echo $before_widget;
       		if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?>
       			<div class="textwidget"><?php echo $instance['filter'] ? wpautop($text) : $text; ?></div>
       		<?php
       		echo $after_widget;
       	}
       }
       return register_widget("HackadelicDiscreetTextWidget");
       }
   
       add_action('widgets_init', 'CreateHackadelicDiscreetTextWidget');
       ```
   
 * Seems to me that WordPress could do this itself in not displaying all empty widgets
   anyway, not just text ones… ah well.
 *  Thread Starter [Stagger Lee](https://wordpress.org/support/users/stagger-lee/)
 * (@stagger-lee)
 * [12 years, 2 months ago](https://wordpress.org/support/topic/hide-empty-widget/#post-4378547)
 * Thanks.
    Why it is not in the WP core ? Human logic is “you dont have use of 
   empty something, or nothing”.
 * Almost like Ford public presentation with new car, and they say it is 100% guaranteed
   you will never drive this car, nor this car can have engine.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Hide empty widget’ is closed to new replies.

 * 4 replies
 * 2 participants
 * Last reply from: [Stagger Lee](https://wordpress.org/support/users/stagger-lee/)
 * Last activity: [12 years, 2 months ago](https://wordpress.org/support/topic/hide-empty-widget/#post-4378547)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
