Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

« Newer Snippets
Older Snippets »
17 total  XML / RSS feed 

Not This Section

// description of your code here

blah blah

Serving links to multilingual pages

Let’s assume you want to have two sets of pages which would be crosslinked with links like “see this page in [another language]”. Set custom fields for the languages you want to support, name them, for example eng and ukr (for English and Ukrainian).

Write articles for those languages and assign them links of their sibling articles on the other language, say, you have an article in English with permanent link “/about/my-dog” and you have the same article in Ukrainian with permanent link “/about/my-dog-ukr”, then set “/about/my-dog” to the custom field “eng” in the article on Ukrainian and “/about/my-dog-ukr” to the custom field “ukr” in the article on English.

Then in the article form you use write a code similar to the following:

<txp:if_custom_field name=“eng”>
<a href=”<txp:custom_field name=“eng” />”>this page in englisha>

<txp:if_custom_field name=“ukr”>
<a href=”<txp:custom_field name=“ukr” />”>this page in ukrainiana>

That’s it! When it’s English link to Ukrainian article will be shown up and vice versa.
It’s just a basic example to give you an idea, hope it helps.

Article list from same category / section as current article

From a post on the Textpattern support forums.

<ul><txp:asy_wondertag><txp:article_custom limit="9999" form="listarticles" section="" category="" />txp:asy_wondertag></ul>

Return a message on blank search form submit

From a post on the Textpattern support forums.

<txp:php> if(gps('q') == '') echo 'Hey, remember to search something!'; txp:php>

Post link to POOKMARK with Textpattern

Simple link to post each individual article to Japanese social bookmark "POOKMARK".


<a href="" title="">POOKMARK THISa>

Technorati Link Count Widget for Textpattern

Technorati tracks when other bloggers link to your blog and this widget makes it possible for you to display the number of links on every blog post. We call them "reactions" to encourage readers to follow the conversation. To add the widget, copy and paste the code below into your blog's template.

<script src="" type="text/javascript">script>
//<txp:permlink />" rel="linkcount">View blog reactions

Adding "post to" links for, digg, and ma.gnolia in Textpattern

Here's how to add "post to" links for, digg, and ma.gnolia to individual articles in Textpattern

post to <a href="">del.icio.usa> 


post to <a href="">digga>


post to <a href="">ma.gnoliaa>

Clicking one of the those links will automatically post a permanent link for an article and, in the case of and ma.gnolia, the title to one of the above services.

Photoblogging with Textpattern: atom feeds

