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";

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.

Hiding the categories of a post when it is “uncategorized”

If you’ve ever had some posts on your WordPress blog, you will have noticed that the “Uncategorized” category is displayed with a link to an archive of uncategorized posts.


The easiest way to solve this, of course, is to categorize every blog post. But, if you don’t want to for some reason, you can use some PHP in your theme to hide the category of these posts. The example below is a customization I did for a theme in Bones but it should be easy to follow for other themes as well. The original line for the display of the categories in Bones is:

&lt;?php printf( __( 'filed under', 'bonestheme' ).': %1$s', get_the_category_list(', ') ); ?&gt;

So, it is just using the WordPress function, get_the_category_list, to list a comma-separated list of categories for the post. How do we hide this if the post is uncategorized? Well, one way to do it is to get a list of categories for the post. If the number of categories is one, and that category is “Uncategorized” then we just won’t use the get_the_category_list display. Here is the code:

$show_categories = true;
$categories = wp_get_post_categories( $post-&gt;ID );
// We don't want to show the categories if there is a single category and it is "uncategorized"
if ( count( $categories ) == 1 &amp;&amp; in_array( 1, $categories ) ) :
 $show_categories = false;
if ( has_category( null, $post-&gt;ID ) &amp;&amp; $show_categories ) :
 echo ' &lt;span class="amp"&gt;&amp;&lt;/span&gt; ' . __('filed under ', 'bonestheme') . get_the_category_list(', ');

We start off with the assumption that we’ll display the categories so we set a flag as true to begin with. Then, we retrieve the categories for a post. wp_get_post_categories will return the post’s categories as an array. Terms, or categories, for WordPress are stored in the wp_terms database table. And “Uncategorized” is actually a stored term with the term_id of 1:

Uncategorized term in database

With this knowledge, we check the number of categories of our post (retrieved with wp_get_post_categories) with a count call. If the result is one and the category array contains the “Uncategorized” category (term_id of 1) then we know we don’t have any categories we want to display. So, we’ll set our flag to false.

Finally, we make sure that the post does have categories and that our flag to display categories is set to true. WordPress’s has_category function, if given no category to check against, will just tell us if the post has any categories. If the post has categories and the flag is set to true, then we will use the category display line in our theme and display the post’s categories.

Hiding the WordPress admin bar on certain pages

There may be times when you would like to hide the WordPress admin bar on certain pages. Maybe the admin bar interferes with the design, for example. This can be especially true for things like pages opening up in a lightbox. Fortunately, it is not too difficult to hide the admin bar on certain pages with a filter in WordPress.

There is actually a filter in WordPress named, logically enough, show_admin_bar. This filter will show the admin bar if true is returned but not show the admin bar if the returned value is false. You pass the $bool value in which is WordPress’s own determination if the admin bar should be shown or not. So, in your theme’s functions.php file, you can add the following code:

function my_theme_hide_admin_bar($bool) {
if ( is_page_template( 'page-pop-up.php' ) ) :
return false;
else :
return $bool;
add_filter('show_admin_bar', 'my_theme_hide_admin_bar');

In the code above, we are simply checking to see if the page we’re on is using the template file, page-pop-up.php. This is a theme file that I used to display pages opening up in a lightbox. If the page is using that template, we return false to suppress the showing of the admin bar. If not, we return the usual $bool value set in WordPress and it will act as normal. Of course, the template conditional is just one example. You could also use other WordPress conditionals to turn the admin bar off for a myriad of other circumstances.

Conditional Required Contact Form 7 Fields

Sometimes you may want to have a required field in Contact Form 7 only when another form field has a certain value. For example, if someone has an option to choose their preferred method of contact between email and telephone, you’d want to make the email address field or the phone call field required accordingly. This isn’t too difficult to accomplish in Contact Form 7. You can look at how Contact Form 7 validates text and email fields by viewing its code in:


Around line 91, you will see the validation filters defined by Contact Form 7:

add_filter( 'wpcf7_validate_text', 'wpcf7_text_validation_filter', 10, 2 );
add_filter( 'wpcf7_validate_text*', 'wpcf7_text_validation_filter', 10, 2 );
add_filter( 'wpcf7_validate_email', 'wpcf7_text_validation_filter', 10, 2 );
add_filter( 'wpcf7_validate_email*', 'wpcf7_text_validation_filter', 10, 2 );

Then, you can look at the function that handles the validation, wpcf7_text_validation_filter. The function accepts two parameters – $result and $tag. The $tag is the field being validated and the $result is, well, the result of the validation. In other words, did something go wrong?

The user-entered text of the field being checked is assigned to $value. Then, the function checks the current tag type. Is it text? Email? And it will process it accordingly. If a value is empty but the field is required, for example, the result will be invalid:

$result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) );

