QR Code Puzzle

For a few years, my family and I’ve done some birthday trolling of a kid my wife and I have watched grow up. This year, he graduated high school, and it seemed like maybe it’d be the beginning of the end of the times during which we’d do big involved birthday things for him (he’ll grow up, move away, no longer be a kid you can really troll, etc.). So I decided to do a more elaborate setup than usual. I sent him on a multi-stepped puzzle scavenger hunt for which I acquired props such as a hollowed-out book, a cipher wheel, a wooden puzzle box, and a cryptex. I came up with a few riddles and lined up the puzzles in such a way that one, once solved, pointed to the next.

The first puzzle was a sheet of paper with only this printed on it:

Huh? What the heck is that? I also had this ready to share in case he needed it:

You can begin to see that there’s a pattern. If you know that it’s a birthday thing, you can maybe begin to see that the patterns might read (in part) “Happy B-Day” if the squares with the slants pointing from bottom left to upper right are filled in. Eureka! You’ll now know that you need to shade in the equivalent squares on the other sheet. And when you do that, you’ll get something like this in place of the first image above (slightly modified, so it doesn’t point to anything real):

That’s pretty quickly recognizable as a QR code. It linked to a little web site I set up that offered a riddle that, when solved, pointed him to the next clue, and so on.

I fancy myself a bit of a nerd, but this felt like really nerdy stuff indeed. But that’s not all!

See, when I first made the QR code thing, I drew it by hand, using drawing software on my computer. First I had to make a 25×25 grid, and then I had to print out the QR code I had created for the web site and copy its filled-in squares onto the grid as diagonals. Then I had to fill in all the other diagonals going in the opposite direction. Then I had to print it out, fill in the squares by hand using a Sharpie (to test real life operating conditions), and hope I hadn’t gotten any of the 625 little diagonal marks mixed up (spoiler: I messed them up a number of times because it was a tedious, manual process). Then I’d lather, rinse, repeat until I got it right. It was tedious, but finally I had a file that worked.

And then the QR code that I created using an online generator expired. I hadn’t realized it would have an expiry. This meant I’d need to make a new QR code and repeat the tedious (and actually sort of painful, repetitive-stress-injury-inducing) process all over again.

I wrote code for many years professionally and for side projects, so at this point, the lazy programmer in me woke up. I figured it wouldn’t be too hard to write some little program that’d output the QR code for me and save me lots of frustration — and also be pretty easy to reuse later should I have another QR code expiry issue or another weird project for which this pretty narrowly-focused output would be useful.

I knew how to output image files, and I knew how to use php code to draw simple lines and shapes. The rest was just figuring out how to input the QR code data and how to make it fairly configurable, so that I could run the same code with different options set and output either diagonals or filled-in squares, saving me both drawing and testing time any time I needed to tweak the QR code.

It’s not especially elegant code, but what I came up with is here. A next update should I ever go back to it would have me figuring out how to just turn a url into a QR code and automatically outputting the image based on a url input, whereas right now, you still have to fill in a data array using ones and zeroes to sort of map out the squares in the code. But for now, it’s a fun little piece of code with a practical — if rarely practical — application. Should you ever have a need to generate such a thing, the code is yours for the taking. It really does make it very easy to map out a QR code and easily display either the shaded version or the more cryptic diagonal version.

Mostly I wanted to share it as an example of how a silly/fun idea can turn into a neat little code project that can later save time, as I spent less time and much less frustration writing the code for this than I did creating the original version manually.

Using xdebug with wp-cli

Sometimes when you want to profile something in WordPress using xdebug, it’s not quite as straightforward as just enabling the tool in php.ini and passing a query string parameter. Say for example that you’re wanting to test how expensive it is to delete a term in WordPress. Well, you can load the term page with the query string parameter in place, but then you still wind up posting data without the query string parameter. You have a few options that I know of:

  1. Turn on xdebug for every request and figure out which cachegrind file contains the bits you actually want to profile.
  2. Hack the code to add the query string to your post.
  3. Write a cookie that xdebug can use in place of the query string variable.

None of these options especially appeal to me. They just seem clumsy.

I’ve used wp-cli more and more lately to interact with WordPress installs. And of course wp-cli has commands for interacting with terms. So it’d be neat to be able to profile wp-cli commands using xdebug. Luckily, it turns out to be pretty easy to do.

