0

I am currently building a WordPress theme that will give users the option to select different layouts for the same template. This will mainly pertain to the home and blog page. I'm thinking like one of the larger commercial themes that have multiple home page layouts. I know from experience that these themes can be quite bloated and load very slowly. What would be the best way to create multiple versions of the same template?

Should I use template_include (link)?

  • I think a lot of the bloat in these pre-packaged themes comes from the WYSIWYG feature they try to build in. Offering several layout options using the native WP template selector simply assigns the chosen PHP file to the content, without all of the Ajax, drag/drop stuff that is so buggy. – jdm2112 May 19 '14 at 1:46
  • @jdm2112 thanks for your response. What do you mean by the native wp template selector? – Zach Russell May 19 '14 at 1:55
  • 1
    In the Page Attributes metabox. Offering a "sidebar right, sidebar left, full width, etc" in that way should add the least amount of weight to your theme. – jdm2112 May 19 '14 at 2:02
  • Ah, that won't help me for a home.php file that's 100% dynamic/widgetized. – Zach Russell May 19 '14 at 2:20
  • I would simply make use of body classes. Each option 'triggers' a specific body class. Each body class can be styled accordingly – Pieter Goosen May 19 '14 at 8:13
3

Efficiency can mean a lot of different things, do you mean efficient to you as a dev (least lines of code?) or efficient to the user (loading the quickest etc).

In any case, template_include fires AFTER WordPress has determined what template it should load, but before it loads it, so WP has already done the 'work' to determine the appropriate template, then you add in your function to take it in another direction. Depending on what you're trying to achieve, you'd be better off either..

  1. Utilising the built in page templates system - http://codex.wordpress.org/Page_Templates

    Basically what @Milo mentioned here

  2. In order to offer more flexibility you can create a 'parent' template and call subsections using get_template_part() based off of conditional variables (such as theme options, current page etc) While you can do the same thing with template filters and load an entirely new template, you probably only need to change certain sections of the template, so loading these using get_template_part() allows for more reuse of code etc
1

I would use a template filter, but note that there are more specific template filters for every type of template, allowing your code to potentially be more concise:

index_template
404_template
archive_template
author_template
category_template
tag_template
taxonomy_template
date_template
home_template
front_page_template
page_template
paged_template
search_template
single_template
text_template, plain_template, text_plain_template (all mime types)
attachment_template
comments_popup

From Template Hierarchy: Filter Hierarchy

  • Thanks for your response. I'm actually talking about creating two different versions of home.php/front-page.php in this case, would this still appply? @Milo – Zach Russell May 19 '14 at 1:54
  • yes, you could give the user a choice between templates, save the selected template name in an option (or post meta), add a filter to home_template where you check the value of the option and load the alternate template. – Milo May 19 '14 at 2:06
  • Would this be a good place to set up transients as well (and have the cache flushed every time the theme settings are changed)? @milo – Zach Russell May 19 '14 at 2:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.