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:
- I have turned off all of the plugins
- I have checked and the value of
''
is given to$title
- When I swap
empty( $instance['title'] )
forempty( $title )
, the code works (no titles show on the text widgets) - When I print out
$title
, I see a space. - On another instance of WP, I have no problem with this code.
Any ideas friends? I do have PHP Text Widget installed, but it is using the stock textwidget codebase.
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 ofapply_filters
call some lines before your test.So you have to test against
$instance['title']
rather than$title
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.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.