Go to content Go to sidebar

Textpattern and custom_url_func

One of my biggest concerns in moving from Radio Userland to Textpattern was preserving inbound links. My initial approach used mod_rewrite and year-month-day mode. This was fairly effective.

But I wasn't completely satisfied. Year-month-day mode is based upon GMT, moving posts after 7pm moved into the next day. And it never really felt like a good long-term solution.

I didn't want to wait for the ideal solution, so I added an id based RewriteRule, created index pages using the newly defined id based links, and started a project to update all my internal links. This allowed me move all my content from Radio Userland to Textpattern.

Now, my only problem was that Take the First Step was using two url modes: year-month-day for Textpattern generated links and my id mode for manually generated links in my content.

The final piece of my solution was “custom_url_func”. I added a plugin that codified my id mode as a “custom_url_func”, and unified all my internal links into a single mode. The entire solution looks like this:

  1. mod_rewrite and year-month-day mode to preserve old inbound links.
  2. more mod_rewrite to handle an alternate link mode of id/NNNN/url-title. The new rules simply extract the post id from the link and pass it on as part of the query string.
    
     RewriteRule ^id/([0-9]+)/?$ index.php?id=$1 [L]
     RewriteRule ^id/([0-9]+)/[^/]+$ index.php?id=$1 [L]
    
  3. A custom_url_func plugin to specify an alternate link format for all Textpattern generated article links. The business end of the plugin follows:
    
    function idx_permlinkurl($article_array)
    {
    	global $prefs;

    if (empty($article_array)) return; extract($article_array);

    if (!isset($title)) $title = $Title; if (empty($url_title)) $url_title = stripSpace($title); if (empty($thisid)) $thisid = $ID;

    if ($prefs['attach_titles_to_permalinks']) { return hu."id/$thisid/$url_title"; } else { return hu."id/$thisid/"; } }

    $prefs['custom_url_func'] = 'idx_permlinkurl';