Advertisement
natehawkes

RSS Widget

Jun 27th, 2014
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.68 KB | None | 0 0
  1. <?php
  2. add_action( 'widgets_init', 'wy_rss_widget' );
  3.  
  4. function wy_rss_widget() {
  5.     register_widget( 'WY_rss_widget' );
  6. }
  7.  
  8. class WY_rss_widget extends WP_Widget {
  9.  
  10.     function WY_rss_widget() {
  11.         $widget_ops = array( 'classname' => 'wy-rss', 'description' => __('A widget for displaying RSS feed', 'wy-rss') );
  12.         $control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'wy-rss-widget' );
  13.         $this->WP_Widget( 'wy-rss-widget', __('RSS Widget', 'wy-rss'), $widget_ops, $control_ops );
  14.     }
  15.    
  16.     function widget( $args, $instance ) {
  17.         extract( $args );
  18.  
  19.         //Our variables from the widget settings.
  20.         $title = apply_filters('widget_title', $instance['title'] );
  21.         $wy_rssURL = $instance['wy_rssURL'];
  22.         $wy_rssLimit = $instance['wy_rssLimit'];
  23.  
  24.         echo $before_widget;
  25.  
  26.         // Display the widget title
  27.         if ( $title ) {
  28.             echo $before_title . $title . $after_title;
  29.         }
  30.  
  31.         //Display the name
  32.         if ( ( $wy_rssURL != " " || $wy_rssURL != "FEED URL" ) && ( $wy_rssLimit != " " ) ) {
  33.             $rss = new DOMDocument();
  34.             $rss->load($wy_rssURL);
  35.             $feed = array();
  36.             foreach ($rss->getElementsByTagName('item') as $node) {
  37.                 $item = array (
  38.                     'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
  39.                     'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
  40.                     'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
  41.                     'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
  42.                 );
  43.                 array_push($feed, $item);
  44.             }
  45.             $limit = $wy_rssLimit;
  46.            
  47.             echo '<ul>';
  48.             echo '<li>'.$wy_rssURL.' ('.$limit.')</li>';
  49.             for($x=0;$x<$limit;$x++) {
  50.                 $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
  51.                 $link = $feed[$x]['link'];
  52.                 $description = $feed[$x]['desc'];
  53.                 $date = date('l F d, Y', strtotime($feed[$x]['date']));
  54.                
  55.                 echo '<li>';
  56.                 echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
  57.                 echo '<small><em>Posted on '.$date.'</em></small></p>';
  58.                 echo '<p>'.$description.'</p>';
  59.                 echo '</li>';
  60.             }
  61.             echo '</ul>';
  62.         }
  63.  
  64.         echo $after_widget;
  65.     }
  66.  
  67.     //Update the widget
  68.      
  69.     function update( $new_instance, $old_instance ) {
  70.         $instance = $old_instance;
  71.  
  72.         //Strip tags from title and name to remove HTML
  73.         $instance['title'] = strip_tags( $new_instance['title'] );
  74.         $instance['wy_rssURL'] = strip_tags( $new_instance['wy_rssURL'] );
  75.         $instance['wy_rssLimit'] = strip_tags( $new_instance['wy_rssLimit'] );
  76.  
  77.         return $instance;
  78.     }
  79.  
  80.    
  81.     function form( $instance ) {
  82.  
  83.         //Set up some default widget settings.
  84.         $defaults = array( 'title' => __('Title', 'wy-rss'), 'wy_rssURL' => __('FEED URL', 'wy-rss'), 'wy_rssLimit' => __('5','wy-rss') );
  85.         $instance = wp_parse_args( (array) $instance, $defaults ); ?>
  86.  
  87.         <p>
  88.             <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'wp-rss'); ?></label>
  89.             <input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:100%;" />
  90.         </p>
  91.  
  92.         <p>
  93.             <label for="<?php echo $this->get_field_id( 'wy_rssURL' ); ?>"><?php _e('URL:', 'wp-rss'); ?></label>
  94.             <input id="<?php echo $this->get_field_id( 'wy_rssURL' ); ?>" name="<?php echo $this->get_field_name( 'wy_rssURL' ); ?>" value="<?php echo $instance['wy_rssURL']; ?>" style="width:100%;" />
  95.         </p>
  96.  
  97.         <p>
  98.             <label for"<?php echo $this->get_field_id( 'wy_rssLimit' ); ?>"><?php _e('Number of items:', 'wp-rss'); ?></label>
  99.             <input id="<?php echo $this->get_field_id( 'wy_rssLimit' ); ?>" name="<?php echo $this->get_field_id( 'wy_rssLimit' ); ?>" value="<?php echo $instance['wy_rssLimit']; ?>" />
  100.         </p>
  101.  
  102.     <?php
  103.     }
  104. }
  105. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement