Note that there are some explanatory texts on larger screens.

plurals
  1. POProblems with if/else logic in Wordpress widget
    primarykey
    data
    text
    <p>I'm working on a customised wordpress widget which displays some social links if the widget text fields are completed:</p> <pre><code>if (!empty($fss_fb_icon) &amp;&amp; !empty($fss_fb_link) &amp;&amp; !empty($fss_fb_text)) { echo "nothing"; } else { echo "&lt;li class='social-new'&gt;&lt;img src='/images/social-media/16px/".$fss_fb_icon.".png' alt='".$fss_fb_text."'/&gt;&lt;a href='".$fss_fb_link."' target='_blank'&gt;".$fss_fb_text."&lt;/a&gt;&lt;/li&gt;"; } </code></pre> <p>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</p> <pre><code>class FSSSocialWidget extends WP_Widget { function FSSSocialWidget() { $widget_ops = array('classname' =&gt; 'FSSSocialWidget', 'description' =&gt; 'Displays social media links in the footer' ); $this-&gt;WP_Widget('FSSSocialWidget', 'FSS Social Media Footer', $widget_ops); } function form($instance) { $instance = wp_parse_args( (array) $instance, array( 'title' =&gt; '' ) ); $title = $instance['title']; $fss_fb_icon = $instance['fss_fb_icon']; $fss_fb_link = $instance['fss_fb_link']; $fss_fb_text = $instance['fss_fb_text']; ?&gt; &lt;p&gt;&lt;label for="&lt;?php echo $this-&gt;get_field_id('title'); ?&gt;"&gt;Title: &lt;input class="widefat" id="&lt;?php echo $this-&gt;get_field_id('title'); ?&gt;" name="&lt;?php echo $this-&gt;get_field_name('title'); ?&gt;" type="text" value="&lt;?php echo attribute_escape($title); ?&gt;" /&gt;&lt;/label&gt;&lt;/p&gt; &lt;p&gt;&lt;label for="&lt;?php echo $this-&gt;get_field_id('fss_fb_icon'); ?&gt;"&gt;Facebook Icon Name: &lt;input class="widefat" id="&lt;?php echo $this-&gt;get_field_id('fss_fb_icon'); ?&gt;" name="&lt;?php echo $this-&gt;get_field_name('fss_fb_icon'); ?&gt;" type="text" value="&lt;?php echo attribute_escape($fss_fb_icon); ?&gt;" /&gt;&lt;/label&gt;&lt;/p&gt; &lt;p&gt;&lt;label for="&lt;?php echo $this-&gt;get_field_id('fss_fb_link'); ?&gt;"&gt;Facebook URL: &lt;input class="widefat" id="&lt;?php echo $this-&gt;get_field_id('fss_fb_link'); ?&gt;" name="&lt;?php echo $this-&gt;get_field_name('fss_fb_link'); ?&gt;" type="text" value="&lt;?php echo attribute_escape($fss_fb_link); ?&gt;" /&gt;&lt;/label&gt;&lt;/p&gt; &lt;p&gt;&lt;label for="&lt;?php echo $this-&gt;get_field_id('fss_fb_text'); ?&gt;"&gt;Facebook Link Text: &lt;input class="widefat" id="&lt;?php echo $this-&gt;get_field_id('fss_fb_text'); ?&gt;" name="&lt;?php echo $this-&gt;get_field_name('fss_fb_text'); ?&gt;" type="text" value="&lt;?php echo attribute_escape($fss_fb_text); ?&gt;" /&gt;&lt;/label&gt;&lt;/p&gt; &lt;?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 "&lt;ul id='footer-social-new'&gt;"; if (!empty($fss_fb_icon) &amp;&amp; !empty($fss_fb_link) &amp;&amp; !empty($fss_fb_text)) { echo "nothing"; } else { echo "&lt;li class='social-new'&gt;&lt;img src='/images/social-media/16px/".$fss_fb_icon.".png' alt='".$fss_fb_text."'/&gt;&lt;a href='".$fss_fb_link."' target='_blank'&gt;".$fss_fb_text."&lt;/a&gt;&lt;/li&gt;"; } echo "&lt;/ul&gt;"; echo $after_widget; } } add_action( 'widgets_init', create_function('', 'return register_widget("FSSSocialWidget");') ); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload