Archive for September, 2009

We have a mortgage offer!

Today, after a month of waiting we finally received a mortgage offer for our house!

Hopefully we shall be moving in within the next couple of weeks.

Pictures to follow soon of the new house! :)

  • Share/Bookmark

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:

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",
                                                "<a href=\"$1\" target=\"_blank\"><img src=\"http://open.thumbshots.org/image.aspx?url=$1\" /></a><br />
                                                 <a href=\"$1\" target=\"_blank\">$1</a>"
,
                                                $content);
                }
                // Paid mode
                if ($this->mode == ‘paid’) {
                        $content = preg_replace("/\[preview\](.+?)\[\/preview\]/i",
                                                "<a href=\"$1\" target=\"_blank\"><img src=\"http://simple.thumbshots.com/image.aspx?cid=$this->cid&v=1&w=$this->width&h=$this->height&url=$1\" /></a><br />
                                                 <a href=\"$1\" target=\"_blank\">$1</a>"
,
                                                $content);
                }
                return $content;
        }
}

This is much tidier than before :)

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

new WP_Thumbshot_Preview();
  • Share/Bookmark

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:

/*
Plugin Name: WP Thumbshot Preview
Plugin URI: http://www.philbayfield.com
Description: Add a preview image to a link using <a href="http://www.thumbshots.org/">Thumbshots.org</a> or <a href="http://www.thumbshots.com/">Thumbshots.com</a> account.
Version: 0.1
Author: Phil Bayfield
Author URI: http://www.philbayfield.com
/*

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:

/**
* 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",
                                        "<a href=\"$1\" target=\"_blank\"><img src=\"http://simple.thumbshots.com/image.aspx?cid=$cid&v=1&w=$width&h=$height&url=$1\" /></a><br />
                                         <a href=\"$1\" target=\"_blank\">$1</a>"
,
                                        $content);
        // Free mode
        } else {
                // Replace all [preview] tags
                $content = preg_replace("/\[preview\](.+?)\[\/preview\]/i",
                                        "<a href=\"$1\" target=\"_blank\"><img src=\"http://open.thumbshots.org/image.aspx?url=$1\" /></a><br />
                                         <a href=\"$1\" target=\"_blank\">$1</a>"
,
                                        $content);
        }
        return $content;
}

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

add_filter(‘the_content’,‘wp_thumbshot_preview’);

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/Bookmark

O2 loose iPhone exclusivity deal!

So it appears that someone somewhere finally had a bit of common sense and allowed Orange to sell the iPhone!

O2 have always had the worst data network in the UK, even 3’s is better! It’ll be interesting to see how many people leave now they have options!

More info here:


  • Share/Bookmark

My favorite TV ad!

Great advert, perhaps you have to be from Plymouth or have lived there for years to get it though!

YouTube Preview Image

Perhaps I just posted this again as an excuse to test the YouTube plugin for Wordpress too ;)

  • Share/Bookmark

Photo Album

I have started to add my photos to the site, starting with the most recent ones.

Due to the slow internet connection this will probably take some time, everything added so far can be found on the photos page.

  • Share/Bookmark

New website, new blog!

Time for a change, my website has been around for 4+ years and was starting to look at bit dated.

The massive amounts of spam comments on the old b2evolution blog were also somewhat of a pain.

So here it is, my new website and new blog, hope to add some new stuff on here very soon! :)

  • Share/Bookmark