To textpattern/publish/atom.php somewhere around line 131 (in 4.0.2), or after
else {
$Body = '';
// If there's no excerpt, use body as content instead of body as summary
if (!trim($Excerpt))
$Body = fixup_for_feed($thisarticle['body'], permlinkurl($a));

add this:
// hack to get pictures to syndicate
if ($a['Section'] == "photo")
$img_url = hu."images/".$a['Image'].'.jpg';
$Body = doSpecial(''.permlinkurl($a).'">'.$img_url.'" alt="the latest and greatest" />');
// /hack

Photoblogging with Textpattern: rss feeds

Add this to textpattern/publish/rss.php somewhere around line 62 (in 4.0.2), or after "$Body = (!trim($Body)) ? $thisarticle['body'] : $Body;":
// hack to get pictures to syndicate
if ($a['Section'] == "photo")
$img_url = hu."images/".$a['Image'].'.jpg';
$Body = doSpecial(''.permlinkurl($a).'">'.$img_url.'" alt="the latest and greatest" />');
// /hack

Testing if this is NOT a category listing

The new releases of textpattern tend to use the new "" url format for category listing links. Which means that those category-filtered archives use the "default" template for formatting & so forth.

To handle this on the front page, you need to be able to test if the current view IS a category search, and you also need to test if this ISN't a category search (to display normal front-page content). To do this I had to modify the glx_if plugin - very easy really.

In Textpattern, go to admin > plugins, and click the "edit" link for glx_if. Add the following function anywhere it would seem to fit:

function glx_if_not_category_list($atts, $thing)
        global $pretext, $is_article_list;
        return (empty($pretext["c"]) && $is_article_list == true) ? parse($thing) : "";

A use for this is shown in this snippet.

Low-Overhead Archives By Month

You used to have to use rss_suparchive to get an archive of articles broken out by month, but this plugin produces an ungodly number of db queries when dealing with large archives (e.g., hundreds or thousands of articles).

Textpattern 4.0.2 allows you to get the same effect using the good ol' tag, which uses a LOT FEWER database queries.

You can see this in action at Not Far North.

First, you need the glx_if plugin installed.

put this in your "article" page template:

/> dl> </txp:glx_if_section_frontpage> <txp:if_individual_article> <txp:article /> txp:if_individual_article>

If you were attentive, you see that we also need an article_archive form. Create that form and put this in it:

  <dt><txp:posted format="%B %Y" />dt>

<dd><txp:permlink><txp:title />txp:permlink>

/><txp:excerpt />dd>

Now, "" will display a list of articles broken out by month, and "" will display only that article using the "default" form.

To handle category listings, install the upm_category_title plugin and add this to your front page ("default") template:

   <h2>Articles in <txp:upm_category_title />h2>

/article/">View all articles

"archivelist"> "article" form="article_archive" />
"2" form="article_newest" />

Note that using the above bit of code requires you to modify the glx_if plugin a bit - more about that in another snippet (it's really easy).

Plugin development workflow improvements

By Sencer on the TXP forums

Make your life developing plugins significantly easier:

1. Use zem�s tamplate.
2. insert row into txp_prefs table with

name = �plugin_cache_dir� and val = �/full/path/www/textpattern/tmp�
(you can let the other ones default to their respective default value)
3. create a directory /full/path/www/textpattern/tmp
4. drop your plugins (uncompiled) in that directory (make sure plugin name and plugin-filename correspond, e.g. zem_test should be in zem_test.php)
(Note: do not put zem_tpl.php in the same directory)

Textpattern clean urls with Lighttpd, the elegant way

I love this shit:

server.error-handler-404 = "/index.php"

Switching a working copy with subversion

If you find that the location to a subversion repository has changed, do this:

svn switch --relocate

Where the old url is

And the new one is

To see if it worked, do this:

svn info | grep URL

And you should see something like this:


(source: Switching a Working Copy)

Textpattern form for Paypal shopping-cart integration

A site I've been working on has a retail area which uses Paypal's shopping cart; since the whole site is Textpattern powered, the necessary HTML for each item is generated by a form. This code is based on 1.0rc1 and using the rei_show_custom plugin. I know that in rc3 there's probably something built in to pull out arbitrary custom fields, so just substitute that.

To use, add each product as an article; in the first custom field put the name you want passed to the cart, and in the second custom field put the price of the item. Then make sure this is called by the article form:

<legend>Buy this product:legend>

Our fine /> is available for the low, low price of $<txp:rei_show_custom customid="2" />.p>

/> <input type="hidden" name="business" value="yourbiz@yourdomain" /> <input type="hidden" name="item_name" value="" /> <input type="hidden" name="amount" value=""2" />" /> <input type="image" id="add_to_cart" src="/path/to/your_cart_image" name="submit" alt="Add this item to your cart" /> <input type="hidden" name="add" value="1" /> p> </form> <form action="" method="post"> <p> <input type="hidden" name="cmd" value="_cart" /> <input type="hidden" name="business" value="yourbiz@yourdomain" /> <input type="image" id="view_cart" src="/path/to/your_view_cart_image.png" name="submit" alt="View all items currently in your cart" /> <input type="hidden" name="display" value="1" /> p> </form> fieldset>

That'll display both an "add to cart" and a "view cart" button.

You'll want to fill in your own Paypal account address and product options, of course.

Upload all .gif and .jpg files in a directory to textpattern

This will upload all files in the current directory ending in jpg jpeg or gif through your textpattern-admin panel to your site.

Save this as a shell script, adjust the first 3 variables and excute. (Note: LOGIN and PASS should be ascii only, or else manually url-encoded)


COOKIE=$(curl -s -D - -d "p_userid=$LOGIN&p_password=$PASS" \
    $URL | head -n10 | sed -n 's/^Set\-Cookie\: //p')

if [ -z $COOKIE ] 
  echo "Can't log in."
  exit 1
  echo "Cookie: "$COOKIE

for file in $(ls -1|egrep '(gif)|(jpe?g)$') ; 
 echo "Sending "$file
 curl -s -H "Cookie: $COOKIE" -F "thefile=@$file" \
   -F "event=image" -F "step=image_insert" $URL > /dev/null

This will not output anything. But after it returns to the prompt, log in to textpattern and take a look into the image section.

Update: Added rudimentary check wether login is successful and basic progress meter.

Clean Textpattern install from the current repository

In a shell, cd to the directory from which you'd like to serve the site, and rename or delete any '.htaccess' or 'index.php' files, and any directories called 'images' or 'files', that may be there.

Checkout the current code:

svn co .

(Note the dot at the end). To complete the installation, just load http://thesiteurl/textpattern/ in a browser, fill in your database info, and when presented with the config.php block, copy it, go back to the shell client and:

nano textpattern/config.php

Paste in the code block, save and exit (ctrl-x, then y, then return).

Back to the browser, click through, add personal details, and that, as they say, is it.

Whenever you want to update to the latest code, cd to the same directory and:

svn update

...and the latest code will be seamlessly integrated with your installation.
« Newer Snippets
Older Snippets »
17 total  XML / RSS feed