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();
});
});
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");'));
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.
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.