Introduction to a WordPress application's frontend

Exclusive offer: get 50% off this eBook here
WordPress Web Application Development

WordPress Web Application Development — Save 50%

Develop powerful web applications quickly using cutting-edge WordPress web development techniques with this book and ebook

$26.99    $13.50
by | November 2013 | e-Commerce Open Source Web Development

This article written by Rakhitha Nimesh Ratnayake, the author of the book WordPress Web Application Development, focuses on the extendable capabilities of themes while exploring the roles of the theme files for web applications.

WordPress powers its frontend with a concept called themes, which consist of a set of predefined template files to match the structure of the default website layouts. In contrast to web applications, a WordPress theme works in a unique way. Preparing a theme for web applications can be one of the more complicated tasks that is not discussed widely in the WordPress development community. Usually, web applications are associated with unique templates, which are entirely different from the default page-based nature of websites.

(For more resources related to this topic, see here.)

Basic file structure of a WordPress theme

As WordPress developers, you should have a fairly good idea about the default file structure of WordPress themes. Let's have a brief introduction of the default files before identifying their usage in web applications. Think about a typical web application layout where we have a common header, footer, and content area. In WordPress, the content area is mainly populated by pages or posts. The design and the content for pages are provided through the page.php template, while the content for posts is provided through one of the following templates:

  • index.php
  • archive.php
  • category.php
  • single.php

Basically, most of these post-related file types are developed to cater to the typical functionality in blogging systems, and hence can be omitted in the context of web applications. Since custom posts are widely used in application development, we need more focus on templates such as single-{post_type} and archive-{post_type} than category.php, archive.php, and tag.php.

Even though default themes contain a number of files for providing default features, only the style.css and index.php files are enough to implement a WordPress theme. Complex web application themes are possible with the standalone index.php file.

In normal circumstances, WordPress sites have a blog built on posts, and all the remaining content of the site is provided through pages. When referring to pages, the first thing that comes to our mind is the static content. But WordPress is a fully functional CMS, and hence the page content can be highly dynamic. Therefore, we can provide complex application screens by using various techniques on pages. Let's continue our exploration by understanding the theme file execution hierarchy.

Understanding template execution hierarchy

WordPress has quite an extensive template execution hierarchy compared to general web application frameworks. However, most of these templates will be of minor importance in the context of web applications. Here, we are going to illustrate the important template files in the context of web applications. The complete template execution hierarchy can be found at:

http://codex.wordpress.org/images/1/18/Template_Hierarchy.png

An example of the template execution hierarchy is as shown in the following diagram:

Once the Initial Request is made, WordPress looks for one of the main starting templates as illustrated in the preceding screenshot. It's obvious that most of the starting templates such as front page, comments popup, and index pages are specifically designed for content management systems. In the context of web applications, we need to put more focus into both singular and archive pages, as most of the functionality depends on top of those templates. Let's identify the functionality of the main template files in the context of web applications:

  • Archive pages: These are used to provide summarized listings of data as a grid.
  • Single posts: These are used to provide detailed information about existing data in the system.
  • Singular pages: These are used for any type of dynamic content associated with the application. Generally, we can use pages for form submissions, dynamic data display, and custom layouts.

Let's dig deeper into the template execution hierarchy on the Singular Page path as illustrated in the following diagram:

Singular Page is divided into two paths that contain posts or pages. Static Page is defined as Custom or Default page templates. In general, we use Default page templates for loading website pages. WordPress looks for a page with the slug or ID before executing the default page.php file. In most scenarios, web application layouts will take the other route of Custom page templates where we create a unique template file inside the theme for each of the layouts and define it as a page template using code comments.

We can create a new custom page template by creating a new PHP file inside the theme folder and using the Template Name definition in code comments illustrated as follows:

<?php
/*
* Template Name: My Custom Template
*/
?>

To the right of the preceding diagram, we have Single Post Page, which is divided into three paths called Blog Post, Custom Post, and Attachment Post. Both Attachment Posts and Blog Posts are designed for blogs and hence will not be used frequently in web applications. However, the Custom Post template will have a major impact on application layouts. As with Static Page, Custom Post looks for specific post type templates before looking for a default single.php file.

The execution hierarchy of an Archive Page is similar in nature to posts, as it looks for post-specific archive pages before reverting to the default archive.php file.

Now we have had a brief introduction to the template loading process used by WordPress. In the next section, we are going to look at the template loading process of a typical web development framework to identify the differences.

Template execution process of web application frameworks

Most stable web application frameworks use a flat and straightforward template execution process compared to the extensive process used by WordPress. These frameworks don't come with built-in templates, and hence each and every template will be generated from scratch.

Consider the following diagram of a typical template execution process:

In this process, Initial Request always comes to the index.php file, which is similar to the process used by WordPress or any other framework. It then looks for custom routes defined within the framework. It's possible to use custom routes within a WordPress context, even though it's not used generally for websites or blogs. Finally, Initial Request looks for the direct template file located in the templates section of the framework. As you can see, the process of a normal framework has very limited depth and specialized templates.

Keep in mind that index.php referred to in the preceding section is the file used as the main starting point of the application, not the template file. In WordPress, we have a specific template file named index.php located inside the themes folder as well.

Managing templates in a typical application framework is a relatively easy task when compared to the extensive template hierarchy used by WordPress. In web applications, it's ideal to keep the template hierarchy as flat as possible with specific templates targeted towards each and every screen.

In general, WordPress developers tend to add custom functionalities and features by using specific templates within the hierarchy. Having multiple templates for a single screen and identifying the order of execution can be a difficult task in large-scale applications, and hence should be avoided in every possible instance.

Web application layout creation techniques

As we move into developing web applications, the logic and screens will become complex, resulting in the need of custom templates beyond the conventional ones. There is a wide range of techniques for putting such functionality into the WordPress code. Each of these techniques have their own pros and cons. Choosing the appropriate technique is vital in avoiding potential bottlenecks in large-scale applications. Here is a list of techniques for creating dynamic content within WordPress applications:

  • Static pages with shortcodes
  • Page templates
  • Custom templates with custom routing

Summary

In this article we learned about basic file structure of the WordPress theme, the template execution hierarchy, and template execution process. We also learned the different techniques of Web application layout creation.

Resources for Article:


Further resources on this subject:


WordPress Web Application Development Develop powerful web applications quickly using cutting-edge WordPress web development techniques with this book and ebook
Published: November 2013
eBook Price: $26.99
Book Price: $44.99
See more
Select your format and quantity:

About the Author :


Books From Packt


WordPress and Flash 10x Cookbook
WordPress and Flash 10x Cookbook

WordPress 3 Plugin Development Essentials
WordPress 3 Plugin Development Essentials

WordPress 3 Complete
WordPress 3 Complete

WordPress Multisite Administration
WordPress Multisite Administration

WordPress 3 Cookbook
WordPress 3 Cookbook

WordPress for Education
WordPress for Education

WordPress 3 For Business Bloggers
WordPress 3 For Business Bloggers

WordPress 2.7 Complete
WordPress 2.7 Complete


No votes yet

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
5
w
6
8
S
E
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software