Anatomy of a Textpattern Plugin, part 1

A PHP developer’s guide to writing Textpattern plugins.

Introduction

Anatomy of a Textpattern Plugin is a developer’s guide to writing plugins for Textpattern. It assumes some familiarity with both Textpattern and PHP.

The first two parts of this series will describe the basic structure and capabilities of a plugin, and step through the process of compiling, installing, testing and running. Parts 3 and 4 will describe the plugin PHP code, from a simple “Hello World” example, to some basic — but useful — tag handlers. I’m not certain if the series will proceed beyond that point, but if it does, later parts will obviously cover more advanced concepts, with particular emphasis on techniques for solving real world problems.

About Textpattern

Since you’re here, I’ll assume at least a passing familiarity with Textpattern: “A free, flexible, elegant, easy-to-use content management system for all kinds of websites, even weblogs.”

Textpattern (or just “txp”) is a little different from most web publishing platforms. Not as rigid as most weblog packages, nor as sterile or complex as most CMS applications, it’s a popular platform for building blogs, small business and professional web sites.

One of the important reasons for Textpattern’s popularity is its template engine. Textpattern is aimed squarely at web designers, not PHP geeks or novice bloggers. Its template tags use a syntax that’s identical to XHTML, and thus familiar and convenient for designers:

<div id="content">
<txp:article limit="10" />
<div>
<txp:older>previous</txp:older>

One of the characterizing features of modern web development is the separation of presentation and content (and, increasingly, behaviour and structure). Textpattern follows this principle by dividing up and allocating tasks to various roles: designers, writers, editors, and so on.

Each task is carried out using tools appropriate to the job. Templates specify presentation, so they’re written in a style already familiar to a web designer. A PHP-based template syntax might be convenient for software developers, but PHP is a procedural programming language, not a markup language. Developers ought to be concerned with behaviour, designers with presentation.

Plugins

Plugins provide an easy way for developers to create new tags for Textpattern templates, and to introduce new features for the back and front end. They are easy to create for anyone with some PHP experience. A new template tag can be as simple as a single PHP function containing a single line of code.

Most things that can be done by modifying the Textpattern core code can be achieved in a plugin. This is a sensible approach in the long term: modifying the core code means you have to re-apply your changes each time you upgrade. If you put your code in a plugin instead, most upgrades require no changes at all.

Textpattern as a web development platform

For PHP developers, Textpattern makes a good choice as a platform and framework. It already provides many of the features necessary for a modern web application: templates, database management, internationalization, URL and GET/POST handling. You can build the main structure and content of a web site using the built-in tags and CMS features, and concentrate on writing the PHP code that’s specific to your application — rather than reinventing the wheel.

For more complex tasks, plugins have access to all of Textpattern’s internal functions, including database access functions. They can hook into Textpattern’s administration user interface, creating new admin pages, and adding extra elements to existing input forms. On the public side, plugins can intercept events, fetch GET and POST input data, and provide new URL structures.

Next: Audience

In this series


Commenting is closed for this article.