Dynamically Overriding a Page’s Template

It is easy to override the template for a post or page in WordPress in a few ways. For example, the naming of page template files can be used to override a single page’s template:

  • page-{id}.php
  • page-{slug}.php

Or, a page template can be chosen in the Page Attributes section when editing a page:

Choose a Template

This works great when you always want a page template to be used for a particular page. But what if you need to change a page’s template based on something that might change? Like the current user’s role, for example? Well, you can use the template_include filter in WordPress to override the normally-used template in a function.

Start out by attaching a function to the filter:

add_filter( 'template_include', 'dynamic_page_template' );

This can be added to your theme’s functions.php file or to a custom plug-in. In this example, the function called dynamic_page_template will be invoked. This function will accept the parameter, $page_template, which is the $page_template that is to be used. If you want the page template defined in a way above (by the template name or the selected template in the Page Attribute area) to work as normal, simply returning $page_template will accomplish this. However, we can also set $page_template to something different and return that instead. Here is a simple example:

function dynamic_page_template( $page_template ){
  global $post;
  if (100 == $post->ID) :
    $page_template = get_template_directory() . "/page-test.php";
  endif;

  return $page_template;
}

In this example, we are pulling in the $post (or page) information by calling it globally. Then, we can check for the post ID value. If the ID is 100, we are setting the page template dynamically to the page-test.php file in our theme. The get_template_directory call gets us the directory for our theme. Now, even if we have a file named page-100.php, our function will take precedence.

“From Bedrooms to Billions” Movie Review

From Bedrooms to BillionsThose who know me know that I am hopelessly mired in the past. I have a working Atari 7800 and an Atari Jaguar (and regret parting with my Atari Lynx and Falcon). I still like 80s music, tv shows and movies. And I make regular pilgrimages to Funspot in New Hampshire, a great classic arcade to scratch that itch periodically.

So, I am really the target audience for a movie like “Bedrooms to Billions,” a documentary about the computer programming renaissance that occurred in the U.K. during the 80s with the introduction of 8 and 16-bit home computers. While I didn’t have any exposure to more European-centric machines like the famous ZX Spectrum, I did get some keyboard time on Atari 8-bits and STs as well as Commodore 64s and Amigas. I was a big fan of the underdog machines and was disappointed to see them disappear when the computer market became boring commoditized PC clones. And, I happen to be a web developer these days so I am still passionate about computers.

Yet, and it pains me to say, I found “Bedrooms to Billions” to be fairly boring. Many of the stories are very matter-of-fact and there wasn’t much depth given to the people behind the software and the machines. I wanted more of a sense of the work environments in those days and some of the inspiration behind the games. Much of the information seemed to be of the variety of how much shipments grew year-over-year. The end of the documentary ends on a discussion of computer science education in the UK today.

Another disappointment for me was that there was very little Atari talk. Obviously, I am biased but I was hoping for more discussion of the Atari ST which was very big in Britain and really led the charge in the 16-bit era before being overtaken by Commodore’s Amiga. The movie seemed to be heavily weighted toward the ZX Spectrum to me and really breezed by later machines.

There certainly is some good things to say about “Bedrooms to Billions.” From the beginning credits, it is obvious that this a well put-together production. Some really cool graphics and chip tunes really got me excited for the movie to begin. And, the few personal stories were interesting. I just couldn’t help but think that Jeff Minter would have a fascinating story or two. It was cool to see folks I really admire like Jeff Minter and Peter Molyneux. I also learned a bit about the ZX Spectrum which, as a kid growing up in the United States, I knew very little about.

In the end, I don’t think I can really recommend “Bedrooms to Billions.” I was hoping for more fun stories like those in Steven Levy’s “Hackers” or Howard Scott Warshaw’s “Once Upon Atari.” I am sure that many people will like this more straightforward run through computer history but, for me, it just didn’t hold my interest.

A Couple Quick Mac Tips

Just a couple of quick Mac tips. First, many programs support using Command-<number> to choose that tab in the program. Some programs are explicit about it and might show the shortcut, like iTerm 2, but this will work in all major browsers I have tried as well as Sublime Text.

Also, most times when you are using a key combination to cycle through something (like tabs in a browser or the Command-Tab program switcher), using Shift will reverse the direction. So if you go past your intended target, you can often go backwards instead of cycling all the way back through.

Quick Macintosh Tips

It is sometimes hard to explain to people why I enjoy working on a Macintosh. It isn’t because they are shinier or I have fallen prey to the “reality distortion field.” Rather, there are a lot of nice tweaks, that are sometimes not so obvious, that make working with the Macintosh such a nice experience.

Quit or hide an application through the app switcher

When using the Command-Tab key combination to cycle through your open applications, you can easily quit or hide the currently selected application. Simply use the standard key combinations of Command-Q to quit the selected application or Command-H to hide it. This is a quick way to get rid of the clutter that can accumulate through a long work session. Just keep one finger pressed down on the Command key and hit Q or H instead of tab to move onto the next application. Oh, and also, to switch direction, hit Shift when Command-tabbing in case you go past the application you wanted.

Getting the folder structure of a file in an application

I love this one and use it all the time. In almost any application, when you are working on a document and you would like to see it in the Finder, Command-click on the application’s title bar.

Title bar

Then, select any of the parent’s folders to jump to it in the Finder.

Most everything is draggable

Speaking of the title bar of an application, often times you can drag the icon to wherever you need it. No need to go to the Finder, get the file and go from there. Just drag the icon from the title bar to your email to attach it or a website to upload it. Oftentimes, if I have a screenshot that I have opened in Preview, I will just drag and drop the icon onto a website like Jira or Zendesk to upload it.

Scroll background items

I really like not having to set the focus to another window to scroll it. For example, if I am working on an Excel document next to an Word window where I am taking notes, I can scroll to view more columns without leaving Word. I simply hover over Excel and use the multi-touch functions to scroll the Excel document to where you need it – vertically or horizontally.