In this case the Contact Form 7 error message for required fields is given as an error (wpcf7_get_message( ‘invalid_required’ )). If $result-invalidate() is called, that field will fail validation and you will get a form error.

We’re not, of course, going to modify the plug-in’s files directly. We want to make our plug-in modifications safe from future plug-in updates so we will tie into the same filters that the plug-in uses. I can put this code in my theme’s functions.php file or put it into a my own plug-in. In my example, I am setting two conditional required fields – email and phone number. So, I will need to set my own custom validations for both text and email fields:

add_filter( 'wpcf7_validate_text', 'my_site_conditional_required', 10, 2 );
add_filter( 'wpcf7_validate_text*', 'my_site_conditional_required', 10, 2 );
add_filter( 'wpcf7_validate_email', 'my_site_conditional_required', 10, 2 );
add_filter( 'wpcf7_validate_email*', 'my_site_conditional_required', 10, 2 );

For both the text (phone) and email fields, I am using the function, my_site_conditional_required, for my own custom validations. Here is my complete function:

function my_site_conditional_required($result, $tag) {
    $tag = new WPCF7_Shortcode( $tag );

    $name = $tag->name;

    $value = isset( $_POST[$name] )
        ? trim( wp_unslash( strtr( (string) $_POST[$name], "\n", " " ) ) )
        : '';

    if ( "phone" == $name && 'Phone' == $_POST['contact-method'] ) :
        if ( '' == $value  ) :
            $result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) );

    if ( "email" == $name && 'Email' == $_POST['contact-method'] ) :
        if ( '' == $value ) :
            $result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) );
        elseif ( ! wpcf7_is_email( $value ) ) :
            $result->invalidate( $tag, wpcf7_get_message( 'invalid_email' ) );

    return $result;

Here is how it works. I am doing the same thing that Contact Form 7 does with setting the $value. Then, instead of checking the tag type, I want to check the tag name. I specifically want to look for the fields named “phone” or “email.” I also have a dropdown named “contact-form” so I can check the $_POST value of contact-form and use that to see if the field being checked should be required. If $_POST[‘contact-form’] is equal to “Phone,” I want the phone field to be required:

if ( "phone" == $name && 'Phone' == $_POST['contact-method'] ) :
        if ( '' == $value  ) :
            $result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) );

I have an extra check for the email. I don’t just want to require the email address, I want to make sure it is a valid email address as well. Contact Form 7 provides a function to check an email, wpcf7_is_email:

elseif ( ! wpcf7_is_email( $value ) ) :

As you can see, my custom function works very similarly to the code in the plug-in. Finally, you probably want to show the user the field that is required based on their downdown selection. I added empty spans after the field labels that I can use to add and remove informational asterisks from:

<label class="email-label">Email<span class="asterisk"></span></label>

Then, I can use jQuery to add and remove the asterisks:

<script type="text/javascript">
 jQuery(function() {
 jQuery("select[name='contact-method']").on('change', function() {
 function add_required_asterisks() {
 if ( jQuery("select[name='contact-method']").val() == "Phone" ) {
 jQuery(".phone-label .asterisk").html("*");
 jQuery(".email-label .asterisk").html("");
 else {
 jQuery(".phone-label .asterisk").html("");
 jQuery(".email-label .asterisk").html("*");

I created a JavaScript function that will see what my dropdown menu (named “contact-method”) is set to and then, based on that, turn the asterisks on or off. Then, I call that function on page load and, whenever the dropdown changes:

jQuery("select[name='contact-method']").on('change', function() {

Quick WordPress Tip – Upload to a Post Without Switching Tabs

Here is a quick tip for WordPress users out there. When you are adding, say, a Featured Image to a post, you are brought to the Media Library window that includes an “Upload Files” tab. This might lead you to believe that you need to switch to that tab in order to upload a new image file. Actually, though, you can save yourself some clicks by simply dragging a photo onto the Media Library. No need to switch tabs.

Drop to Upload

Saving changes made in Chrome’s Web Inspector

It is wonderful to be able to use Chrome’s Web Inspector development tools to tweak the CSS of a web page to get elements just right. However, what is the best way to apply these changes to the actual stylesheet? One could, of course, just move these changes by recreating the tweaks to the CSS document manually. There is a way, though, to actually save out the modified CSS file right from the Web Inspector. To do this, simply click on the CSS link at the right-hand side of the Styles palette:

CSS link


Clicking on the link will open up the modified CSS file in the Web Inspector’s Sources tab:



Right-clicking on the opened CSS file will allow you to view the changes made to the CSS file via the Elements tool (Local Modifications) as well as save out the changed CSS file which you can use to replace the old CSS.

Right click