WordPress “Edit This” Links via Ajax

Working on a WordPress customisation recently, I added an “Edit This” link which only logged-in people can see. To get caching right, the server always outputs the same thing – an invisible link – and only in the browser does the decision get made to show the link or not. This exemplifies the pattern I only ever identified as “Ajax as a Remedy for the Cacheability-Personalization Dilemma”, but which I will now call “Browser-Side Personalisation”.

In this case, the personalised content is not secret, so it’s fine to output it in the HTML, and simply make it invisible by default. And it’s not mission-critical either, so the app degrades nicely if Javascript isn’t present – it simply won’t be displayed and the user will have to go into the “Manage Posts” area and look up the post from there.

To output “Edit This” from the server, within a WordPress loop:

javascript
< view plain text >
  1. <div class="editPostLink"><? edit_post_link() ?></div>

Links of this nature are rendered invisible within layout.css:

  1. .editPostLink { display: none; }

… but we switch them on if the user is logged in:

javascript
< view plain text >
  1. var loggedIn = /wordpress_logged_in/.test(document.cookie);
  2.   if (loggedIn) $(".editPostLink").show(); // it's easy with JQuery

Man pages that read more like legal contracts

Case in point: I want to use “find” to find files I’ve recently created.

man find (BSD edition):

-ctime n[smhdw] If no units are specified, this primary evaluates to true if the difference between the time of last modification time and the time find was started, rounded up to the next full 24-hour period, is n 24-hour periods.

If units are specified, this primary evaluates to true if the difference between the time of last modification time and the time find was started is exactly n units. Please refer to the -atime primary description for information on supported time units.

Humane documentation FAIL! The precision here is all well and good to include somewhere, but unnecessary for most usages. Please, just tell me what I need to know and Don’t Make Me Think!.

What I wanted to see:


-mtime t

Include only files modified t days ago. You will typically want to specify a +/- sign. e.g. “-ctime +3″ for files created 3 or more days ago. To measure in another unit of time, you can append one of the following to t: s for seconds, m for minutes, h for hours, d for days, w for weeks, but note that “-ctime” unfortunately only works if the time is at least one day. For example, “find . -ctime 1d6h” will return files created in the past 30 seconds.

Technical detail: (the legalese stuff goes here. 99% of users will never need it and never read it.)


The improved version focuses on what the typical user needs to know, uses active voice, avoids the cross-reference (it’s documentation, not code, so embrace redundancy!), and most importantly, includes an example. (The man page does include examples at the bottom, but (a) they should be included against each component too; and (b) they still shy away from concrete values: “-newer ttt”!!!)

A lot of these man pages have been around for two decades or more, with minimal changes to my knowledge; I’d love to see someone like Ubuntu sponsor an effort to bring them up to date. Plain English without dumbing down.

Guid0: A Javascript GUID Generator

Guid0.js

Guid0 is a GUID library for Javascript. Okay, it doesn’t yet do official, bona fide, 128-bit, GUIDs yet, mainly for API design reasons. But this is a library you might find useful if you want to generate a unique ID in your Ajax app.

Usage:

javascript
< view plain text >
  1. guid = new Guid();
  2. guid.generate(); // Returns a unique ID, e.g. "dkvagrkx1rt"

With options:

javascript
< view plain text >
  1. guid = new Guid(
  2.   {
  3.     chars: Guid.constants.base85,  // or you could say "abc" if you only wanted those chars to appear
  4.     epoch: "June 1, 2003",
  5.     counterSequenceLength: 2, // a counter field appended to the end
  6.     randomSequenceLength: 2 // a random field appended to the end
  7.   }
  8. )

The demo:

The demo generates a bunch of GUIDs. They look almost the same because most of the GUID is just a time representation in Base N (where N is the number of characters in the GUID’s configured charset).

I made Guid0 in conjunction with TiddlyWiki work. Within a store, tiddlers are keyed on their title. In the typical case, the user has entered a title to describe the content. But when we get into application territory – using TiddlyWiki as an application framework – there are reasons why we want the more familiar database approach of automagically generating the primary key. These are several reasons for this:

  • Tiddlers with no meaningful title. Sometimes, tiddlers are just containers for text someone has entered. A perfect example is the Comments Plugin I’ve been working on (separate blog post pending). A comment is a tiddler. When you have a blog or forum or whatever with comments, you usually don’t invite users title their comment…it’s not very enticing. So in this case, the comment is the tiddler text and the title must be auto-generated so we can put it in the store.
  • Renaming. As with the usual motivation for auto-generated IDs over meaningful IDs, renaming support is sometimes required. In one case, I have a user submitting a first cut of something, and an admin coming along later and cleansing it. We can’t expect the user to get the name right the first time, and there are several tiddlers referencing the initial tiddler (via custom fields) by the time it might be renamed.
  • Can’t ensure uniqueness. With multiple users, you don’t always want to force them into choosing different titles for each tiddler. If the comments plugin did include titles for instance, you wouldn’t want a uniqueness constraint. Using GUIDs, you could support multiple titles with the same display name.

The library is generic – not TiddlyWiki-specific – but I’ve wrapped it into a small TiddlyWiki plugin. In the future, we may expand the plugin. For example, each time a new tiddler is created, the plugin may intercept the call and give the tiddler a GUID title, and stick the title in a “name” field.

I’ll update this post when proper 128-bit GUID is supported.