Widget Ready WordPress Template Files Displayed Conditionally

As a WordPress developer, part of my concern for my WordPress users is the ability for them to change their WordPress website content as much as possible; Without having to resort to contacting me to do so for them. This makes me think outside of the box on how I can make the WordPress theme have an intended layout, while allowing the flexibility of dynamic content. That’s where widgets come into play!

WordPress Widgets provide a structured way to allow dynamic content in a specific area of your theme without (or at least minimize the chance of) breaking the layout. Sometimes certain widgets are meant to look good in one area, but maybe not another area of the WordPress theme.

I really think widgets are the wave of the future with WordPress Theme Development. Especially with all the fancy updates to the WordPress theme customizer lately.

If you’re a WordPress Theme Developer and you’re theme’s aren’t “widget ready”, you should really change that. You can start by reading the rest of this article. 🙂

The following “How to Conditionally Display Widget Ready WordPress Template Files Tutorial” is very basic and bare-bones, but enough to give you the idea to run with.

Disclaimer: This “how to / tutorial” doesn’t cover WordPress Theme Options. This assumes you already have your WordPress theme settings managed in some way.

In this “how to / tutorial” I’ll demonstrate how you can give your WordPress users more control over the header section of their WordPress website. Although, don’t stop your creativity there. You could apply this basic idea in many other conventions.

Step 1 – Tell WordPress About the “Widget Areas” Supported by Your WordPress Theme.

The following code should be pasted in your currently active WordPress theme’s functions.php file:

Step 2 – Create the WordPress Template Files for Each Type of Header.

In this example, you’ll want to create 4 new template files in the root directory of your currently active WordPress theme:

  1. header-column-1.php
  2. header-column-2.php
  3. header-column-3.php
  4. header-column-4.php

Step 3 – Make Each New WordPress Header Template File “Widget Ready”.

In this example, I’ll demonstrate a 4 column header template (header-column-4.php):

Step 4 – Edit header.php in Your Currently Active WordPress Theme to Activate Dynamic Header Output.

There you have it! You can now “dynamically” display content in your header template file using widgets. Optionally, you could display static content which could serve as your “default content” or “fallback content” in-case there’s no widgets assigned to that particular widget area.

This small and simple change can really add a lot of flexibility to your WordPress Themes.

Credits

Original question asked by @AlonsoF1 on WordPress Stack Exchange: Show/hide widgets in WordPress admin based on current Advanced Custom Fields option.
Answer to Question: Show/hide widgets in WordPress admin based on current Advanced Custom Fields option by @Michael Ecklund on WordPress Stack Exchange.

Related Questions

This article assists in answering all of the following related questions:

  1. [WordPress Development] Show/hide widgets in WordPress admin based on current Advanced Custom Fields option asked by: @AlonsoF1
  2. [WordPress Development] What’s the best way to handle css for multiple header layouts/multiple custom header.php files? asked by: @AlonsoF1
  3. [WordPress Development] How Can I Register Menus and Widgets Conditionally Based on Theme Options/Settings? asked by: @Ryan Dorn
  4. [WordPress Development] Custom Menus, Widgets & Conditional Statements asked by: @Justice Is Cheap

Leave a Reply