Archive for the ‘Plugins’ category

Plugin Conflict: WP Welcome Email Editor -> Mingle

March 15th, 2010

Mingle LogoJust a quick one this evening. A user has recently noticed that the WordPress plugin called Mingle conflicts with my plugin WP Welcome Email Editor. It, for some reason, causes WordPress to call it’s own version of the wp_new_user_notification function before my plugin, or any other for that matter, can declare theirs.

The remedy is simple, fix Mingle. In the absence of that you can fix it using the following method:

  1. Open mingle.php
  2. After the PHP comment for the plugin at the top add the following code
    add_action('init', 'mngl_init');
    function mngl_init() {
  3. Right at the bottom of the code before the closing ?>, add the following:
    }
  4. Save the file and then plugins like mine will work with it without causing a conflict.

If either the Mingle author fixes the problem or anyone has a better way of sorting this then please do let me know and I will update this post to let others know.

Please make sure you back up the mingle.php file before making these changes. Just in case ;) Please also be aware that if you update the Mingle plugin then you need to put this code in again to maintain the fix.

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.

SQL Import from a file using PHP

March 4th, 2009

On seeing the title of this post your first uncontrollable outburst may be ‘Why on earth would I want to do that?‘. On further reading, you may decide that PHP is the way to get tables created from within an automated install process (WordPress plugin activation or PHP based software ‘installation’). It may trigger another outburst when you see that the code I am about to paste in is more than a single line long (which would be all it would take using the PHP exec() command).

Why not use the Exec command?

Alot of developers cringe at the thought of the exec command in PHP, others have disabled it due to ‘security issues’. My reason for not using it is because my script doesnt just import SQL. It can do two very important things for me:

  • Prioritise the import
  • Prefix the table names

Prioritising the import isn’t essential but it means that your SQL file can be a mess of random statements and no matter where the INSERTs are, they will be grouped and run last to allow the CREATE statements a chance to work.

Prefixing the table names is almost self explanatory. Wordress, which I wrote this script for, allows Database prefixes to be used when setting up a blog. It only makes sense that to avoid conflict when more than one blog is in the same database to prefix my tables with the same string.

How does it all work?

For those interested the code above simply does the following:

  • Imports the file from an absolute path passed to the function into an array split by line
  • Loops through the lines looking for starting words defined as SQL statement keywords in an array and generates a list of complete statements
  • Removes all comments as it goes (comment defined as any line starting with ‘–’
  • Looks for the first backtick character (`) which MUST be present (at least around the table name) and inserts the WordPress Database Prefix
  • Inserts the completed query into an array grouped by those that are INSERT queries and those that aren’t
  • Sorts the new array by a priority figure (new numbers can be added to mark muti-level priority if you like)
  • Runs the queries

Integration

To integrate it into your WordPress plugin then simply copy/paste the code into yours and pass it an absolute path on plugin activation. I also use it to drop tables on a ‘hard’ deactivation by passing it an SQL file full of DROP commands. To integrate it into your NON WordPress site then simply remove all references to $wpdb and substitute ‘$wpdb->prefix’ with your own string or variable name.

The Code

Here is the code although I have also provided it in download form: MySQL Import Script (1.14 kB)

function mysql_import($filename) {
	global $wpdb;

	$return = false;
	$sql_start = array('INSERT', 'UPDATE', 'DELETE', 'DROP', 'GRANT', 'REVOKE', 'CREATE', 'ALTER');
	$sql_run_last = array('INSERT');

	if (file_exists($filename)) {
		$lines = file($filename);
		$queries = array();
		$query = '';

		if (is_array($lines)) {
			foreach ($lines as $line) {
				$line = trim($line);

				if(!preg_match("'^--'", $line)) {
					if (!trim($line)) {
						if ($query != '') {
							$first_word = trim(strtoupper(substr($query, 0, strpos($query, ' '))));
							if (in_array($first_word, $sql_start)) {
								$pos = strpos($query, '`')+1;
								$query = substr($query, 0, $pos) . $wpdb->prefix . substr($query, $pos);
							}

							$priority = 1;
							if (in_array($first_word, $sql_run_last)) {
								$priority = 10;
							} 

							$queries[$priority][] = $query;
							$query = '';
						}
					} else {
						$query .= $line;
					}
				}
			}

			ksort($queries);

			foreach ($queries as $priority=>$to_run) {
				foreach ($to_run as $i=>$sql) {
					$wpdb->query($sql);
				}
			}
		}
	}
}

More ‘Your Brand’ Products Released/Updated

January 26th, 2009

I generally like to write how-tos and tutorials on this site but I feel the need to mention some of the WordPress work i have been doing with Tim Nash.

We have been working very hard over the last couple of months putting together a number of new plugins and updating some older ones.

Finally we got the major bugs ironed out and the products released last week. They are as follows:

The following additions to Your Members were released:

  • Your Members Firesale

The following products are currently in closed Beta and will be shortly be released for all (in Beta):

  • Your Responder – Autoresponder Plugin
  • Your Classifieds – Classified Advertising Plugin

All of the products have had their interfaces ‘tweaked’ to allow for the reshuffle (complete change) in the recent release of WordPress 2.7 and we are always optimising the code to be both more maintainable and more efficient.

If you have not heard of ‘Your Brand’ or want to find out more then please visit www.newmedias.co.uk or drop by the forums where myself and Tim can usually be found answering questions and helping people out.

We have also written a few Free WordPress Plugins that are available! Click here to visit the Freebies page on Newmedias.

SB Child List Plugin

January 11th, 2009

I have been working on my boat site for a short while now and want to show some sort of heirarchy in the pages. I have decided to call one of my pages ‘articles’ and have my articles use it as their parent.

Logically you would expect to see a list of the child pages on the articles page along with a pretty picture and some intro text. This doesn’t seem possible natively with wordpress so I decided to make it happen.

Adding the hook to any page will by default show an unordered list showing the children and links to them. If you prefer to style it yourself then don’t worry because you can do that too using the templating system on the settings page.

There is also add another tag, , that allows you to provide a back to parent link from any child. This enables you (in the articles example) to add a “click here to read more on this subject” link. It is also template based so it can say anything or look however you see fit.

Download it here: Download Page