Sanford
Sanford

Reputation: 91

Wordpress: !empty($title) returns 1 when $title is empty

I am working on revamping a site and discovered a strange quirk.

In the widget function (found in default-functions.php) code, there is a line that says:

if ( !empty( $title ) ) { echo $before_title . '[' . $title . '] ' . $after_title; }

function widget( $args, $instance ) {
        extract($args);
        $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
        $text = apply_filters( 'widget_text', empty( $instance['text'] ) ? '' : $instance['text'], $instance );
        echo $before_widget;
        if ( !empty( $title ) ) { echo $before_title . '[' . $title . '] ' . $after_title; } ?>
            <div class="textwidget"><?php echo !empty( $instance['filter'] ) ? wpautop( $text ) : $text; ?></div>
        <?php
        echo $after_widget;
    }

The boolean at the !empty($title) is supposed to pass a 0 if the $title has no value, but for some reason, it returns a 1. I have looked in the database and everything and can not ascertain what is the challenge.

(I added the brackets to test the issue)

Additional debugging:

Any ideas friends? I do have PHP Text Widget installed, but it is using the stock textwidget codebase.

Upvotes: 1

Views: 1266

Answers (3)

Sanford
Sanford

Reputation: 91

Found the problem. Or I should say @Otto found the solution.

It was here: https://core.trac.wordpress.org/ticket/21430

He helped me look at the $wp_filter and discover that Elegant Themes (the theme producer) added a space into the $title for their own theme.

Upvotes: 0

dweeves
dweeves

Reputation: 5605

you may check what apply_filters does, but i'm quite sure in my WP related memories that it adds some html around the value it is passed.

$instance['title'] is the value of the title of the post that comes from DB.

$title however , is the result of apply_filters call some lines before your test.

So you have to test against $instance['title'] rather than $title

Upvotes: 1

Ben Overmyer
Ben Overmyer

Reputation: 302

If the string checked by empty() has whitespace, then it returns false. You'll need to use something like $trimmedTitle = trim($title); if(empty($trimmedTitle)) { doStuff(); } to get rid of the whitespace.

Upvotes: 0

Related Questions