I'm working on a customised wordpress widget which displays some social links if the widget text fields are completed:
if (!empty($fss_fb_icon) && !empty($fss_fb_link) && !empty($fss_fb_text)) {
echo "nothing";
} else {
echo "<li class='social-new'><img src='/images/social-media/16px/".$fss_fb_icon.".png' alt='".$fss_fb_text."'/><a href='".$fss_fb_link."' target='_blank'>".$fss_fb_text."</a></li>";
}
Problem is this test always seems to return the else line of code, no matter the state of the widget's fields. I'm not certain if I've made a mistake in the php syntax or if there is some oddity of wordpress widgets I'm unaware of. Full code follows
class FSSSocialWidget extends WP_Widget
{
function FSSSocialWidget()
{
$widget_ops = array('classname' => 'FSSSocialWidget', 'description' => 'Displays social media links in the footer' );
$this->WP_Widget('FSSSocialWidget', 'FSS Social Media Footer', $widget_ops);
}
function form($instance)
{
$instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
$title = $instance['title'];
$fss_fb_icon = $instance['fss_fb_icon'];
$fss_fb_link = $instance['fss_fb_link'];
$fss_fb_text = $instance['fss_fb_text'];
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>">Title: <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id('fss_fb_icon'); ?>">Facebook Icon Name: <input class="widefat" id="<?php echo $this->get_field_id('fss_fb_icon'); ?>" name="<?php echo $this->get_field_name('fss_fb_icon'); ?>" type="text" value="<?php echo attribute_escape($fss_fb_icon); ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id('fss_fb_link'); ?>">Facebook URL: <input class="widefat" id="<?php echo $this->get_field_id('fss_fb_link'); ?>" name="<?php echo $this->get_field_name('fss_fb_link'); ?>" type="text" value="<?php echo attribute_escape($fss_fb_link); ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id('fss_fb_text'); ?>">Facebook Link Text: <input class="widefat" id="<?php echo $this->get_field_id('fss_fb_text'); ?>" name="<?php echo $this->get_field_name('fss_fb_text'); ?>" type="text" value="<?php echo attribute_escape($fss_fb_text); ?>" /></label></p>
<?php
}
function update($new_instance, $old_instance)
{
$instance = $old_instance;
$instance['title'] = $new_instance['title'];
$instance['fss_fb_icon'] = $new_instance['fss_fb_icon'];
$instance['fss_fb_link'] = $new_instance['fss_fb_link'];
$instance['fss_fb_text'] = $new_instance['fss_fb_text'];
return $instance;
}
function widget($args, $instance)
{
extract($args, EXTR_SKIP);
/* User-selected settings. */
$fss_fb_icon = $instance['fss_fb_icon'];
$fss_fb_link = $instance['fss_fb_link'];
$fss_fb_text = $instance['fss_fb_text'];
echo $before_widget;
$title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']);
if (!empty($title))
echo $before_title . $title . $after_title;;
// WIDGET CODE GOES HERE
echo "<ul id='footer-social-new'>";
if (!empty($fss_fb_icon) && !empty($fss_fb_link) && !empty($fss_fb_text)) {
echo "nothing";
} else {
echo "<li class='social-new'><img src='/images/social-media/16px/".$fss_fb_icon.".png' alt='".$fss_fb_text."'/><a href='".$fss_fb_link."' target='_blank'>".$fss_fb_text."</a></li>";
}
echo "</ul>";
echo $after_widget;
}
}
add_action( 'widgets_init', create_function('', 'return register_widget("FSSSocialWidget");') );
You are missing two !
operators and you have the if/else blocks the wrong way around:
if (!empty($fss_fb_icon) && !empty($fss_fb_link) && !empty($fss_fb_text))) {
echo "<li class='social-new'>...etc...";
} else {
echo "nothing";
}
You need to delete $title = empty($instance['title']) ? ' '
this white space ' '
to ''
. if not your function not be empty.