Today I present PHP code to remedy a long-vexing (to me) deficiency in WordPress.
Zero-day use cases
The first-day needs of those starting with new software are what I call “zero-day use cases”. For example, users of a word processor require the ability to change typefaces and apply bolding to text usually pretty early in their experience. Footnoting is rarely needed immediately, if ever.
WordPress is pretty awesome, from its five-minute install to a user interface that allows those non-technical to create and maintain websites and blogs.
Irksome deficiencies and bizarre dichotomies
Two things which seriously irritate me about the stock deployment of WordPress are
- the bizarre dichotomy, foisted on users, between pages and posts. When programmatically dealing with the WordPress under the hood why should I have to think about two fundamentally different kinds of things when they’re both just content, albeit with pages not being anchored in the timeline. Grrr. I’ll explain below.
- the inability to simply link to another page or post in the same website. There are so many times when I want to refer to something else I’ve written, like about that great video I recently made.
A programmatic statement of the problem
What follows is how I’d like to refer to other pages or posts, some syntactic examples, and some error cases.
My use cases
The basic idea is that I want to create a shortcode to allow me to link to another page or post, knowing only the title, not caring about whether it was a page or a post.
[link t="title of page or post" a="optional alternative text"]
The first of two use cases is using the title as the text displayed in the link.
[link t="Migrating an HTML website to WordPress, pt. 1"]
This shows up as Migrating an HTML website to WordPress, pt. 1.
The second use case is supplying some alternative text as the link text.
[link t="Migrating an HTML website to WordPress, pt. 1" a="moving to WordPress"]
Which shows up as moving to WordPress.
You’ll notice I don’t specify whether the target is a page or a post; the truth is I can’t remember and don’t want to care. The computer ought to do my bidding. This shortcode works.
Showing you success is nice, but how we handle errors is vital. Here are common types of errors; missing arguments and passing arguments with typos or other errors.
[link] [link t=""] [link t="tpyo"]
Errors look like this: [link] shortcode error: can't find "tpyo".
I looked for solutions for those who aren’t PHP programmers or uncomfortable mucking about in WordPress installations. I found nothing. Let me know if you find something to offer. In the meantime, adding this functionality is reasonably simple; there’s one step: insert the bit of code shown below into the
functions.php file in your CHILD THEME.
A programmatic solution
The solution to this problem, which you’ll find in this gist, implements the functionality described above.
Place the code between the
< ?php and
?> found in the
functions.php file of your theme (or, more properly, your child theme), will implement the link shortcode as described above. Themes are located in