Introducing the mta_sanitize Textpattern Plug-in ¬

2008-06-30

I was running into a small issue on a project that I’ve been developing with the Textpattern CMS: I couldn’t find a tag to convert a title to a “URL-only title” like the Write tab on the admin side does for you. Textpattern has a built-in function named sanitizeForUrl() which provides this functionality, but no way to get that from the template system.

After looking for a tag that does this, I quickly whipped up a new plug-in: mta_sanitize.

When would you likely use this? Usually to assist in building links.

In my case, I needed to build a link in the form of “http://domain.tld/section/persons-name/” where the person’s name (first & last; properly capitalized) was stored in a custom field. The person’s name would be shown in one article, but needed to link to another article with more details.

I could’ve provided a link to the person’s detail article, but then I would’ve had to find a unique way to look up the article’s title using only the URL, title, or ID. I felt it’d be more flexible to store the name in a human readable format which wouldn’t require additional database lookups to display and so that left converting the name to a URL-friendly format. What better way than to use the same method that Textpattern already uses to convert titles to URL-friendly format?

With mta_sanitize installed, here’s an example of how I would build a link to a person’s detail article:

<a href="<txp:site_url />/section/<txp:mta_sanitize><txp:custom_field name="Person" /></txp:mta_sanitize>/"><txp:custom_field name="Person" /></a>

Which would result in the following HTML (assuming the “Person” custom field were populated with “John Doe”):

<a href="domain.tld/section/john-doe/">John Doe</a>

Hopefully others will find this useful. If you have any questions, comments, or feature requests, please drop me a line.

  Textile help