CNR: Little Touches – Slug Editing

By Sol in Lab

One of Cornerstone’s key features is its structured permalinks that align post permalinks with the permalinks of the sections they’re in. Yesterday, a small, yet important bit of functionality was added to this feature— slug editing.

All of WordPress’ default permalink structures allow you to edit the slugs— or names— of posts and pages from the post/page edit screen after they have been created. It’s a little touch, but it’s an important one because it keeps the control in the user’s hands. Up until now, Cornerstone’s permalink structure has been left out of this game, meaning users could not partake in slug editing from the post edit screen if they were using Cornerstone’s permalink structure.

Enabling editable slugs was a fairly simple task. In fact, WordPress does most of the heavy lifting for you; all you need to do is give it something it can work with.

All of the magic happens in get_sample_permalink_html(), which starts off by calling get_sample_permalink(), so I guess at least half of the magic happens in that function. Anyway, this function builds a permalink in much the same way as the normal get_permalink() function, so the same hooks that you’ve registered to implement a new permalink structure will be called here as well. The key difference is that for slug editing to work, %postname% must be used in place of the post’s actual slug so that WordPress knows what part of the permalink can be edited. Thankfully, WordPress set’s the post object’s filter to “sample” prior to passing it to your custom permalink functions so it’s pretty simple to determine when WordPress is requesting an editable permalink.

//Use placeholder instead of actual name for sample permalinks
if ( isset($post->filter) && 'sample' == $post->filter )
	$name = '%postname%';

WordPress pretty much handles the rest from there, replacing %postname% with the post’s slug and formats it so that it can be edited.

So instead of

You now get

And with a quick click of the Edit button, you can experience the joy of slug editing