Simple PHP RSS Reader Wordpress Plugin
// December 4th, 2009 // Development, PHP, Web Development, Wordpress
I wanted to add some articles into a page on one of my Wordpress sites. I wanted something very simple and easy to use. It has been a long time since I had written a plugin.
First things first, start with declaring what your plugin is called, your name, version, your URL and other information at the top of the PHP file like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 | /** * @package RSS Reader * @author Mark Robson * @version 0.1 */ /* Plugin Name: RSS Reader Plugin URI: http://www.whiteforest.co.uk Description: Pull in RSS articles into your site Author: Mark Robson Version: 0.1 Author URI: http://www.whiteforest.co.uk/ */ |
I then started off with the core of the functionality, in this case the read_rss() function. First thing to do is set up a DOMDocument, and then load in the document using the objects load() function, passing in the rss feed you want. As this is a very basic plugin and I knew exactly what feed I wanted, I simply hardcoded it. It wouldn’t be too difficult to either store the feed in a database, or pass it in when the feed is embedded in a post. For now we will leave it like this.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 | function read_rss() { $xmldoc = new DOMDocument(); $xmldoc->load('http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/world/rss.xml');</code> $info = ""; foreach ($xmldoc->getElementsByTagName('item') as $feeditem) { $info .= '<h3><a href="' . $feeditem->getElementsByTagName('link')->item(0)->nodeValue . '"> ' . $feeditem->getElementsByTagName('title')->item(0)->nodeValue . </a></h3>'; $info .= "<p>" . $feeditem->getElementsByTagName('description')->item(0)->nodeValue . "<p>"; } echo $info; } |
Ok, so that all works. It pulls in the title, and link to the article, and also the description of the post. That is all I needed right now. Next, we want to embed something in the particular post we are bringing the feed into. In this case, I want to place a comment in the post, which will then trigger Wordpress to replace that, and insert the rendered feed into the content. This is done with this:
28 29 30 31 32 33 34 35 36 37 38 | function setup_rss($content) { if(! preg_match('<!--rssfeed-->', $content)) { return $content; } else { read_rss(); } } |
Ok, now that is just about ready. Now we need to tell Wordpress to look for each instance of the above This is done simply by adding a filter on the post content using:
49 | add_filter('the_content', 'setup_rss'); |
Here is the final code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | /** * @package RSS Reader * @author Mark Robson * @version 0.1 */ /* Plugin Name: RSS Reader Plugin URI: http://www.whiteforest.co.uk Description: Pull in RSS articles into your site Author: Mark Robson Version: 0.1 Author URI: http://www.whiteforest.co.uk/ */ function setup_rss($content) { if(! preg_match('<!--rssfeed-->', $content)) { return $content; } else { read_rss(); } } function read_rss() { $xmldoc = new DOMDocument(); $xmldoc->load('http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/world/rss.xml'); $info = ""; foreach ($xmldoc->getElementsByTagName('item') as $feeditem) { $info .= '<h3><a href="' . $feeditem->getElementsByTagName('link')->item(0)->nodeValue . '"> ' . $feeditem->getElementsByTagName('title')->item(0)->nodeValue . '</a></h3>'; $info .= "<p>" . $feeditem->getElementsByTagName('description')->item(0)->nodeValue . "<p>"; } echo $info; } add_filter('the_content', 'setup_rss'); |

Perfect. Thanks.