0

I have coded a widget. In the admin area the user can enter the url for their youtube, facebook, twitter and google+. On the front end of the site in the browser, they can click on a logo for either of these 4, & it will take them to the social network. Logos for facebook, twitter & youtube are dispaying, but the youtube one isn't.

Please can you have a look at my code (below) and tell me why the youtube logo isn't being displayed? :

 class mp_example_widget02 extends WP_widget {

    function mp_example_widget02() {
    parent::WP_widget(false, $name = ' Follow us');
    }

    function widget($args, $instance) {
    extract( $args );

    $title = apply_filters('widget_title', $instance['title']);
    $address = $instance['address'];
    $email =  $instance['email'];
    $phone =  $instance['phone'];

    echo $before_widget;

    if ( $title ) {
    echo $before_title . $title . $after_title;  
    }

    if ( $address ) {
    echo  ' <div class="widget-textarea">' . $address . ' </div> '; 
    }

    if ( $email ) {
    echo '<a href="mailto:' .antispambot($email) .'"  > ' .$email . '</a><br />';
      }

    if ( $phone ) {
    if ( wp_is_mobile() ) {
    echo  "<a href='tel:$phone'  >$phone</a><br />";
    }else{
    echo '
         <div class="widget-textphone">' . $phone . ' </div> '; 
         }
         }


    echo $after_widget;
    }

     function update($new_instance, $old_instance) {
     $instance = $old_instance;
     $instance['title'] = strip_tags($new_instance['title']);
     $instance['address'] = strip_tags($new_instance['address']);
     $instance['email'] = ( is_email($new_instance['email']) ) ? $new_instance['email'] : '';
    $instance['phone'] = strip_tags($new_instance['phone']);



     return $instance;
     }

     function form($instance) {

     $title = esc_attr($instance['title']);
     $address = esc_attr($instance['address']);
     $email = esc_attr($instance['email']);
     $phone = esc_attr($instance['phone']);
     ?>

     <p>
     <label for="<?php echo $this->get_field_id('title'); ?> ">
     <?php _e('Title'); ?>  </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('address'); ?>"><?php _e('Address'); ?></label>

        <textarea class="widefat" id="<?php echo $this->get_field_id('address'); ?>" name="<?php echo $this->get_field_name('address'); ?>"><?php echo $address; ?></textarea>

    </p> 
    <br /> <br /> <p></p>

     <p>
    <label for="<?php echo  $this->get_field_id('email'); ?>">
     <?php _e('Email'); ?>  </label>
     <input class="widefat" id="<?php echo $this->get_field_id('email'); ?>" name="<?php echo $this->get_field_name('email'); ?>" type="email" value="<?php echo $email; ?> " />
    </p>


     <p> <label for="<?php echo $this->get_field_id('phone'); ?> ">
     <?php _e('Phone'); ?> </label>
     <input class="widefat" id="<?php echo $this->get_field_id('phone'); ?>" name="<?php echo $this->get_field_name('phone'); ?>" type="phone" value="<?php echo $phone; ?>" />

        </p>

    <?php
    }
    }

    add_action('widgets_init', create_function('', 'return register_widget("mp_example_widget");'));


class phone_social02 extends WP_Widget {

