John D Wells

I build websites with HTML, CSS, jQuery, PHP, ExpressionEngine & CodeIgniter.

Proud co-founder of the kick-ass, London-based creative agency One Darnley Road.

© johndwells

Do you miss being a hacker? Then build yourself a plugin.

No Hacking Required ™: The ExpressionEngine community proudly touts this as one of EE’s great strengths over competing CMS platforms.  But if you miss hacking, if you’d like to capture the satisfaction and thrill of raw, unadulterated PHP code, you should build yourself a plugin.

Published:
Dec 30, 2010
Filed under:
EECMS
Comments:
2 - add yours

For me, the ah-hah moment came as I read one of Brandon Kelly's posts over on his Pixel & Tonic blog. He was kindly reviewing some of his work done to launch the new P&T website. In it he described how he'd written a custom plugin function to replace all instances of "http://" with "https://" for any templates that were going to be operating under SSL. To me, it was a hack, plain and simple: an elegant demonstration how to bend EE to your will, to harness EE's sometimes peculiar parsing order to your advantage.

Since then, I begin every project with an empty plugin. As you can see from EE's developer documentation, the barebones of a plugin are incredibly simple:

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$plugin_info = array(
	'pi_name' => 'SiteName',
	'pi_version' =>'1.0.0',
	'pi_author' =>'Your Name',
	'pi_author_url' => 'http://sitename.com',
	'pi_description' => 'SiteName - A few hacker functions for this theme/site'
);

class SiteName {

	var $EE;

	function __construct()
	{
		$this->EE =& get_instance();
	}
}
/* End of file pi.sitename.php */
/* Location: ./system/expressionengine/third_party/sitename/pi.sitename.php */

Once you get into the swing of rolling your own plugins, you might be surprised at what you come up with; in circumstances where you could both simplify and speed up your templates by relying on a hacked up plugin to do some outputting for you. Consider it part of your template arsenal, just like CSS files and Javascripts.

A Real World Hack

I'm currently working on a project that relies heavily on Flickr.  I'm using DataGrab to periodically query the Flickr API for new or modified photos from our client account, and to import their information into a channel. I store all information about the photos such as Farm ID, Server ID, etc, but never the images themselves. I need to create <img> tags based on Flickr's formula, which, if you've worked with before, is not the easiest to remember. So I wrote this plugin:

/*
 * Assembles a valid URL to flickr image
 */
function flickr()
{
	$size = strtolower($this->EE->TMPL->fetch_param('size'));
	$size = ($size) ? $size : 'square'; // default is square
	
	$id = strtolower($this->EE->TMPL->fetch_param('id')); // UID of photo
	$server = strtolower($this->EE->TMPL->fetch_param('server')); // server ID
	$secret = strtolower($this->EE->TMPL->fetch_param('secret')); // secret
	$farm = strtolower($this->EE->TMPL->fetch_param('farm')); // farm ID
	
	switch ($size) // determine prefix based on size
	{
		case('square') :
			$prefix = '_s';
		break;
		case('thumb') :
			$prefix = '_t';
		break;
		case('small') :
			$prefix = '_m';
		break;
		case('large') :
			$prefix = '_b';
		break;
		case('medium') :
		default :
			$prefix = '';
		break;
	}
	
	return "http://farm{$farm}.static.flickr.com/{$server}/{$id}_{$secret}{$prefix}.jpg";
}

As you'll notice, it's incredibly simple; it retrieves my parameters, defines a prefix string based on the requested size, and returns the compiled src string. If I need a large version of my image, this is all I need to do:

{exp:sitename:flickr size="large" id="{f_id}" server="{f_server}" secret="{f_secret}" farm="{f_farm}"}

If I'd rather output a square thumbnail, I only need change the first parameter to size="square". As the Meerkat for CompareTheMarket.com would declare, "Simples! Chk-chk."

Hack vs 3rd Party Addon

Why do I consider the above example a hack and not a 3rd party add-on? To my mind, what makes a hack is this: it's a (sometimes hasty) snippet of code that exists to solve one problem and one problem only. It is built under very specific circumstances, to be used by a very specific audience: Just Me. Notice that my function performs no checks; it does not sanitise data, it doesn't abort if not given enough data to return a valid URL.

It's simple and effective. If I port it over to another project, it very well may need to change. And that's a good thing: I am free to change it at will.  I don't worry about legacy support.  Who cares if it might bork on IIS. I don't need to file a feature request with the developer to ask for some new piece of functionality.

Plugins are the perfect place for you to "hack" because they too are simple and effective.  Modules & Extensions require touching the database, they require install and uninstall methods, you must activate them via the Control Panel.  But a plugin? Just drop it in your third_party folder and go.

Resources To Get Started

If I've convinced you to build some simple plugins of your own, then keep these links on hand, print out some cheat sheets, and start hacking away:

Parting thought: if you really want to have some fun, then spend some time under the hood: EE's source code is well commented and logically organised.  A few hours perusing the source, along with learning EE's general DB structure, will make you a pro hacker in no time.

Published:
Dec 30, 2010
Filed under:
EECMS
Comments:
2 - add yours
 

Have your say...

Awesome post. I really digg EE plugins. Wrote 2 myself for a project that needed some specific “hacks”. Stuff that I didn’t find prebuilt by the community. So, leveraging my existing php skills into plugins was fairly easy.

In the end, you end up with a nice repository of simple “hacks” you can reuse if needed, making this very simple to enhance EE, unlike other CMSes where it’s more cumbersome to do.

  • #2
  • On 03:13 PM, 10/01/11
  • From London, UK
  • John D Wells said:

Thanks, yeah I totally agree - EE plugins are genuinely fun to toy with. A lot of the time I just don’t see the need to bother with relying on a 3rd party plugin.

But I can appreciate that designers tend to love EE precisely because they don’t have to bother with PHP. I guess that’s OK too. ;)