• Resolved Stagger Lee

    (@stagger-lee)


    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

    (@stagger-lee)

    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

    (@stagger-lee)

    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.

    Thread Starter Stagger Lee

    (@stagger-lee)

    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.