WordPress Plugin – Further deveopment

Now that I have a very simple working plugin it’s time for a little clean up.

Before I start adding addition functions and build additional features in messy procedural style it makes sense at this state to make it object orientated. WordPress continues to support PHP 4.3 and upwards, hence the plugin should also support PHP 4 configurations.

Here is the class made from the original function, variables have been moved to the object’s global scope, the hooks are included in the constructor and the Render() function now contains the regex replacements:

[code lang=”php”]class WP_Thumbshot_Preview {

/**
* Which mode to use, free or paid.
*
* @var string
*/
var $mode = ‘free’;

/**
* The user id (for paid mode).
*
* @var integer
*/
var $cid;

/**
* The image width
*
* @var integer
*/
var $width;

/**
* The image height
*
* @var integer
*/
var $height;

/**
* Constructor
*
* @return void
*/
function WP_Thumbshot_Preview() {
add_filter(‘the_content’, array(&$this, ‘Render’));
}

/**
* Render the content
*
* @param $content string
* @return string
*/
function Render($content) {
// Free mode
if ($this->mode == ‘free’) {
$content = preg_replace(“/\[preview\](.+?)\[\/preview\]/i”,

$1“,
$content);
}
// Paid mode
if ($this->mode == ‘paid’) {
$content = preg_replace(“/\[preview\](.+?)\[\/preview\]/i”,
cid&v=1&w=$this->width&h=$this->height&url=$1\” />
$1“,
$content);
}
return $content;
}
}[/code]

This is much tidier than before 🙂

Now to make everything work all that is required is an instance of the object:

[code lang=”php”]new WP_Thumbshot_Preview();[/code]

Share

My first WordPress Plugin

As I have an account with Thumbshots.com I thought it would be nice to include it on my blog and have preview images of links in posts. Despite the 1000’s of WordPress plugins available, unfortunately none of them worked specifically for Thumbshots. 🙁

I noticed there were a few similar plugins which worked with alternative thumbnail sites, so initially thought that I could hack them up a bit to use with Thumbshots. Another dead end, none of them seamed to have the features I was looking for.

So it looks like I’ll have to make a plugin…

In the whole 24 hours I have been using this version of WordPress I have been impressed with how it has evolved since I last used it, especially in that it is so easy to use. Plugin development is no exception to this.

There are 2 versions of Thumbshots, both a free and paid version (on .org and .com respectively), so it makes sense that if I am to eventually get this plugin to a point where I can publish it that it should support both. The free one can also be used ‘out of the box’ without an account or any configuration, whereas the paid one obviously requires an account and additional settings.

To get the plugin to work I needed to create the plugin information which is simply stuck in a comment block and WordPress recognises it, it looks something like this:

[code lang=”php”]/*
Plugin Name: WP Thumbshot Preview
Plugin URI: http://www.philbayfield.com
Description: Add a preview image to a link using Thumbshots.org or Thumbshots.com account.
Version: 0.1
Author: Phil Bayfield
Author URI: http://www.philbayfield.com
/*[/code]

Secondly I needed a short function to perform the core functionality of the plugin. To enable the user to choose when they want to see a preview it made sense to use bbcode style tags so it could be easily used when adding or editing a post. Any URL wrapped in [preview] tags would be converted to a link with thumbnail image. I’ve hard coded the settings for now for testing purposes, the end result is this function:

[code lang=”php”]/**
* Content filter function
*
* @param $content string
* @return string
*/
function wp_thumbshot_preview($content) {
/**
* Plugin usage mode (free or paid)
*
* @var string
* @todo this needs to be replaced with a get_option call
*/
$mode = ‘paid’;
// Paid mode
if ($mode == ‘paid’) {
/**
* Thumbshots user id
*
* @var integer
* @todo this needs to be replaced with a get_option call
*/
$cid = 1508;
/**
* Image width
*
* @var integer
* @todo this needs to be replaced with a get_option call
*/
$width = 160;
/**
* Image height
*
* @var integer
* @todo this needs to be replaced with a get_option call
*/
$height = 120;
// Replace all [preview] tags
$content = preg_replace(“/\[preview\](.+?)\[\/preview\]/i”,

$1“,
$content);
// Free mode
} else {
// Replace all [preview] tags
$content = preg_replace(“/\[preview\](.+?)\[\/preview\]/i”,

$1“,
$content);
}
return $content;
}[/code]

Finally to get this to work a hook is required which tells WordPress to run my new function on the post content:

[code lang=”php”]add_filter(‘the_content’,’wp_thumbshot_preview’);[/code]

That’s it, all done, this is what it produces:




To do:

  • Add default values
  • Store default values in the database
  • Modify the function to retrieve the settings from the database
  • Add a page in the admin console to customise the settings
  • Convert the plugin to OO format
Share