find the shortcut extension development

Tips for Extension Development

Sometimes writing a new plugin can be difficult. Especially if you’re not familiar with the code for the system for which you want to write. It can be a daunting task but also a highly rewarding task. We’d like to share a few tips you can use when working on extension development.

Each of these tips were items we learned while developing our own plugins (which you can download and view for yourself). Remember, no one is born an expert. We all have to start somewhere and we’re all learning. Perhaps some of these tips will help you to become a better developer as well. Extension development can be difficult but it can also be fun.

Always Look for Shortcuts

This tip is the easiest to do but often the most overlooked. When you’re beginning development on a new system you don’t know what’s already been created. Many times other developers have come before you and done something similar. This means there’s a good chance at some point along the way it’s been coded as an option directly into the system. Whether you’re looking for a simple regular expression function to perform a find and replace or something bigger. The chances are high that some time previously some one else has also had a similar need.

Real-Life Example

When we developed our Tweetables plugin we were developing for both Joomla and WordPress. These two CMS’s require very different code formatting and structure. WordPress has a very strong set of functions readily available for plugins to use. Notice we use the term – functions, not triggers. There’s a difference.

As a quick background, Tweetables uses a regular expression to find specific shortcodes in content items and replace them with a tweetable link. This is very convenient for people who want to allow others to tweet (re-tweet) specific phrases from their blog post all without leaving the website. 

When writing the code for the WordPress version of Tweetables we were able to take advantage of built in WordPress functionality and greatly simplify the process.

add_shortcode( 'tweetable', 'tweetable_function' );

This little bit of WordPress code allowed us to specify the text to search for and the resulting function to run when found. Pretty simple? Yep.  Let’s look by contrast at the corresponding Joomla code.

$pattern = '#\(.*)\[\/tweetable\]#iU';

while (preg_match($pattern, $content, $regs, PREG_OFFSET_CAPTURE))
$replacement = $this->_replaceText($regs[2][0], $regs[1][0], $params);

// Replace found tags with correct tweet
$content = substr_replace($content, $replacement, $regs[0][1], strlen($regs[0][0]));

Let us be very clear. We are in no way comparing WordPress and Joomla. This simply demonstrates one example where understanding the code already available will make development easier. There are many other examples where Joomla has pre-existing code and WordPress would require more lines written.

The bottom line is when developing for a system, take the time to find out what already exists. There is no need to reinvent the wheel if something already exists which can be used. These are the shortcuts. Look for them and use them.