First, get xdebug and wp-cli set up. Links above will point you to documentation for doing so. When I set xdebug up, the extension was added to apache’s php config but not to the cli version of php.ini, so I had to copy the relevant line (extension=/path/to/xdebug.so) from one php.ini to the other.

Now you just have to add the -dxdebug.profiler_enable=1 option to your call to php at the command line. With wp-cli, you’re not calling php directly at the command line, though, so you have to figure out how to get the option passed through to the actual call. It turns out that the bash script that wraps wp-cli has a handy argument named $WP_CLI_PHP_ARGS that you can use to pass php arguments.

So if you always want to use xdebug with wp-cli, you can just do something like this in your .bash_profile:

export WP_CLI_PHP_ARGS=-dxdebug.profiler_enable=1

But maybe you don’t always want to profile wp-cli. Doing so slows operations down a little, and it generates pretty big cachegrind files, especially for complex or long-running operations (for example, a simple wp term delete command that takes around 2 seconds was generating 5MB cachegrind files for me). To run xdebug with wp-cli only when I really wanted to and without having to remember to set or unset $WP_CLI_PHP_ARGS when I wanted to toggle, I just added these lines to my .bash_profile:


alias wp="export WP_CLI_PHP_ARGS=; $HOME/.wp-cli/bin/wp"
alias wpd="export WP_CLI_PHP_ARGS=-dxdebug.profiler_enable=1; $HOME/.wp-cli/bin/wp"

Now, if I want to generate cachegrind files to profile, I execute wpd instead of wp, and life is good.

Custom Feed Links in WordPress

At a WordPress meetup tonight, the question arose of how to override default feed links for a WordPress site. For example, what if you’re using FeedBurner and want to just change the links in your source to the relevant FeedBurner links without hacking your theme? I don’t know if it’s the best way, but it looks like this is pretty easily done with a plugin that, in its simplest form, looks like this:

<?php
function feedme_remove_feed_links() {
        remove_theme_support( 'automatic-feed-links' );
}
add_action ( 'after_setup_theme', 'feedme_remove_feed_links', 11 );

function feedme_add_feed_links() {
?>
<!-- CUSTOM FEEDS HERE -->
<?php
}
add_action( 'wp_head', 'feedme_add_feed_links' );

Of course, you would need to make the feedme_add_feed_links() function do something a bit more useful, and in an ideal world, you’d provide an admin screen that allows people to specify their links.

One important detail that may not jump out at you is that when adding the “after_setup_theme” action, you need to give it a priority higher than 10. Else it just goes into a stack with all other of its sibling actions with the default priority and may be (in fact seems to be) overridden by one of them.

Stage

For a long time at my day job, one of our big web site issues has been the staging of database-driven content. Particularly if you’re editing Drupal pages that have a lot of markup in them, publishing a node can be sort of scary, as it goes live instantly with any bugs you’ve introduced. In theory, Drupal’s preview feature can be used to view your changes before you commit to them, but this too is scary, as the content isn’t rendered exactly as it will be once published. Further, using vanilla Drupal with its preview function to stage content requires that you roll out changes one by one. If you want to group changes for a mass rollout, the best you can do is wrap your changes in html comments and uncomment them one by one during deployment, hoping you don’t fat-finger anything in the process. I’ve always thought this would be a pretty difficult problem to solve, but yesterday, I came up with what feels like a satisfactory method for staging content.

The new stage module addresses both safety-netted staging of individual content and management of change sets.

It works by tapping into Drupal’s revision system, which already allows you to track changes to content over time and to revert to older content. For specified types of content, any additions or edits are published using the normal Drupal workflow, but on publish, the revision number is pinned at its last blessed point. You can edit or add any number of documents, and they all remain pinned at their pre-edit revision until you roll the whole batch of changes forward. When you roll a batch forward, all the revision numbers are brought to their most recent and pinned there until the next deployment. In the administration section, you identify staging and production servers. If you view an affected node from one of the specified staging hosts, you see the latest copy; if you view it from a production host, you see the pinned version.

This workflow is ideal for environments in which fairly frequent milestones are deployed. Because of Drupal’s handy dandy revision system, you can compare versions of the content across pushes to see what’s changed.

The module is hot off the presses this morning and so is probably still buggy and feature-poor, but it’s a start.