    function phone_social02() {
        parent::WP_Widget(false, $name = __('Share us', 'phone_social02') );
    }

function form($instance) {
if( $instance) {

     $email = esc_attr($instance['email']);
     $youtube = esc_attr($instance['youtube']);
     $facebook = esc_attr($instance['facebook']);
     $twitter = esc_attr($instance['twitter']);
     $gplus = esc_attr($instance['gplus']);
     } else {
     $title = '';
     $telephone = '';
     $email = '';
     $youtube = '';
     $facebook = '';
     $twitter = '';
     $gplus = '';
    }
?>

<p>
</p>
<p>
<label for="<?php echo $this->get_field_id('youtube'); ?>"><?php _e('Youtube:', 'wp_widget_plugin'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('youtube'); ?>" name="<?php echo $this->get_field_name('youtube'); ?>" type="text" value="<?php if(empty($youtube)) { echo "http://";} else { echo $youtube; } ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('facebook'); ?>"><?php _e('Facebook URL:', 'wp_widget_plugin'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('facebook'); ?>" name="<?php echo $this->get_field_name('facebook'); ?>" type="text" value="<?php if(empty($facebook)) { echo "http://";} else { echo $facebook; } ?>" />
</p>

<p>
<label for="<?php echo $this->get_field_id('twitter'); ?>"><?php _e('Twitter URL:', 'wp_widget_plugin'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('twitter'); ?>" name="<?php echo $this->get_field_name('twitter'); ?>" type="text" value="<?php if(empty($twitter)) { echo "http://";} else { echo $twitter; } ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('gplus'); ?>"><?php _e('Google Plus URL:', 'wp_widget_plugin'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('gplus'); ?>" name="<?php echo $this->get_field_name('gplus'); ?>" type="text" value="<?php if(empty($gplus)) { echo "http://";} else { echo $gplus; } ?>" />
</p>
<?php }



function update($new_instance, $old_instance) {
      $instance = $old_instance;

      $instance['email'] = strip_tags($new_instance['email']);
      $instance['youtube'] = strip_tags($new_instance['youtube']);
      $instance['facebook'] = strip_tags($new_instance['facebook']);
      $instance['twitter'] = strip_tags($new_instance['twitter']);
      $instance['gplus'] = strip_tags($new_instance['gplus']);

           return $instance;
}


function widget($args, $instance) {
   extract( $args );


   $email = $instance['email'];
   $facebook = $instance['youtube'];
   $facebook = $instance['facebook'];
   $twitter = $instance['twitter'];
   $gplus = $instance['gplus'];

   echo $before_widget;


      echo '<div id="phonesocialmain">';
      echo '<div id="socialdetmain">';


   echo'<p>Follow us: </p>';
   if( $youtube ) {
     echo '<a href="'.$youtube.'" target="_blank"> <img src="'. get_stylesheet_directory_uri() .'/images/you3.png" height="35" width="35" alt="youtube"></a>';
    }
    if ( $facebook ) {
   echo  '<a href="'.$facebook.'" target="_blank"><img src="'. get_stylesheet_directory_uri() .'/images/fb3.png" height="35" width="35" alt="facebook"></a>';  
   }
   if ( $twitter ) {
   echo  '<a href="'.$twitter.'" target="_blank"><img src="'. get_stylesheet_directory_uri ().'/images/twit3.png" height="35" width="35" alt="Twitter"></a>';  
   }
   if ( $gplus ) {
 echo  '<a href="'.$gplus.'" target="_blank"><img src="'. get_stylesheet_directory_uri() .'/images/go3.png" height="35" width="35" alt="gplus"></a>';  
   }
   echo  '</div>'; 

  echo '</div>'; 
    echo '</div>';
   echo $after_widget;
}
}   

add_action('widgets_init', create_function('', 'return register_widget("phone_social02");'));
0

You have a $facebook = $instance['youtube']; instead of $youtube = $instance['youtube'];

  • Thank-you for that. I have corrected that but it still doesn't seem to be displaying. – Leona grey Jan 13 '15 at 18:27
  • Then you're going to need to debug line by line, if you print_r($instance) is youtube in there? Does the image linked actually exist where it's supposed to? – Andrew Bartel Jan 13 '15 at 19:01
  • are you positive that images/you3.png exists? – karpstrucking Jan 13 '15 at 19:03
  • @karpstrucking yes it does, I've checked this. Thanks. – Leona grey Jan 15 '15 at 10:11
  • @AndrewBartel if I use print_r($instance); just before the last echo $after_widget; then youtube isn't in there. Thank-you for the support! – Leona grey Jan 15 '15 at 10:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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