Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm still quite a newbie developer so small problems like this bring me to a stand still from time to time.

I've basically built a custom plugin to display pdf images next to a link very basic I know, but my problem is I need the $title1 text to be a different colour on certain page templates so I'm trying to alter the output code with a conditional but it doesn't seem to be parsed in the browser.

the code now been parsed if the conditional only the .menu-widget div gets output on all page templates instead of .white replacing it allowing me to style it with css.

the IMAGES Constant is defined within functions.php file.

Here is my code

    class donor_pdf_plugin extends WP_Widget {

// Constructor
function donor_pdf_plugin() {
parent::WP_Widget(false, $name = __('Pdf footer description', 'donor_pdf_plugin'));
}

// Widget form creation
function form($instance) {

// Check values
if($instance) {
$link = esc_attr($instance['link']);
$link1 = esc_attr($instance['link1']);
$link2 = esc_attr($instance['link2']);
$link3 = esc_attr($instance['link3']);
$title = esc_attr($instance['title']);
$title1 = esc_attr($instance['title1']);
$title2 = esc_attr($instance['title2']);
$title3 = esc_attr($instance['title3']);
} else {
$link = '';
$link1 = '';
$link2 = '';
$link3 = '';
$title = '';
$title1 = '';
$title2 = '';
$title3 = '';
}
?>

       <p>
       <label for="<?php echo $this->get_field_id('link'); ?>"><?php _e('Menu link', 'wp_widget_plugin'); ?></label>
       <input class="widefat" id="<?php echo $this->get_field_id('link'); ?>" name="<?php echo $this->get_field_name('link'); ?>" type="text" value="<?php echo $link; ?>" />
       </p>
       <p>
       <label for="<?php echo $this->get_field_id('link1'); ?>"><?php _e('Menu link1', 'wp_widget_plugin'); ?></label>
       <input class="widefat" id="<?php echo $this->get_field_id('link1'); ?>" name="<?php echo $this->get_field_name('link1'); ?>" type="text" value="<?php echo $link1; ?>" />
       </p>
       <p>
       <label for="<?php echo $this->get_field_id('link2'); ?>"><?php _e('Menu link2', 'wp_widget_plugin'); ?></label>
       <input class="widefat" id="<?php echo $this->get_field_id('link2'); ?>" name="<?php echo $this->get_field_name('link2'); ?>" type="text" value="<?php echo $link2; ?>" />
       </p>
       <p>
       <label for="<?php echo $this->get_field_id('link3'); ?>"><?php _e('Menu link3', 'wp_widget_plugin'); ?></label>
       <input class="widefat" id="<?php echo $this->get_field_id('link3'); ?>" name="<?php echo $this->get_field_name('link3'); ?>" type="text" value="<?php echo $link3; ?>" />
       </p>
       <p>
       <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Menu title', 'wp_widget_plugin'); ?></label>
       <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
       </p>
       <p>
       <label for="<?php echo $this->get_field_id('title1'); ?>"><?php _e('Menu title1', 'wp_widget_plugin'); ?></label>
       <input class="widefat" id="<?php echo $this->get_field_id('title1'); ?>" name="<?php echo $this->get_field_name('title1'); ?>" type="text" value="<?php echo $title1; ?>" />
       </p>
       <p>
       <label for="<?php echo $this->get_field_id('title2'); ?>"><?php _e('Menu title2', 'wp_widget_plugin'); ?></label>
       <input class="widefat" id="<?php echo $this->get_field_id('title2'); ?>" name="<?php echo $this->get_field_name('title2'); ?>" type="text" value="<?php echo $title2; ?>" />
       </p>
       <p>
       <label for="<?php echo $this->get_field_id('title3'); ?>"><?php _e('Menu title3', 'wp_widget_plugin'); ?></label>
       <input class="widefat" id="<?php echo $this->get_field_id('title3'); ?>" name="<?php echo $this->get_field_name('title3'); ?>" type="text" value="<?php echo $title3; ?>" />
       </p>





      <?php // end of widget form creation ?> 
<?php
}

// Widget update
function update($new_instance, $old_instance) {
$instance = $old_instance;
// Fields
$instance['link'] = strip_tags($new_instance['link']);
$instance['link1'] = strip_tags($new_instance['link1']);
$instance['link2'] = strip_tags($new_instance['link2']);
$instance['link3'] = strip_tags($new_instance['link3']);
$instance['title'] = strip_tags($new_instance['title']);
$instance['title1'] = strip_tags($new_instance['title1']);
$instance['title2'] = strip_tags($new_instance['title2']);
$instance['title3'] = strip_tags($new_instance['title3']);

return $instance;
}

// Widget display
function widget($args, $instance) {
extract($args);
$link = $instance['link'];
$link1 = $instance['link1'];
$link2 = $instance['link2'];
$link3 = $instance['link3'];
$title = $instance['title'];
$title1 = $instance['title1'];
$title2 = $instance['title2'];
$title3 = $instance['title3'];



if(!is_home()) {
echo "<div class=\"menu-widget\">
<p><a href=\"$link\"><img src=\"" . IMAGES . "/pdf.png\"/><span class=\"pdf-description\"> $title</a></p>
<p><a href=\"$link1\"><img src=\"" . IMAGES . "/pdf.png\"/><span class=\"pdf-description\">$title1</span></a></p>
<p><a href=\"$link2\"><img src=\"" . IMAGES . "/pdf.png\"/><span class=\"pdf-description\">$title2</a></p>
<p><a href=\"$link3\"><img src=\"" . IMAGES . "/pdf.png\"/><span class=\"pdf-description\">$title3</a></p>
</div>";
} else {
echo "<div class=\"white\">
<p><a href=\"$link\"><img src=\"" . IMAGES . "/pdf.png\"/><span class=\"pdf-description\"> $title</a></p>
<p><a href=\"$link1\"><img src=\"" . IMAGES . "/pdf.png\"/><span class=\"pdf-description\">$title1</span></a></p>
<p><a href=\"$link2\"><img src=\"" . IMAGES . "/pdf.png\"/><span class=\"pdf-description\">$title2</a></p>
<p><a href=\"$link3\"><img src=\"" . IMAGES . "/pdf.png\"/><span class=\"pdf-description\">$title3</a></p>
</div>";

}
}
}


