File this in the tiny bits of code of almost nil importance. But it also reinforces that in building things like my SPLOT themes for other people, I’m not totally in the power to foresee all the things people will try.

One of the “features” built into TRU Writer, TRU Collector, and SPLOTbox is an ability to have a link that generates a link to a random item in the collection. Try:

In the old days, I had a page-random.php template that did this with something like:

These means that pre-creating a page with a permalin ending in /random did the trick.

Later I got fancier, and made it so the template and the page were not needed, all is done in the theme.

So we create a few available query variables, the thing that actually makes it work is a request to something like http://mycoolsplot.ca/writer/index.php?random=1 The themes have a few custom query variables, random is just one of the bunch.

Then we have a custom rewrite rule added when the theme intializes:

What this does is to make a request like http://mycoolsplot.ca/writer/random go to the one that WordPress really looks at http://mycoolsplot.ca/writer/index.php?random=1 .

But how to know what to do with it? We use another hook that gets tossed into the things WordPress looks for when unraveling URLs. It’s the same code I used before in the template.

That’s all fine. It means that all SPLOTs get the /random feature.

But I never guessed what might happen if someone creates a SPLOT item (or the site owner creates a Page) with that same permalink. I did not think about it until Brian Lamb shared a message from a faculty member using a SPLOT who wanted there to be something in the mix with a URL of /random.

The subject line of “I Think I Broke the SPLOTS!” included:

My class is enjoying SPLOTs-ville. :o)

A problem with ‘random”. My learner posted a comic about an encounter with something “random” and, as I asked, titled the post “random”. Now, when I click on that post to read to whole entry, the SPLOTs elves offer up any of the other posts in random order. Somehow titling an entry “random” is causing the SPLOTs machine to go into the mode of reading in random order.

This is exactly what my code was set up to do- it intercepts any post set up to use the permalink /random and serves up another one.

I thought of a few fixes. One would be a new theme feature to turn off the random link magic. But there’s already a long list of theme options.

The new approach maybe is heavy handed, but it prevents the unexpected randomness of /random. There is now a “hook” when posts/pages/splot contributions are saved- if their title is just “Random” we adjust it to be saved as /randomly. And this no conflict happens to make it get caught in the random spinner.

This is a standard approach you can use to change anything when saved. Maybe you want to add a special category. or tag Maybe you want to add something to the end of the post. Maybe… well you can do anything.

You can inject an action when a post is saved. Then you might check a condition that decides if it needs to do anything- in this cases, is it a post with a post_name (the permalink) of “random”. If so there is a counter-intuitive step in first you actually have to remove the very save_post action that triggered the response, you do the thing you want to do, and then reinstate the action.

The reason fo the removal and adding back of the action is otherwise, updating the post would trigger a save_post and then… infinite recursion.

This little bit of functionality has been added to the latest versions of TRU Writer, TRU Collector, and SPLOTbox.

Another way to avoid this situation is to veer away from the default WordPress setting for permalinks- it usually creates the web address for a post based on the title. If you have a really long title, you get a really long URL. I often use a permalink structure based on the database id. For the Splotbox demo site at http://splot.ca/box I use this Permalink setting:

Permalink Settings using Custom Structure of /%year%/%post_id%/

So this produces links like https://splot.ca/box/2020/244/ and https://splot.ca/box/2019/133/ and https://splot.ca/box/2018/61/ – this would never clash with /random.

There you go, a whole lot of explanation for something I never anticipated and for years, no one stumbled into. Still, it’s fun to work through a way to addresses these types of situations.



Featured Image: I made this from scratch. Shall I attribute myself? Sure. This image by me is shared into the public domain using Creative Commons CC0. Go have fun with it.

If this kind of stuff has value, please support me by tossing a one time PayPal kibble or monthly on Patreon
Become a patron at Patreon!
Profile Picture for CogDog The Blog
An early 90s builder of web stuff and blogging Alan Levine barks at CogDogBlog.com on web storytelling (#ds106 #4life), photography, bending WordPress, and serendipity in the infinite internet river. He thinks it's weird to write about himself in the third person. And he is 100% into the Fediverse (or tells himself so) Tooting as @cogdog@cosocial.ca

Leave a Reply

Your email address will not be published. Required fields are marked *