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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// set arguments for WP_Query on published posts to get 1 at random $args = array( 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => 1, 'orderby' => 'rand' ); // run query run $my_random_post = new WP_Query ( $args ); while ( $my_random_post->have_posts () ) { $my_random_post->the_post (); // It's time! Go someplace random, have a great time wp_redirect ( get_permalink () ); exit; } |
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.
1 2 3 4 5 6 7 8 9 10 11 |
// ----- add url parameters so we can do cool stuff, wally add_filter('query_vars', 'truwriter_queryvars' ); function truwriter_queryvars( $qvars ) { $qvars[] = 'tk'; // token key for editing previously made stuff $qvars[] = 'wid'; // post id for editing $qvars[] = 'random'; // random flag $qvars[] = 'elink'; // edit link flag $qvars[] = 'ispre'; // another preview flag return $qvars; } |
Then we have a custom rewrite rule added when the theme intializes:
1 2 3 4 5 |
/* set up rewrite rules */ function truwriter_rewrite_rules() { // for sending to random item add_rewrite_rule('random/?$', 'index.php?random=1', 'top'); } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
// redirections for rewrites on the /random and /get-edit-link add_action( 'template_redirect', 'truwriter_write_director' ); function truwriter_write_director() { if ( get_query_var('random') == 1 ) { // set arguments for WP_Query on published posts to get 1 at random $args = array( 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => 1, 'orderby' => 'rand' ); // It's time! Go someplace random $my_random_post = new WP_Query ( $args ); while ( $my_random_post->have_posts () ) { $my_random_post->the_post (); // redirect to the random post wp_redirect ( get_permalink () ); exit; } } } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
add_action( 'save_post', 'splot_save_post_random_check' ); function splot_save_post_random_check( $post_id ) { // verify post is not a revision and that the post slug is "random" $new_post = get_post( $post_id ); if ( ! wp_is_post_revision( $post_id ) and $new_post->post_name == 'random' ) { // unhook this function to prevent infinite looping remove_action( 'save_post', 'splot_save_post_random_check' ); // update the post slug wp_update_post( array( 'ID' => $post_id, 'post_name' => 'randomly' // do your thing here )); // re-hook this function add_action( 'save_post', 'splot_save_post_random_check' ); } } |
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:

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.