// Register widget
add_action('widgets_init', create_function('', 'return register_widget("donor_pdf_plugin");'));
?>

the code not been parsed is this bit.

} else {
echo "<div class=\"white\">
<p><a href=\"$link\"><img src=\"" . IMAGES . "/pdf.png\"/><span class=\"pdf-description\"> $title</a></p>
<p><a href=\"$link1\"><img src=\"" . IMAGES . "/pdf.png\"/><span class=\"pdf-description\">$title1</span></a></p>
<p><a href=\"$link2\"><img src=\"" . IMAGES . "/pdf.png\"/><span class=\"pdf-description\">$title2</a></p>
<p><a href=\"$link3\"><img src=\"" . IMAGES . "/pdf.png\"/><span class=\"pdf-description\">$title3</a></p>
</div>";

}
share|improve this question
    
What isn't being parsed? Be specific. Where is the IMAGES constant set? –  s_ha_dum Oct 3 '13 at 22:47
    
updated post - apologies. –  Dannyw24 Oct 3 '13 at 22:54
1  
Are you sure you need is_home() and not is_front_page() I.E. is your home page a static page? –  G. M. Oct 3 '13 at 23:28
    
Yeah my home page is a page template which is selected in the settings. –  Dannyw24 Oct 4 '13 at 22:22
    
@Dannyw24 so is_home can't work for you, as I guessed. –  G. M. Oct 4 '13 at 22:23
add comment

1 Answer

up vote 0 down vote accepted

You code is not wrong, probably the only problem is that you are using is_home() when you need is_front_page.

However, just for a tip, I want show how your code can be semplified a lot:

class donor_pdf_plugin extends WP_Widget {

  static $fields = array();

  // Constructor
  function donor_pdf_plugin() {
    self::$fields = array(
      'link' => __('Menu link', 'wp_widget_plugin'),
      'link1' => __('Menu link 1', 'wp_widget_plugin'),
      'link2' => __('Menu link 2', 'wp_widget_plugin'),
      'link3' => __('Menu link 3', 'wp_widget_plugin'),
      'title' => __('Menu title', 'wp_widget_plugin'),
      'title1' => __('Menu title 1', 'wp_widget_plugin'),
      'title2' => __('Menu title 2', 'wp_widget_plugin'),
      'title3' => __('Menu title 3', 'wp_widget_plugin'),
    );
    parent::WP_Widget(false, $name = __('Pdf footer description', 'donor_pdf_plugin'));
  }

  // Widget form creation
  function form($instance) {
    foreach ( self::$fields as $field => $label) {
      $value = isset($instance[$field]) ? esc_attr($instance[$field]) : '';
      $id = $this->get_field_id($field);
      printf( '<p><label for="%s">%s</label>', $id, esc_html($label) );
      $format = '<input class="widefat" id="%s" name="%s" type="text" value="%s"/></p>';
      printf( $format, $id, $this->get_field_name($field), esc_attr($value) );
    }
  }

  // Widget update
  function update($new_instance, $old_instance) {
    return array_map('sanitize_text_field', $new_instance);
  }

  // Widget display
  function widget($args, $instance) {
   $class = is_front_page() ? 'white' : 'menu-widget';
   printf('<div class="%s">', $class);
   $format = '<p><a href="%s"><img src="%s"/><span class="pdf-description">%s</a></p>';
   $icon = IMAGES . '/pdf.png';
   for ($i=0; $i<4; $i++) {
     $u = $i == 0 ? '' : $i;
     if ( empty($instance['link' . $u]) || empty($instance['title' . $u]) ) continue;
     printf($format, esc_url($instance['link' . $u]), $icon, $instance['title' . $u]);
   }
   echo '</div>';
  }

}
share|improve this answer
    
Thanks G.M, I read however that you should use is_home when its within a plugin this was directly on the codex - Thanks for updated code it looks alot better :) –  Dannyw24 Oct 4 '13 at 22:16
    
@Dannyw24 Absolutely No matter if in a plugin or elsewhere: is_home is true only if your home page is not setted as a static page. is_front_page is true when home page is setted as a static page and when is setted to display last posts. So if you want to be sure something happen on site home page you have to use is_front_page, not is_home. Read Codex –  G. M. Oct 4 '13 at 22:22
    
thanks that makes sense to me now. I'm still going through the code you've added quite new to the printf function but I understand the concept –  Dannyw24 Oct 4 '13 at 22:29
    
@Dannyw24 there was an error. Try now, I've updated code. –  G. M. Oct 4 '13 at 22:58
    
when I add the php tags it brings up a server error. I'll try turning on debuging to find the error line. –  Dannyw24 Oct 4 '13 at 22:59
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.