Making a better audio shortcode for WordPress


The Logo for WordPress.

If you use WordPress, you can get a fairly basic embedded audio player by using the audio shortcode: [​audio mp3="/path/to/file.mp3"] I didn't particularly like how it was styled so - because WordPress is so hackable - I changed it! Now my embedded audio looks like this: 🔊 Location Based QR Codes - Introducing http://xmts.mobi/🎤 edent 💾 Download this audio file. It gets a nice border, a title, displays any attached image, and uses the native HTML5 audio element. Here's the code …

Continue reading →

Use WP CLI to find all blog posts without a featured image - two methods


The Logo for WordPress.

This uses the wp shell command. It gives you an interactive prompt into which you can do various WordPress "things". One small annoyance is that it doesn't like multi-line entry. It treats every hit of the enter key as "plz run the codez" - so, at the end of this blog post, I've put the commands in copy-n-paste format. Once you've installed WP CLIP, go to the command line and run wp shell. You'll be greeted with an interactive prompt wp> Method One - Quick Search This command constructs a…

Continue reading →

Rewriting WordPress's JetPack Related Posts Shortcode


The new layout has 4 items, each boxed off, with a larger image and more distinct text.

I like the JetPack related post functionality. But I wanted to customise it far beyond what the default code allows for. So here's how I went from this: To this: Documentation The complete documentation for related posts is pretty easy to follow. This is an adaptation of "Use Jetpack_RelatedPosts_Raw to build your own list of Related Posts". Remove the automatic placement You can turn off the original "related posts" by adding this to your theme's functions.php: function…

Continue reading →

Improving the WordPress Comments Form with Client-Side Validation


The Logo for WordPress.

If you use WordPress's HTML5 comments, there's an annoying little gotcha. There's a four year old bug which prevents client-side form validation. HTML allows <input> elements to have a required attribute. In theory, that means the form shouldn't submit until the input is filled in. Sadly, WordPress uses novalidate on the form - as the name suggests it stops any validation. But! WordPress is very hackable. Here's how to make a comment form which does client-side validation and as a bonus…

Continue reading →

How to use the new <search> element with WordPress


The Logo for WordPress.

There's a new HTML element in town! You can now use <search> to semantically mark up a search box. It's great for letting screen-readers and other assistive tech know what a form does. It's only supported in WebKit for now - other browsers will get it eventually. The WordPress default search widget hasn't yet updated, but I'm going to show you how you can start using it today! In your theme, create a new file called searchform.php - WordPress will automatically load it for the search widget. …

Continue reading →

This blog is now on the Fediverse!


Setting screen showing the blog being enabled.

You can now have this blog federated to your social media site by following @blog@shkspr.mobi If you're on Mastodon, it should look something like this: You should be able to follow it on Lemmy, kBin, PixelFed, and some cool social network I've never heard of. How This blog runs on WordPress. Thanks to the tireless work of Matthias Pfefferle, there's now an official WordPress ActivityPub plugin. It's pretty easy to set up - just install and click on settings. But there are a few niggles …

Continue reading →

Getting WordPress / JetPack Subscriber Counts via the API... the hard way


The Logo for WordPress.

People can subscribe to receive my blog via email. This is managed by the JetPack plugin. I want to be able to display something like "Join 1,234 subscribers and receive updates via email". So, how do I get the subscriber count from the API? As documented in the JetPack HTTP API, it is possible to interact with JetPack programmatically. A good starting point is /wp-json/ - that will show you all the API endpoints available on your blog. By filtering on "subscribers", we find: …

Continue reading →

Automatically deleting WordPress comments using a theme


The Logo for WordPress.

Let's say you want to automatically delete specific sorts of comments from your WordPress blog. For example, immediately trashing any comment that has a specific phrase "Elephant Juice" in it. You can do this by going to Settings -> Discussion → Disallowed Comment Keys. Or you can add something to your theme's functions.php file. Here's the code snippet: add_action( 'comment_post', 'check_comment_content', 10, 2 ); function check_comment_content( $comment_id, $comment_approved ) { $…

Continue reading →

Should the WordPress scheduler use datetime-local?


The Logo for WordPress.

There's a brilliant post by WordPress about how they've optimised some of the backend code to make it more efficient. So here's a suggestion for something else which can be optimised. If you want to schedule a blog post to be published later, you have to use this WordPress control: I find it mildly annoying. I don't get why part of it is a dropdown. And the number fields don't pop up my phone's number keypad. And I have to look at a different calendar if I want to schedule something for a…

Continue reading →

Importing IntenseDebate Comment XML into Commentics


The Logo for WordPress.

This is ridiculously niche. If this is of help to anyone other than to me... please shout! The IntenseDebate comment system is slowly dying. It hasn't received any updates from Automattic for years. Recently it stopped being able to let users submit new comments. So I've switched to Commentics which is a self-hosted PHP / MySQL comment system. It's lightweight, pretty good at respecting users' privacy, and very customisable. But it doesn't let you easily import comments. Here's how I fixed…

Continue reading →

Build your own "On This Day" page for WordPress


A graphic of a calendar showing the date "February 25 Sunday"

I blog. A lot. Too much really. One of the things I like to do is see what I was rambling on about this time last year. And the year before that. And so on. So, here's my On This Day page and here's how I built it. WARNING Extremely quick and dirty code ahead! This allows you to add a shortcode like [ edent_on_this_day ] to a page and have it auto generate a list of posts you published on this day in previous years. You may need to exclude that page from your cache. Add these functions to…

Continue reading →

Style your WordPress Atom feed


A nicely formatted RSS feed.

I recently read Darek Kay's excellent post about styling RSS feeds and wanted to do something similar. So, here's my simple guide to styling your WordPress blog's RSS / Atom theme. The end result is that if someone clicks on a link to your feed, they see something nicely formatted, like this: Prerequisites This involves editing your WordPress blog's theme. If you don't know what you're doing, you can mess up your blog. Make sure you take some backups before experimenting on your live…

Continue reading →