« Archives in February, 2012
28
February

How to: WordPress Themes (Part 1)

Hello everyone.

I’ve been asked a couple of times to create a how to on creating WordPress themes, so I thought I better get onto doing one. If you’re not really interested in knowing how a WordPress theme works, feel free to skip this post.

Just as a general overview of how this tutorial will be structured, first I’ll go over the general files required for a theme to even be considered a theme. I will then briefly explain what to put in each file. This tutorial assumes you’ve already got a general design and layout coded (ie. HTML and CSS code for the theme).

Files

For WordPress to recognise your theme, you need to have a minimum of two files:

  1. style.css – All the styles for your theme; and
  2. index.php – Theme entry point for (most) pages.

I say most pages for index.php, since you can create other files that WordPress will use for the entry point for certain types of posts or pages. These extra files are:

  1. header.php – Default HTML header used by your theme;
  2. footer.php – Default HTML footer used by your theme;
  3. functions.php – Special functions required by the theme;
  4. 404.php – For requests resulting in a 404;
  5. search.php – For search results;
  6. archive.php – For archive type pages, like author posts list, category posts, or a certain day, month or year;
  7. single.php – For a single post;
  8. page.php – For a single static page;
  9. front-page.php – For the page visitors see when viewing the home page of the blog; and
  10. home.php – For the list of all posts.

There are other files that a theme can include, but will most likely not be useful for a standard WordPress install. All the (mentioned and unmentioned) files make up the template hierarchy, as shown in this picture (credit to the WordPress developers). To make things simple, in this tutorial I’ll be making archive.php, search.php and home.php include a file called “templates/multiple.php”, and index.php point to a file called “templates/singular.php” within the theme directory. This is how I create my own themes, which I feel simplifies the final result.

Now that you have all these files, your theme is pretty close to being recognised by WordPress. There is just one more thing required for the system to know your theme is even a theme, and all the details about it. This comes in the form of a “header” in your style.css file, like so:

/*
Theme Name: The Dialog
Theme URI: http://blog.thereaper.net.au/wordpress-themes/the-dialog
Description: A very simple, dark 2-column WordPress theme with support for widgets, custom menu and threaded comments. Compatible with WordPress 3.0 and above.
Version: 1.0
Author: The Reaper
Author URI: http://blog.thereaper.net.au/
License: GNU General Public License v2.0
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Tags: dark, two-column, fixed-width, theme-options, threaded-comments, editor-style, right-sidebar, custom-menu
*/

This header defines the theme name, a URI/URL for directing site owners to more information about the theme, a description of the theme, its version, theme author information, and tags defining what the theme includes (used by the WordPress installation system). The example above is the one used by my current theme as of the posting of this tutorial.

Code Placement

Alright, you’ve created the files you want to use. Which files do you put what in? For two parts of the HTML, it’s quite simple – all header type stuff, including doctype, html opening tag, the head tag, and the opening of the body tag, should be placed in header.php. Most of the time, you’ll also want to include any visible header elements within this file (including your page “wrapper”, if you use one). My own header.php files usually end up something like the following (before adding template tags):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>PAGE TITLE</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
</head>
<body>
<div class="wrap">
<div class="header">
<h1 class="blog-title"><a href="#">NAME</a></h1>
<p class="blog-tag">DESCRIPTION</p>
<div class="top-nav">
NAVIGATION
</div>
</div>
<div class="main">
<div class="content">

It should now be easy to tell what goes into footer.php – all the footer stuff, which is usually just to close all elements opened in header.php. So, the example to go with the above header.php would be:

</div>
</div>
<div class="footer">
<p>Theme <a href="http://blog.thereaper.net.au/wordpress-themes/the-dialog">The Dialog</a> &copy; Copyright <a href="http://blog.thereaper.net.au/">The Reaper</a> | Powered By <a href="http://wordpress.org/">WordPress</a></p>
</div>
</div>
</body>
</html>

Pretty empty, eh? To be honest, most footer.php files are (if you create your own websites using this technique, you’d know how empty they can get sometimes). These is a way to pad this out a bit more – by including a sidebar in the theme! Doing this will add one extra line of code to this file (which I will discuss in the next section).

Now we have the header and footer parts of the HTML split up, you should be left with basically nothing left from your original design (except for post or page title, content, etc). These remaining parts of your theme go within the main content parts of your code (in the case of this tutorial, within templates/singular.php and templates/multiple.php). This point is usually where you will start adding in WordPress template tags.

Template Tags

Ok, so you’ve created all the files you need and placed all your HTML code in the correct files. Now, how do you show content that goes with the actual site? That’s where template tags come in. We’ll start with our header and footer files.

header.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes('xhtml'); ?>> 
<head> 
<meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />
<title><?php
bloginfo('name');
wp_title('|');
?></title>
<link rel="stylesheet" type="text/css" href="<?php echo get_stylesheet_directory_uri(),'/style.css'; ?>" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>
<div class="wrap">
<div class="header">
<h1 class="blog-title"><a href="<?php echo home_url(),'/'; ?>"><?php bloginfo('name'); ?></a></h1>
<p class="blog-tag"><?php bloginfo('description'); ?></p>
<div class="top-nav">
<?php
$menu_args = array('depth' => 2, 'container' => false, 'theme_location' => 'main-nav', 'show_home' => true);
wp_nav_menu($menu_args);
?>
</div>
</div>
<div class="main">
<div class="content">

