Archive for PHP

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');

Random SQL Results

// November 12th, 2008 // No Comments » // Database, MySQL, PHP, SQL

Last year I needed to display 3 random featured items. The solution was very simple using MySQL with this query:

SELECT * FROM `table` WHERE `column` = 'criteria' ORDER BY RAND() LIMIT 3;

Quick and dirty query generator

// November 12th, 2008 // No Comments » // PHP

Again, I needed to quicky put in WHERE and ANDs depending on how many parameters were submitted.

Instead of putting multiple conditionals, I just put them into an array:

$sqlarray = array();
if($year)
	$sqlarray[] = "(table.Year = " . $year . ")";
if($week)
	$sqlarray[] = "(table.Week = " . $week . ")";

if($otherparameter)
	  $sqlarray[] = "(otherparam = '" . $otherparam . "')";

$sql = "";
if(sizeof($sqlarray) > 0)
{
	$sql = " WHERE ";
	for($i = 0; $i < sizeof($sqlarray); $i++)
	{
		$sql = $sql . $sqlarray[$i];
		if($i != sizeof($sqlarray) - 1)
			$sql = $sql . " AND ";
	}
}

Inserting a Space Before a Capital Letter

// October 14th, 2008 // No Comments » // PHP

I needed to insert a space between a camel case string, however, some of the strings contained more than one uppercase letter. I needed to have a regular expression that inserted a space only after an uppercase letter that had a lowercase letter following it. Here is the code:

1
2
3
4
5
//put space after uppercase
$string = preg_replace('/(\w+)([A-Z])/U', '\\1 \\2', $string);
 
//put space before uppercase following lowercase
$string = preg_replace('/([a-z])([A-Z])/', '\\1 \\2', $string);

Incrementing date

// October 14th, 2008 // No Comments » // PHP


$date = date("Y-m-d");
for($day = 0; $day < 5;$day++)
{
echo date("jS F",strtotime($date));
$date = date("jS F",strtotime($date) + 86400);
}

Sorting Specific Array

// October 13th, 2008 // No Comments » // PHP

I needed to dynamically load a group of options from the database. The thing was that these were not organised alphabetically; instead they were ordered in platinum, gold, silver, bronze format. This posed a little bit of a problem. The way around it was seeting up an array with the correct order of options. Then query the DB and bring back the results. Looping through these results and getting the array key of the organised array. A new array created stores the options from the db, and is sorted on its keys with ksort.

$orderedarr = array(
0 => “platinum”,
1 => “gold”,
2 => “silver”,
3 => “bronze”);

$rsCustomerStatus = $db->query(“SELECT DISTINCT ProspectStatus
FROM tblFleetCustomer
WHERE (ProspectStatus IS NOT NULL) AND (ProspectStatus <> ”) ORDER BY ProspectStatus DESC”,1);

$displayarr = array();

foreach($rsCustomerStatus as $rowStatus)
{
//put the value in the correct position in array
$valarr = array_keys($orderedarr, strtolower($rowStatus["ProspectStatus"]));
$displayarr[$valarr[0]] = $rowStatus["ProspectStatus"];
}

ksort($displayarr);

//display array in correct order
foreach($displayarr as $val)
echo “<option value=\””. $val .”\”>”. htmlentities(ucwords(strtolower($val))) . “</option>\r\n”;