Archive for the ‘Plugins’ category

SB Twitter Plugin

November 21st, 2009

Let’s get this straight, I hate Twitter with a vengence on account of it being a pointless waste of time. However, I can see that some people enjoy using it and also want to share their exciting lives with other people.

I have just had the need to develop a very basic plugin to put a Twitter widget (not a Wordpress widget) on a website. I didn’t see the point in forcing the user to put masses of code onto their page each time they wanted to include the widget so created a shortcode to do the same job.

I have rather imaginatively called it SB Twitter and it allows the user of a shortcode to put the Twitter flash widget onto a post or a page.

Usage

[sb_twitter name="you_user_name"]

You can optionally include width=”" and/or height=”" to manipulate the size of the box.

Download

The download file contains a readme which essentially says to upload the file to your plugins directory, activate it and use the shortcode provided.

SB Twitter (2.29 KB)

Screenshots

Example output of SB Twitter plugin

SB Pay Me

July 22nd, 2009

No that isn’t a request although feel free if you like :)

SB Pay Me is a new plugin that I have written along the same lines as those ‘Donate’ plugins you see everywhere. Well I decided that plugins like ‘Buy Me Beer’ and other similar donation type things weren’t really very professional so I wrote my own.

Features

The SB Pay Me plugin is designed to use the Wordpress shortcode API (using rather predictibly sb_pay_me as a hook) to provide a very simple to integrate payment form. It is fully templated so you can very easily decide how you want it to look. I have integrated a couple of Paypal images by default but you can add your own with no problems what so ever. The currency, payment amount and description are all configurable. The latter two can be setup to be edited by the user on your site instead of sending them to Paypal and getting them to do it there. The admin interface is fully commented and straightforward to allow even the most basic of users to be able to user it.

Example

Send Money
Just a working example of the plugin.
Amount GBP
Payment For

Screenshots

Default Template

Default Template

Modified Template

Modified Template

Admin Page

Admin Page

Download
I intend to release the plugin into the extend directory but at the time of writing it was down. Use this link to get the most recent version: SB Pay Me (10.27 KB)

Welcome Email Editor for Wordpress

April 20th, 2009

Something which regular readers might be aware of is that Wordpress has taken up most of my attention for the last year or so. It seems a very flexible Blogging engine come CMS that can almost deal with anything… almost. When you begin to use it you think wow great flashy AJAX everywhere but when you take a look at getting it work with a site you start to see where it’s downfalls lie. I am, however, not a Wordpress hater. In fact it’s the opposite, I love it! Wordpress really comes into it’s own with it’s fantastic plugin API. It means that anyone can write their own noddy (technical term :) ) bit of code and get it to do something in no time. Or so I thought…

One of the least customisable parts of Wordpress is the Login/Register process. The register page seems unmoveable without a bit of copy and pasting of the code and the email you get when you register is very dire indeed. There are no hooks for editing the welcome email text or Wordpress admin page for updating the from address or headers.

Well, as it stood I needed to be able to edit the welcome email and registration process for work so I went about writing a standalone registration system (to follow) and Welcome Email Editor. Unfortunately the copy/paste method was required for the registration widget but the Welcome Email Editor is done properly.

As there were no hooks for the welcome email I had to override the wp_new_user_notification function and write my own then simply converted it to use Options and wrote a nice admin page for it. Let me know if I can extend it at all or if you find any bugs..

Download Wordpress Welcome Email Editor (14.03 KB)

Admin Email Received

Admin Email Received

Admin Page for Plugin

Admin Page for Plugin

Email Received By User

Email Received By User

Turning an Array or Object into XML using PHP

March 25th, 2009

I have read a fair few tutorials written by others using classes and DOM functions to create XML from arrays. But what happens if you have an older PHP installation or prefer to keep things really simple as I do.

The following function declarations generate an XML string from an associative array. In my scripts that use it, it’s usually coupled with a download function so that XML is generated on the fly and then downloaded to a file. The main advantage of this is that you can write a basic import script using the SimpleXML library to restore the array at any time which makes it perfect for use as part of a backup/restore system.

Wordpress stores alot of it’s sitewide data in a table called wp_options which stores a named key and a value string for each piece of information. Plugins can also use this table by using the add_option, update_option, delete_option and get_option functions passing values, arrays or objects along with a key name. This isn’t an ideal data structure but does mean that plugin writers can easily store information without bloating the database with unnecessary and often badly designed tables.

The Code..

function generate_xml_from_array($array, $node_name) {
	$xml = '';

	if (is_array($array) || is_object($array)) {
		foreach ($array as $key=>$value) {
			if (is_numeric($key)) {
				$key = $node_name;
			}

			$xml .= '<' . $key . '>' . "\n" . generate_xml_from_array($value, $node_name) . '</' . $key . '>' . "\n";
		}
	} else {
		$xml = htmlspecialchars($array, ENT_QUOTES) . "\n";
	}

	return $xml;
}

function generate_valid_xml_from_array($array, $node_block='nodes', $node_name='node') {
	$xml = '<?xml version="1.0" encoding="UTF-8" ?>' . "\n";

	$xml .= '<' . $node_block . '>' . "\n";
	$xml .= generate_xml_from_array($array, $node_name);
	$xml .= '</' . $node_block . '>' . "\n";

	return $xml;
}

Usage

You can use the above functions by generating an array or object as normal and then using the following code. The function returns the XML and you can then do with it as you wish.

$xml = generate_valid_xml_from_array($array);

Things to consider

XML doesn’t allow for numeric tags so any numbers are replaced by the content of the $node_name variable. XML also doesn’t allow for certain special characters within it’s tags and ,for this reason, the htmlspecialchars function is passed over the raw data before it is placed into the string.

You can, however make use of cdata tags which tells XML readers to essentially ‘ignore whats coming next’. See the w3schools explanation of cdata here: http://www.w3schools.com/XML/xml_cdata.asp. There is no right or wrong way to escape data but if you have problems with my script then factor in the cdata tags replacing the htmlspecialchars call and see how you get on.

Alternatively feel free to contact me by commenting on this post or using the contact form and I will work with you to get your script working.