Named Alternates for WordPress


Screenshot of Lynx, the text browser, showing named alternates.

HTML documents have the concept of an alternate representation of the document. For example, a page's header might say: <link rel="alternate" type="application/rss+xml" href="https://shkspr.mobi/blog/feed"> That tells you there's an alternative representation of the page, what sort of content it is, and where it is located. That's nice. But it's hard for a browser to tell the user what that page is. It might be able to guess from the type, but it isn't always certain. So the <link> element …

Continue reading →

link rel="alternate" type="text/plain"


The Logo for WordPress.

Hot on the heels of yesterday's post, I've now made all of this blog available in text-only mode. Simply append .txt to the URl of any page and you'll get back the contents in plain UTF-8 text. No formatting, no images (although you can see the alt text), no nothing! Front page https://shkspr.mobi/blog/.txt This blog post https://shkspr.mobi/blog/2024/05/link-relalternate-typetext-plain/.txt A tag https://shkspr.mobi/blog/tag/solar.txt This was slightly tricky to get right! While there…

Continue reading →

A completely plaintext WordPress Theme


Screenshot showing my blog rendered just as text.

This is a silly idea. But it works. I saw Dan Q wondering about plaintext WordPress themes - so I made one. This is what this blog looks like using it: The Code You only need two files. An index.php and a style.css. The CSS file can be empty, but it needs to exist - otherwise WordPress won't let you activate the theme. The index file displays the requested post, or front page, in plain text. It isn't the most sophisticated code I've ever written! header(&#039;Content-Type: text/plain; …

Continue reading →

WordPress GeSHi Highlighting for Markdown


The PHP logo.

I've launched a WordPress Plugin for an extremely niche use-case. WP GeSHi Highlight Redux works with WordPress's Classic Editor to convert Markdown to syntax highlighted code. That allows me to write: ```php $a = "Hello"; $b = 5 * 2; echo $a . str($b); ``` And have it displayed as: $a = "Hello"; $b = 5 * 2; echo $a . str($b); I've previously written about the WP GeSHi Highlight plugin. My plugin is a fork of that. It has the following changes: RSS & Atom feeds - disable code…

Continue reading →

Server-Side Rendering of Embedded Markdown Code Snippets in WordPress


The Logo for WordPress.

Because I'm a grumpy old man, I don't use Gutenberg or Block themes on my WordPress. Instead, I write everything in Markdown. When I write code snippets in Markdown, they look like this: ```php $a = 1; echo $a; if ($a < 5) { // Do Something return thing( $a, true ); } ``` But I want to render that with code highlighting. I was using the Prismatic Plugin. It is excellent and very customisable. But it uses JavaScript to do the code highlighting. I want to respect my readers' time and …

Continue reading →

3,000 blog posts!


The Logo for WordPress.

This is the 3,000th blog post I've published on this site! Bloody hell! I first started a blog on Blogger.com in 2004 - twenty years ago. Like all blogs, I managed half a dozen posts before I forgot about it. Cut to 2007 and I decided to launch shkspr.mobi as a weird site dedicated to rendering Shakespeare's plays in txt spk. Judging by Archive.org I was still using Blogger. By 2008 I was blogging most months. And then I never really stopped. In early 2009 I switched to WordPress which led…

Continue reading →

A personal WordPress MonoRepo for my themes and plugins


The Logo for WordPress.

I use a self-built WordPress theme for this blog. I also use a variety of self-developed WordPress plugins for various enhancements. I used to publish these plugins, but I get terribly confused by the SVN shenanigans involved, and they weren't used by many people, so I stopped. Recently, I've been moving all my plugin code into my theme. This is sort-of-but-not-quite a MonoRepo. I've also tried to move away, as far as possible, from using other people's plugins. Most of the ones I had were …

Continue reading →

A library of all my book reviews


A grid of books with their titles and star ratings.

One of the things I love about having a database-backed blog like WordPress is that's it opens up a delightful range of possibilities for displaying content. I've read and reviewed around 300 books over the last few years. So I wrote a scrap of code which goes through all my book reviews, grabs their cover and rating, and displays them in a nice grid. You can visit it at shkspr.mobi/blog/library You know how when you're at a friend's home you rummage through their bookshelf? That's the same…

Continue reading →

Publish Confirmation For WordPress Classic (2023)


Screenshot of a page asking for confirmation before publishing.

Here's a quick scrap of code that works. There are lots of outdated tutorials out there for old versions of WordPress. This one is tested to be working in WordPress 6.3.2. This will pop up a confirmation dialogue when you try to publish, update, or schedule a post or page. The Code Add this to your theme's functions.php file: add_action( "admin_footer", "confirm_publish" ); function confirm_publish() { echo <<< EOT <script> var publishButton =…

Continue reading →

Find WordPress featured images with no alt text


The Logo for WordPress.

WordPress allows you to set a featured image - called a "thumbnail" in the API. This gives a single image which can be used on a listing page, or shown when a post is shared on social media. The WordPress Media Library lets you set the alt text of an image. But, crucially, this alt text can be different when the image is used as a featured image. Here's how to find all your featured images which don't have alt text. One Liner Paste this into wp shell to get a list. foreach (get_posts(…

Continue reading →

Find the URl causing your WordPress Error


The Logo for WordPress.

PHP has some pretty good error handling and logging, but I do sometimes find it confusing. For example, look at this warning message: [18-Oct-2023 12:34:56 UTC] PHP Warning: Something bad happened in /wp-content/something.php on line 123 OK, so we can go to something.php and scroll to line 123 and try to figure out what's gone wrong. But we don't know which page, post, or URl caused the error. If the error only occurs on /page/test?input=6 and not /page/test?output=7 that would be handy…

Continue reading →

Displaying internal linkbacks on WordPress


Screenshot of my website. The headline says "What links here from around this site." Underneath are three links.

I have written a lot of blog posts. In some of those posts I link to other posts on my site. What's the easiest way of displaying those internal incoming links? Here's what it looks like: Code All we need to do is search WordPress for the URl of the current page. Loop through the results. Then display those links. $the_query = new WP_Query( array( 's' => get_the_permalink(), // This post 'post_type' => 'post', // Only posts, not pages "posts_per_page" =>…

Continue reading →