Simple PHP RSS Reader WordPress Plugin

// December 4th, 2009 // 1 Comment » // 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');

Google Event Tracking

// November 6th, 2009 // No Comments » // Development, Google Analytics, Javascript, JQuery

I was asked recently if there was any way to track click events on an accordion we have implemented on a site. When first asked I didn’t think this was possible without an awful lot of javascript. However, a colleague mentioned Google event tracking is already available in Google if you have the standard setup. You can attach various events on various elements. The example on the google site is attaching the event to a play button.

Event tracking is so simple to impliment. You don’t even have to set anything up within anaylitics, you simply add the tracking event, and when events happen, this will be recorded. The format for the event is:


_trackEvent(category, action, optional_label, optional_value)

To impliment it using JQuery, I simply added a click event to the element like this:

1
2
3
4
5
6
7
$('#accordion h3 a').click(function()
{
	//get the section title
	var section_title = $(this).text();
	//call pageTracker and track the event
	pageTracker._trackEvent("recipes", "click", section_title);
});

Simple!

Sample Accesskey List

// November 2nd, 2009 // 1 Comment » // Accessibility, Development, User Experience, XHTML

Access keys are simply keyboard shortcuts. They are defined combinations of keys, Alt + a key which allows the user to access that field or section of a webpage.

You impliment accesskeys using the accesskey attribute on links and forms fields. If the access key is defined on a link, the browser will follow the link. If defined on a form field, the browser will set focus on that field.

To use accesskeys on Windows, press ALT + an access key. On a Mac press Control + an access key.

There are no access key standards, so here are some commonly-used keyboard shortcuts:

  • Alt + 1 – Home (yourdomain.com)
  • Alt + 2 – Skip Nav
  • Alt + 3 – Site Map
  • Alt + 4 – Search
  • Alt + 5 – Products
  • Alt + 6 – Forums
  • Alt + 7 – Blogs
  • Alt + 8 – Privacy
  • Alt + 9 – Feedback
  • Alt + 0 – Accesskey List

Browser Compatibility Tool

// September 2nd, 2009 // No Comments » // CSS, XHTML

I came across a great browser compatibility tool to check websites for IE browsers such is IE6, IE 5.5, well, all of them in fact. What is really great about it is that it is FREE! Great to work with this along with hacking bits of CSS, thanks again Wikipedia!

Folding Plug

// June 29th, 2009 // No Comments » // Design, User Experience

Now why didn’t I think of this? I’m sure we have all thought that about something or other. This, is pure genious. The UK plugs are huge compared to the double pin plugs from various countries around the world. Because of this problem a great solution has been created by Mr Min Kyu Choi.

Get elements by class name with javascript

// May 14th, 2009 // 4 Comments » // CSS, Javascript

Javascript does not support getting elements by class name; it only allows you to get elements by id. It is very useful having the ability to do this however, such as opening windows on a click when an anchor has an “external” class attached to it. As I don’t use frameworks such as JQuery for everything, I created this function which gets each element with a given class name and returns an array of each element.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
document.getElementsByClassName = function(className)
{
  var children = document.getElementsByTagName('*') || document.all;
  var elements = new Array();
  for (var i = 0; i < children.length; i++)
  {
    var child = children[i];
    var classNames = child.className.split(' ');
    for (var j = 0; j < classNames.length; j++)
    {
      if (classNames[j] == className)
      {
        elements.push(child);
        break;
      }
    }
  }
  return elements;
}

Separating MySQL Username and Host

// May 6th, 2009 // No Comments » // Database, Development, MySQL

As the MySQL USER() command returns users as ‘user’@’host’, it can be difficult to do straight comparasons. One way to extract both username and host is by using SUBSTRING_INDEX. For example, in a stored procedure you could use:


-- USER
SELECT SUBSTRING_INDEX(USER(), '@', 1) INTO txtUsername;
-- HOST
SELECT SUBSTRING_INDEX(USER(), '@', -1) INTO txtHost;

Voila!

Users and Permissions in MySQL

// March 24th, 2009 // No Comments » // Database, MySQL

The best way for creating users in MySQL is by using CREATE USER or GRANT statements. Another way is by manipulating MySQLs grant tables, but this is not recommended.

By default the superuser is root. A password must be specified for this. It is best if this is only used for superadmin purposes.

For new accounts you need to create two users with the same name. One is for access via localhost, the other remotely.

GRANT ALL PRIVILEGES ON *.* TO 'user'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'user'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;

To remove a user, use

DROP USER user;

To Change a password, use:

SET PASSWORD FOR 'user'@'localhost' = PASSWORD('newpassword');
SET PASSWORD FOR 'user'@'%' = PASSWORD('newpassword');

Backing up a MySQL Database

// March 24th, 2009 // No Comments » // Database, MySQL

Using cmd prompt in Windows, navigate to the bin directory where MySQL has been installed. There will be a file called “mysqldump.exe” in that directory. Enter the following command:

mysqldump -u root -ppass --databases your_db > backup.sql

Notice that there is no space between the -p and the password, this is a bug in MySQL.

The backup.sql file will be created in the directory that you are in. You can specify C:\your_db.sql if you want the file to be backed up on the C:\ drive.

If you want to backup the structure only, use -d for the “–no-data-option”. For example:

mysqldump -u root -ppass -d --databases your_db > backup.sql

Disappointments with the iPhone

// March 10th, 2009 // No Comments » // Fun, iPhone

Don’t get me wrong, the iPhone is a great piece of kit. With all they hype and all the amazing things the phone can do, I was let down by a few basics that I would have taken for granted with even the cheapest of phones. For example ..

You cant send MMS with an iPhone

What? Yeah, seriously! You can only email photos. You cant even send pictures on the iPhone! It took a while for this to sink in after searching around in the menus. I thought surely this would be a standard part of the kit? Unfortunately not. There is supposedly a way around it. Apparently each mobile phone number has a corresponding email address with its network, so you could email it to this number @ network.com. For example, with virgin you would put in the mobile number, with international dialing code, then @virgin.com, and the image should be send to the phone. I have not tried this out however.

You cant take video

Exactly. This is just as bad as not being able to send an MMS. There is an app that you can download for about $15 though. This also raises a complaint that a lot of applications should be standard on the phone, and why should you pay extra for things that are standard on other phones. The downloadable application uses an open source codec so it has been criticized for charging for the app.

Camera is bad quality in low light

I think a flash is a bit overkill on phones to be honest. I just found the quality of low light conditions to be worse on the iPhone than on other mobiles I have seen. Some sort of comparison chart would be good.

Battery life is bad

There are ways to extend the life of the iPhones battery, however, if you want to use the iPod and maybe do a few calls and a little browsing you will have to charge the battery daily.

Apart from all of those the iPod is a cool bit of kit.