Created
June 16, 2015 08:59
-
-
Save davidvandenbor/7e6dc98cff421b7d8a88 to your computer and use it in GitHub Desktop.
Multiple RSS feeds in WordPress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Class to return multiple RSS feeds in WordPress | |
* Instructions: | |
* 1. Upload this class to theme folder | |
* 2. Include this class in functions.php ie: include "mytheme/wow_rss.php"; | |
* | |
* In theme template: | |
* 3. Create new object of class ie: $obj = new wow_Rss ("http://www.mywebsite.co.uk/feed","5"); | |
* Enter feed url as 1st argument and how many items from that feed as the 2nd | |
* 4. Display that object ie: echo '<ul class="myfeed">'.$obj.'</ul>'; | |
* Didn't include <ul> in class so that you can style the individual classes | |
* 5. Repeat 3 & 4 with other feeds | |
* | |
* Optional - You can limit the class to use on certain pages or posts using WP is_page(), is_post() etc | |
* Tips - Get it working first before editing display_rss(),make small changes to track errors easier | |
* | |
**/ | |
class wow_Rss | |
{ | |
public $feed = null; | |
public $url = null; | |
public $maxitems = null; | |
/** | |
* __construct | |
* | |
* pass values when class is istantiated | |
* | |
* @param string $url The URL You want to fetch | |
* @param numeric $maxitems How many items you want to fetch | |
*/ | |
public function __construct($url, $maxitems) | |
{ | |
$this->url = $url; | |
$this->maxitems = $maxitems; | |
} | |
/** | |
* get_first_image_url | |
* | |
* Looks for first image in string | |
* | |
* @param string $html | |
* @return string | |
*/ | |
public function get_first_image_url($html) | |
{ | |
if (preg_match('/<img.+?src="(.+?)"/', $html, $matches)) | |
{ | |
return $matches[1]; | |
} | |
} | |
/** | |
* shorten | |
* | |
* Creates an excerpt of description | |
* | |
* @param string $string The feed | |
* @param numeric $length How many characters to limit to | |
* @return string returns shortened text | |
*/ | |
public function shorten($string, $length) | |
{ | |
$suffix = '…'; | |
$short_desc = trim(str_replace(array( | |
"/r", | |
"/n", | |
"/t" | |
), ' ', strip_tags($string))); | |
$desc = trim(substr($short_desc, 0, $length)); | |
$lastchar = substr($desc, -1, 1); | |
if ($lastchar == '.' || $lastchar == '!' || $lastchar == '?') | |
$suffix = ''; | |
$desc .= $suffix; | |
return $desc; | |
} | |
/** | |
* get_comments | |
* | |
* Gets the number of comments | |
* | |
* @param string $item the feed | |
* @return string The correct number of comments | |
*/ | |
public function get_comments($item) | |
{ | |
$comments = $item->get_item_tags('http://purl.org/rss/1.0/modules/slash/', 'comments'); | |
$number = $comments[0]['data']; | |
if ($number == '1') | |
{ | |
return $number . ' ' . 'Comment'; | |
} | |
else | |
{ | |
return $number . ' ' . 'Comments'; | |
} | |
} | |
/** | |
* __tostring | |
* | |
* Converts obj to a string | |
* | |
* @return string displays rss feed | |
*/ | |
public function __toString() | |
{ | |
return $this->display_rss(); | |
} | |
/** | |
* display_rss | |
* | |
* fetches feed , checks for errors, uses get_comments, shorten, get_first_img_url | |
* | |
*/ | |
public function display_rss() | |
{ | |
$feed = ''; | |
$rss_items = ''; | |
$rss = fetch_feed($this->url);//uses built in simplepie to fetch feeds | |
if (!is_wp_error($rss)): | |
$maxitems = $rss->get_item_quantity($this->maxitems); | |
$rss_items = $rss->get_items(0, $maxitems); | |
endif; | |
if (!is_array($rss_items) || $maxitems == 0) | |
{ | |
$feed .= '<li>No items.</li>'; | |
} | |
else | |
foreach ($rss_items as $item): | |
$feed .= '<li class="item">'; | |
$feed .= '<span class="rss-image"> | |
<img src="' . $this->get_first_image_url($item->get_content()) . '"/> | |
</span>'; | |
$feed .= '<span class"data"> | |
<h5> | |
<a href="' | |
. esc_url($item->get_permalink()) . '" title="' | |
. esc_html($item->get_title()) . '">' | |
. esc_html($item->get_title()) . | |
'</a> | |
</h5>'; | |
$feed .= '<span class="date-image"> </span>'; | |
$feed .= '<small>' . $item->get_date('F Y') . ' </small>'; | |
$feed .= '<span class="comment-image"> </span>'; | |
$feed .= '<small>' . $this->get_comments($item) . '</small>'; | |
$feed .= '<p>' . $this->shorten($item->get_description(), '150') . '</p> | |
</span> | |
</li>'; | |
endforeach; | |
return $feed; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment