Anatomy of a Textpattern Plugin, part 3

A PHP developer’s guide to writing Textpattern plugins.

Hello World Deluxe

Let’s use what we’ve learned to remix the zem_hello_world tag handler. We’ll fix the HTML encoding bug, and — introducing another built-in Textpattern function, doTag — add new wraptag and class attributes.

function zem_hello_deluxe($atts) {
   extract(lAtts(array(
      'name'  => 'Alice',
      'wraptag' => '',
      'class' => "hello",
   ),$atts));

   return doTag(htmlspecialchars($name), $wraptag, $class);
}

@doTag@ accepts three arguments: @$content@, @$tag@, @$class@.  It encloses the content with the specified tag, using the CSS class name.  It's smart enough to behave appropriately when the tag or class values are empty.
<txp:zem_hello_deluxe name="Alice & Bob" />
Alice &amp;amp; Bob
<txp:zem_hello_deluxe name="Alice & Bob" wraptag="div" />
<div class="hello">Alice &amp;amp; Bob</div>
<txp:zem_hello_deluxe wraptag="p" class="" />
<p>Alice</p>

Next, in part 4: debugging essentials, and enclosing tags.

1 There’s no official pegistry of plugin developer prefixes. Check the Central Plugin Registry for clashes.

2 As of Textpattern 4.0.4, dmp allows any number of arguments, like dmp('my atts', $atts);. In prior versions, only a single argument is supported.

Previous: Output

In this series


Thank you for this series. It is clearly written and extremely informative.

A fantastic resource for present and future plugin authors.

hakjoon    Mar 24, 01:51 pm    #

Great series! I’ve been hacking around with other people’s plugins – a lot of trial and mostly errors.

This series of articles is exactly what I needed in the first place.

— Anura    Mar 25, 08:57 pm    #

Commenting is closed for this article.