Five esoteric but important technical secrets we snuck into the Textpattern 4.0.4 release for plugin developers.
I’d like to draw your attention to two sneaky little additions to the XML feed code. atom.php line 90 and rss.php line 64 each add an event callback that allows plugins to alter the content of article XML feeds.
A trivial example:
register_callback('zem_myfunc', 'atom_entry'); register_callback('zem_myfunc', 'rss_entry');function zem_myfunc($event, $step) { return tag("Look ma, I'm in a feed", 'some_tag'); }
Anything returned by atom_entry or rss_entry event handlers will be appended to the feed output for each entry. It’s up to you to make sure everything is valid and correctly escaped – see escape_cdata and escape_output.
Podcasting, here we come.
Additionally, it’s possible to alter the contents of the $thisarticle global from within the callback event handler to change the output of existing feed tags. The event handler is called after $thisarticle is populated, but before any of the feed output is generated for that article. You could use this to reformat the article excerpt or body specifically for XML feeds, for example, or append something to the excerpt:
register_callback('zem_append', 'atom_entry'); register_callback('zem_append', 'rss_entry');function zem_append($event, $step) { global $thisarticle; $thisarticle['excerpt'] .= '<p>something more</p>'; }
It’s possible for a plugin to override the glyphs used by Textile for “smart quotes”, apostrophes and the like. Just define the appropriate constants at plugin load time (admin-side, of course):
define('txt_quote_double_open', '«');
define('txt_quote_double_close', '»');
A full list of constants is available in classTextile.php.
We’d initially planned to automatically override puncuation glyphs for all supported Textpattern languages. It turns out that smart quotes aren’t quite so smart however — its errors become more obvious with non-English quote styles. Use with caution, and report bugs as you find them.
It’s now much easier for admin-side plugins to add new UI elements to existing admin pages by manipulating the page DOM. The dom_attach function in txplib_html.php does the hard part:
$my_html = <<<EOF <h3>My UI thing</h3> ... EOF;dom_attach('article-col-1', $my_html);
Where article-col-1 is the ID of the XHTML element to which $my_html will be appended. $my_html may contain any XHTML, including javascript. The zem_article_info plugin gives a practical example.
While you’re there, take a look at script_js and toggle_box.
Textpattern now automatically uses timestamps and HTTP If-Modified-Since headers to enable browser-based caching of public side pages. Textpattern checks the last-modified timestamp and avoids sending a whole page, and heavy database access, unless the site has been updated since the previous request.
Plugins that need to manipulate caching have two mechanisms available. One is to reset the last-modified timestamp (which is stored globally for the whole site), using the update_lastmod function. Call this on the admin side when the user adds or edits database content.
Another option is to alter the content of the $prefs['send_lastmod'] global at plugin load time. Cache headers are sent after plugins are loaded, but before the page template is fetched or parsed. Use this if you need to selectively disable caching for certain sections or URLs.
Note that, for testing purposes, Textpattern only sends and accepts cache timestamps in Live mode.
As of 4.0.4, Textpattern’s built in database select functions (safe_row and the like) allow the use of commas in the $table parameter. They use the safe_pfx_j function to make sure the table prefix is correctly added to each table name.
In other words, you can join two tables like this:
$rs = safe_rows('bar.*', 'foo,bar', 'foo.bar_id=bar.id ...');
This will work regardless of whether or not the user has a table prefix set. Textpattern will look after the table PFX for you, and alias each name so you can refer to it without the prefix in the column and where clauses. There’s no need to fall back on safe_query() for common joins.
23 October 2006, 13:11 by Alex ·
Commenting is closed for this article.
Alex is a software developer from Melbourne, Australia. Threshold State is his consulting business.
“Labor is committed to introducing mandatory ISP filtering.” – Stephen Conroy, the new Communications Minister.
An excellent, minimal text editor for Windows.
All *.wordpress.com blogs have been blocked in Turkey – apparently because of one person, Turkish creationist Adnan Oktar.
The Opera browser team measured the percentage of people who use certain features. Several popular feature requests turned out to be unused, or almost so.
Love the dom_attach thing man, but the join code is saving me a ton of time ;-).
Btw: _dom_attach_ + textile
— Pedro Oct 24, 08:02 am #
I didn’t know that the sql join syntax was new to 4.0.4. I just used it in a plugin I created. Thanks!
— amit Oct 24, 11:52 am #
You could use the same join syntax with previous versions, but it only worked properly when PFX was empty.
— Alex Oct 24, 11:59 am #
Could you provide a minimal example of how to alter the article body for the feed? (I’d love to add the content of some custom field)
— Markus Oct 28, 10:07 pm #
Markus: added above.
— Alex Oct 29, 09:01 am #
so easy ;)
thanks a lot!
— Markus Oct 31, 05:49 am #
dont do it
— steve-edmands Jan 14, 01:58 am #