Looks a bit different now, doesn’t it? From the top of the page:

  • language_attributes is used to output any required attributes to do with content language and direction, as well as any extra XML namespaces included by plugins. It can take either “xhtml” or “html” as parameters;
  • bloginfo is the primary function used for getting information about the blog itself. As you can see, it’s used a few times throughout the page for displaying different types of information;
  • wp_title is used to print out the title of the current post or page being viewed;
  • get_stylesheet_directory_uri returns the URL to the theme’s directory;
  • wp_head is the mechanism used by WordPress for providing a hook to plugins (and the system itself), so they can print out anything they require to function in the head of the page;
  • body_class prints the class attribute on the body element, including a list of classes that can be used by your theme in the styles.
  • wp_nav_menu creates and prints the navigation bar. In this example, I’m telling WordPress to only print the navigation 2 levels deep, have no container element, and to use the bar called ‘main-nav’ (which is defined in functions.php). If this navigation bar doesn’t exist, or contains no navigation bar in the WordPress admin, it will print out a standard navigation of all top level pages.

footer.php:

</div>
<?php get_sidebar(); ?>
</div>
<div class="footer">
<p>Theme <a href="http://blog.thereaper.net.au/wordpress-themes/the-dialog">The Dialog</a> &copy; Copyright <a href="http://blog.thereaper.net.au/">The Reaper</a> | Powered By <a href="http://wordpress.org/">WordPress</a></p>
</div>
</div>
<?php wp_footer(); ?>
</body>
</html>

As you can see, we’ve added the line for printing the sidebar – get_sidebar. The other template tag in this file, wp_footer, is another hook for plugins (and WordPress) to print out anything they require to function. If a user has an account, and is logged in, this will print out the admin bar.

Now for the actual content. The content itself contains a fair few more template tags. As such, it probably deserves its own post. Keep a lookout for the post containing the content part of the theme coming up in the next couple of days.
Thanks for reading, and I hope you stick around for part 2.
Robert

20
February

Where Did The Holidays Go?

Hello everyone,

As the title sort of gives away, Summer holidays are basically over for me. Orientation week (O’week) started today (not that I went to any of the events), so it’s nearly time to get back into ‘study mode’ for another year. It seemed to come up a lot quicker than it usually does, though. Not saying that I’m not excited for this year, since it is my last and seems a lot better than any previous year, but I don’t feel I got to do much of I wanted to do over the holidays. I did a fair bit of gaming and coding, and I got out of the house enough (for me), but the holidays were missing something. What was missing, I can’t really say.

All I’ll say is “oh well”. Nothing I can do about it now. I’m ready for the year anyway, and I think it’s about time I got back to University. I’ve been going a little bit insane over the past month or two, so it’s probably a good thing the holidays are over. Can start regaining my sanity! Maybe. We’ll see.

Wish me luck!
Robert

14
February

Reaper Quicky #23 – In The Domain

Hey everyone!

As you can probably see, I’ve gotten myself a domain name – thereaper.net.au. The old Hostcell subdomain is still active, so all links will continue to work (but redirect to this domain). I thought it was about time I got myself a proper domain, to take my site away from looking like it was owned by someone else.

So what do you all think? Do you think I make a good choice of domain name? Or does it not interest you? Leave your thoughts in the comments below!

Lastly, thanks to my best mate for helping me get the domain. I think he’d appreciate it if you all take a quick look at his site and work.

Robert

06
February

Projects? What Projects?

Hey everyone,

It’s been over a week since I last made a post, so I thought I should get to doing one. Unfortunately, I wasn’t too sure what to talk about. I’m not doing that much at the moment (since university doesn’t go back for another three weeks), and everything I have been doing is a little private (in my opinion). In the end, I decided I might as well let you all know the progress of my current personal projects on and off this blog.

As you can probably tell by now, I created a brand new theme called “The Dialog” (which I thought was fitting). You can read a bit more about it here, but in short I was trying to design a theme that looked a little like a window that you’d see in Windows, Mac or Linux (running a window manager like Gnome or KDE). It ended up looking a little like it does now, so I decided to just go with it and changed my design to a dialog (hence its name). I still want to implement a few smaller features, like being able to change the colour scheme, but it looks for the most part how I want it for now.

I sort of have a need for a Content Management System (CMS) for my main website, but I’ve heard bad things about Joomla and Drupal (even though they seem like fairly good systems). Plus, I need different features than what those systems provide. So I thought I’d go ahead and start building my own system. It’s currently still in initial development, and probably won’t be completed until maybe the end of the year, but it is open source and will be available soon after I’ve got it working for myself.

I’m continually updating my statistics plugin at the moment. It now features being able to show visitors information about how many others have recently visited, and what browser and operating system seem to be the most popular. I’m hoping to extend this feature further, but I’m not sure what else to show to those who are interested in that information. You can find the statistics page here.

I’ve been watching “The Unit” on DVD lately. I got the full series (four seasons worth) two or three weeks ago. I’m really enjoying watching them, since I didn’t get to see them all when they were broadcast. I’m now about half way through season three, so it shouldn’t be too long until I’ve watched them all. For those that are interested in action/war-type shows, I highly recommend seeing it.

Last thing I’ve been doing lately is playing some games. I’ve gotten myself QUBE (Quick Understanding of Block Extrusion), Osmos and the first Half Life series of games (Half Life, Half Life: Blue Shift and Half Life: Opposing Force). I’ve completed QUBE already – it’s basically like Portal, but without the portals. I recommend it for those interested in puzzles. Osmos, I’m still working on, but I’ve completed a few of the easier levels. Half Life, I have gotten a fair way through the original game. To be honest, I haven’t noticed the story to be as integral as it is in the Half Life 2 series.

That’s it for this post. Thank you for reading.
Robert