Create A WordPress Theme From Scratch

How to Create a Wordpress Theme from Scratch

Sep 5th in Wordpress by Sam Parkinson

Following on from the recent article on "PSD to HTML", this tutorial will look at taking a HTML/CSS template and turning it into a functioning WordPress theme. There is so much you can do when creating your own theme we couldn't nearly cover it all. So, we're going to look at how themes are structured, creation of the core files and splitting up that index.html file.

PG

Author: Sam Parkinson

I live in London, the capital city of England. I enjoy it, there is always something to do. As to what I've done web related, I've created a proxy site, a blog and a club site, I've also havd the pleasure of creating several web designs for a few lucky people. I've almost learn't PHP, HTML and CSS are my area of expertise though I'm also a good guy to have when it comes to hardware.

Overview - The Structure Of A Theme

The structure of a WordPress theme is fairly simple, I like to start with the CSS file. It details everything about the theme for WordPress to use. You then have index.php - it's simply the template file you're using with the PHP template tags included. Included with that is header.php & footer.php, files that are used across the whole site. Now most themes don't use just four files and that's because WordPress allows you to use template files to layout different content. There are the defined layout files, such as archives.php and single.php. However you can also create your own, say, if you wanted to make a page that had a totally different layout to the default.

Because this is such a large topic we're splitting it into a two part series - this part making a simple but functioning theme from a standard HTML & CSS template, and the second part will look at adding more of the advanced features.

I will be working on turning the great template "Typography Paramount" by Six Shooter Media into a simple WordPress theme.

Step 1 - style.css

The style sheet is the defining file of the theme for WordPress. There are a few simple things you need to do. The first is renaming the main (if you have more that one) file to style.css, next you need to add a bit of commenting to the file.

/*   
Theme Name: Typography Paramount
Theme URI: http://www.sixshootermedia.com/
Description: An image-less template focusing on Typography.
Author: Sam Parkinson
Author URI: http://xseria.com
Version: 1.0
.
General comments/License Statement if any.
.
*/

The code above is all contained in a comment, so it won't affect the style definitions. Now I filled it out with a few details, these will be used by WordPress to display the details of the theme to admins. Make sure you add it to the top of the file with no white-spaces before it.

I've gone and renamed the style sheet file from the template, it was called 1.css. I have also made a new folder called typography-paramount which will be what I upload to the WordPress theme folder. Put the style sheet in this folder, but not under another directory otherwise it cannot be seen by WordPress.

Step 2 - The Header and Footer

In this step, we're going to create the two files: header.php and footer.php. Although they are optional both are used in most themes, they're not exactly hard to use either.

header.php

Starting with the header, create a new file in the theme folder called header.php, then open up index.html from the template and copy the following from it. This will become the header and will be displayed on every page of the site, bear that in mind when making other templates.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>-</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="css/1.css" type="text/css" media="screen,projection" />

</head>
 
<body>

<div id="wrapper">

	<div id="header">
		
	<p class="description">
				
	An imageless template focusing on Typography.
				
	</p>
				
	<h1><a href="#">Typography Paramount</a></h1>
				
	<ul id="nav">
				
	<li><a href="#">Link Here</a></li>
						
	<li><a href="#" class="active">Link Here</a></li>
						
	<li><a href="#">Link Here</a></li>
						
	<li><a href="#">Link Here</a></li>
						
	<li><a href="#">Link Here</a></li>
				
	</ul>
		
	</div>

We're now going to add the WordPress template tags to header.php, these tell WordPress where to inject the various content into the theme. Also remember to change that link to the stylesheet.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes() ?>>
<head profile="http://gmpg.org/xfn/11">
<title><?php bloginfo('name'); ?> <?php wp_title(); ?></title>
<meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />
<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen,projection" />

<?php wp_head(); ?>

</head>
 
<body>

<div id="wrapper">

	<div id="header">
		
	<p class="description"><?php bloginfo('description'); ?></p>
				
	<h1><a href="<?php echo get_option('home'); ?>/"><?php bloginfo('name'); ?></a></h1>
				
	<ul id="nav">
	<?php wp_list_pages('sort_column=menu_order&title_li='); ?> 
	</ul>
		
	</div>

There's quite a lot that's been added but it's all fairly simple when you look through it. All the tags above are well documented in the WordPress Codex. I'm just going to go through what each of the functions do.

language_attributes() - Prints the language type for the <html> tag.
bloginfo() - Used to print information about the site, the parameters are available on the Codex, 'name' returns the title of the blog.
wp_title() - Simply returns the title of the page.
wp_head() - Prints the javascript links and other header stuff.
get_option() - Retrieves a value from the options database.
wp_list_pages() - Lists links to the site's pages, the parameters sort the pages correctly and also stop WordPress from printing a default title.

footer.php

Create the file footer.php and copy everything in the template from <div id="footer"> down-wards and shove it in the new file. A dynamic footer that displays the correct year, site title and a feed link is common place in themes, so lets add one.

<div id="footer">
		
	<!-- Please leave this line intact -->
	<p>Template design by <a href="http://www.sixshootermedia.com">Six Shooter Media</a><br />
	<!-- you can delete below here -->

	© <?php the_time('Y'); ?> <?php bloginfo('name'); ?><br />
	<a href="<?php bloginfo('rss2_url'); ?>">Grab the feed</a></p>
		
</div>

</div>

< ?php wp_footer(); ?>

</body>
</html>

I've gone and changed the footer to display the copyright icon, followed by the current year (<?php the_time('Y'); ?>) and the site's name (<?php bloginfo('name'); ?>) then on a new line put a link to the rss feed (<?php bloginfo('rss2_url'); ?>).

wp_footer() is what WordPress uses to add things to the bottom of the site, more often that not used by plugins to add things like site tracking code.

Step 3 - The Core File

We're now going to create index.php

index.php

This is one of the two required files for a WordPress theme (the other being style.css), so lets get started. Create the file and put it along with the rest, then add to it the following.

<?php get_header(); ?>

<?php get_footer(); ?>

This simply tells WordPress where to include the header.php and footer.php files. Because this is a two part series we're going to include the creation of the sidebar in the next article. You can either chose to leave it's div in as static html or just leave it out which is what I will do. Add the following between the previous two tags.

<div id="content">

	<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
		
	<h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
				
	<?php the_content(); ?>
	
	<p><?php the_time('F j, Y'); ?> at <?php the_time('g:i a'); ?> | <?php the_category(', '); ?> | <?php comments_number('No comment', '1 comment', '% comments'); ?></p>
	
	<?php endwhile; else: ?>
	
	<h2>Woops...</h2>
	
	<p>Sorry, no posts we're found.</p>
	
	<?php endif; ?>
	
	<p align="center"><?php posts_nav_link(); ?></p>
		
</div>

This is what WordPress call the WordPress Loop. The first line of PHP starts this loop, endwhile is the end of it. WordPress fills out the loop for each article on the site, and if there isn't any it displays that "Woops..." content. I've also added a navigation link that will place link's to more articles, so visitors can take a look at older content without using the archive.

In the next article we will write up single.php, this would be what is displayed if a visitor clicks on the title of a post. It will include the commenting system, unlike index.php.

Review - Does it Work?

So we now have four files in out theme, assuming that you didn't forget to update you index.php file (doh!) it should work as a simple but functional theme.

Test it out in that new theme previewer that been put in the latest WordPress, after looking around there only seems to be an issue with long titles, easily fixed by adding the following to #content h2 in the style sheet.

line-height: 30px;

Continue to Part 2.

  • Subscribe to the NETTUTS RSS Feed for more daily web development tuts and articles.


Related Posts

Check out some more great tutorials and articles that you might like

Enjoy this Post?

Your vote will help us grow this site and provide even more awesomeness

Plus Members

Source Files, Bonus Tutorials and
More for $9 a month for all TUTS+
sites in one subscription.

Join Now

User Comments

( ADD YOURS )
  1. PG

    Dan September 5th

    That was the fastest tutorial I have ever seen for wordpress

    ( Reply )
  2. PG

    Dan September 5th

    This should increase flow to Theme Forest.

    ( Reply )
  3. PG

    Snorri3D September 5th

    Thanks alot for that this is just what i needed a good tut about how to connect my Xhtml/css to wordpress

    ( Reply )
  4. PG

    Paul Dukes September 5th

    This is a great tutorial. Having just done my first Wordpress themed site, I can honestly say this is all you really need to get one going.

    ( Reply )
  5. PG

    patt September 5th

    splendid!

    straight ahead, only the strings you need to know,
    seems like what i’ve been looking for all my life.

    ( Reply )
  6. PG

    Riff September 5th

    @Dan

    This should increase flow to Theme Forest.

    Nah, better not - if someone is gonna to create a theme for Wordpress after reading this tutorial, then ThemeForest will be overun by low-quality stuff - because it takes time and practice to learn the secrets behind theming WP, you can’t do it after one tutorial :P

    ( Reply )
    1. PG

      yamaniac March 20th

      your probably right RIff. But this is just a begginers tutorial, just to let ppl know wht to do for a theme to start working :)

      ( Reply )
  7. PG

    benjo September 5th

    Wow…this is what I have been afraid of? Thanks for making this manageable.!

    ( Reply )
  8. PG

    Niklas September 5th

    Nice tutorial! As always it’s easy to follow and to the point. Great for a friday fun time :)

    ( Reply )
  9. PG

    Shane September 5th

    As @Dan said, probably the quickest get yourself up and running theme tutorial I’ve ever seen.

    Perhaps many people survive by hacking available themes until they’re happy with the output, but it’s great to know how to start from scratch. I’ve found that themes out there never quite have what I’m after.

    Thanks for the tutorial.

    ( Reply )
  10. PG

    Mike T. September 5th

    this is great, i am in the process of doing wordpress for my personal site, and wanted to learn more about styling, thanks for this!

    ( Reply )
  11. PG

    Max | Design Shard September 5th

    Great stuff

    ( Reply )
  12. PG

    Sam P. September 5th

    Great to hear you like the tutorial :)

    @Riff I’m sure there is some sort of quality control for Themes Forest, and if not there should be.

    ( Reply )
  13. PG

    insic September 5th

    WOW cool tutorial. Im having a hard time creating Wordpress theme before. Now here comes the nice tutorial

    ( Reply )
  14. PG

    Josh September 5th

    Great tutorial.

    ( Reply )
  15. PG

    Ludovic September 5th

    Yes, great job, very useful.

    ( Reply )
  16. PG

    Joel September 5th

    Wow, that was fast!

    Now if you could show people how to work with sidebars, it’d be awesome.

    Still waiting for that book on WordPress theming!

    ( Reply )
  17. PG

    kyle September 5th

    check out CSS-Tricks.com. Much better 3-Part Tutorial for building a wordpress site from scratch.

    ( Reply )
  18. PG

    Patrick Lewis September 5th

    This tutorial is sweet and to the point; could there be a little explanation on custom fields, to explain how you displayed the picture with the post?

    ( Reply )
  19. PG

    Rich September 5th

    Thanks for the great tutorial. I’m just beginning to learn how to build wordpress themes, and this helps a lot! Not to be pushing but when do you expect to publish part 2?

    Thanks

    ( Reply )
  20. PG

    Chris Robinson September 5th

    nice tut, but this only scratches the surface of creating themes for WP

    ( Reply )
  21. PG

    Dainis Graveris September 5th

    Yeah, right Chris Robinson, but how you imagine to see complete wordpress tutorial here? Maybe I must give a suggestion to create WP theme creation series from scratch? That would be too good to be true, am I right? :)

    ( Reply )
  22. PG

    Chris Reynolds September 5th

    Probably one of the better, and more simple, Wordpress theme tutorials.

    Nice job!

    ( Reply )
  23. PG

    kyle September 5th

    @Dainis Graveris - there are other places on the web you can find in depth articles for wordpress. google “wordpress 3 part tutorial”

    ( Reply )
  24. PG

    Nate September 5th

    Short and sweet. Thanks for sharing.

    ( Reply )
  25. PG

    Tabris Chen September 5th

    Very nice, dugg!

    ( Reply )
  26. PG

    curtis allen September 5th

    Great Wordpress tutorial. Keep up the good work.

    ( Reply )
  27. PG

    Lamin Barrow September 5th

    @Dan.. am agreeing with you on that but it will require deep wordpress knowledge to make it function and look good.

    ( Reply )
  28. PG

    Sam P. September 5th

    @Patrick Ha, it’s just a standard picture inserted into the content of the post, just put at the top an floated left, no custom fields on this simple template :P

    @Rich I’m looking at a week, but theres a lot going on at school right now so… not too long though.

    ( Reply )
  29. PG

    Sam P. September 5th

    @Joel Sidebar’s coming in the next article. Feel free to wait impatiently :P

    ( Reply )
  30. PG

    Fabryz September 5th

    Thanks for posting! I started making some templates in fireworks, this will help me handling the coding step.

    ( Reply )
  31. PG

    Ben Griffiths September 5th

    Excellent, goes in hand with themeforest too ;)

    ( Reply )
  32. PG

    accessoire September 5th

    Thank you SO much!!! I didn’t read it yet but i guess it’s a good tut as always. I’m looking for such a tutorial for thousands of years ^^!

    ( Reply )
  33. PG

    chris September 5th

    n1 dude! thx 4 it!

    ( Reply )
  34. PG

    Brandon Kaylor September 5th

    Awsome Tutorial. I am going to try this out when I get the time.
    Very detailed and goes in depth.

    Thanks Sam. :)

    ( Reply )
  35. PG

    Taylor Satula September 5th

    Wow that was the fastest tut to theme a wp theme on earth. Good review though

    ( Reply )
  36. PG

    Jonathan September 5th

    Quick and easy. Nice introduction into wordpress themes.

    ( Reply )
  37. PG

    Damir September 5th

    Bravo.

    ( Reply )
  38. PG

    dlv September 5th

    D10, excellent
    i won’t create a wordpress theme, but like a W user i can learn more about it with this kind of “base” tutorial
    thanks a lot!
    adeux

    ( Reply )
  39. PG

    Mark Abucayon September 6th

    I love the outcome of this one. thank you for sharing this.

    ( Reply )
  40. PG

    Orama September 6th

    I actually visited this site today in hopes of finding an introductory CMS tutorial and VOILA!

    ( Reply )
  41. PG

    Chad September 6th

    You say “Also remember to change that link to the stylesheet.”

    Mine is inside theme folder of course, and called style.css

    However, nothing is getting picked up.

    ( Reply )
  42. PG

    Chad September 6th

    Question…

    where’s the “next article” for single.php tutorial? How do we add side bar column?

    ( Reply )
  43. PG

    Jad Graphics September 6th

    Great tutorial. I was looking for something like this for a very long time.

    I hope to use this to convert my site, http://www.jadgraphics.net to a wordpress site. Thanks again!

    ( Reply )
  44. PG

    Nysuatro September 7th

    You guys can read my mind. I needed this. Thanks alot

    ( Reply )
  45. PG

    zamdesign September 7th

    yeah, very simple way to making wordpress theme, i will try :) thanks

    ( Reply )
  46. PG

    Sam P. September 7th

    @Chad check you code against the downloadable source, that one works for sure.

    ( Reply )
  47. PG

    Dainis Graveris September 7th

    @kyle thanks! That’s was more than I expected! Appreciated!

    ( Reply )
  48. PG

    Jared September 7th

    I’ve been looking for a tut like this one, thanks!

    ( Reply )
  49. PG

    Manaswinee September 8th

    Great Stuff!! fastest n easiest way..

    ( Reply )
  50. PG

    Tom September 8th

    Really basic guide, but it’s well done.
    I’m waiting for more advanced hacks!

    ( Reply )
  51. PG

    Johnny September 8th

    Yes, this was a really short tut. It should’ve had another title of it. But ok to see some of the basics in Wordpress core funcs.

    ( Reply )
  52. PG

    Mike Bobiney September 8th

    Wordpress is a great platform for creating blog themes on as this article demonstrates.

    notepad++ - good stuff

    ( Reply )
  53. PG

    Hans September 9th

    Thanks for a quick intro to php and wordpress! I’ll probably use it in the future to modify existing themes!

    ( Reply )
  54. PG

    Jake Holman September 9th

    Awesome! This is exactly what I’ve been looking for myself, need to eventually get around to skinning my own blog for my portfolio - have to give this tut a good going over later!

    ( Reply )
  55. PG

    Aaron Irizarry September 9th

    Brief and useful… thanks again… css-tricks.com also has a bit more in depth one that is a great resource.
    Will there be more parts to this?

    ~ Aaron I

    ( Reply )
  56. PG

    JamieB September 10th

    Good stuff, I’m a newbie to wordpress but have played about with some of the themes. This article answers a couple of questions I had.

    ( Reply )
  57. PG

    Andris September 10th

    Thanx a lot for that. I was always looking for some good tutorial to show me how to write my own wordpresstheme, and that’s a really helpful tutorial.

    ( Reply )
  58. PG

    Tom George September 10th

    Excellent tut and well explained!

    Would be love to see a tutorial on plugin creation.

    Thanks!

    ( Reply )
  59. PG

    Josh September 12th

    Great tutorial…I can’t wait for “How to Be a Rockstar Wordpresser”. These posts should keep me busy until then though. :)

    ( Reply )
  60. PG

    deniar September 17th

    Wow thanks, it helps me so much

    ( Reply )
  61. PG

    zen September 18th

    Thanx for this nice tutorial

    ( Reply )
  62. PG

    Joe September 19th

    Could you have made this any harder to follow?

    In step 2:

    What theme folder?

    What index.html are you talking about?

    ( Reply )
  63. PG

    Desserts September 25th

    great post, thanks for this!

    ( Reply )
  64. PG

    praveen September 26th

    Thnx ..was really for a newbie like me :D
    keep up da gud wrk..

    ( Reply )
  65. PG

    liquid October 1st

    Great post, excellent for newbies like me!!
    Thanks

    ( Reply )
  66. PG

    alex October 3rd

    I’ve been putting off getting into Wordpress theming for far too long

    great tutorial, thanks!

    ( Reply )
  67. PG

    Ken October 9th

    Hi,
    You did a great job writing down here a lot of good information.
    I’m not an expert in php and your advices really help.
    Now, I want to change my default wp theme (on my new blog - the old one is ok) in just one way: I want every post from index and achieve pages to has (read more) by default. Let’s say, they will can read only the first paragraph, or first 50 characters or something like this… not entire post in index.
    I really don’t like when the people can read with just one click on my home page, all (10) latest posts.
    Can you help me with this?
    I really appreciate!
    Ken.

    ( Reply )
  68. PG

    Mr Bootle October 16th

    Really, really, really simple, clear, quick and easy tutorial. Been hunting for something like this for a long time. Well done and thanks. I now have about 5 wordpress based sites to create!

    WoO-hoO!

    ( Reply )
  69. PG

    Aizat October 30th

    What a very great tutorial for me to learn to create my own wp theme. tyvm

    ( Reply )
  70. PG

    GeemeeTheway October 30th

    Geat! I’ve been looking for something that simple so far! Good work!

    ( Reply )
  71. PG

    Wrablizliar November 17th

    i want to share my free wordpress theme here.

    Preview:
    http://www.elegantthemes.com/preview/eVid/

    Download:
    http://www.sendspace.com/file/6uv0n2

    ( Reply )
  72. PG

    belko December 18th

    fantastic tutorial, thanks

    ( Reply )
  73. This is good to get things started but its so tempting to use the source files and not look to much into the template tags… but god easy start thanks

    ( Reply )
  74. PG

    Rezkizuka February 25th

    wow, this is great. i thought i can’t start. but this is very good. so i know i can now

    ( Reply )
  75. PG

    G_Lok February 27th

    Um… sorry to say this but it’s not exactly ‘From Scratch’, but more like modifying existing template.

    I do appreciate the initiative and the tutorial is quite useful but the truth is lacks a lot in terms of ‘creating’.

    ( Reply )
    1. PG

      Sam Parkinson April 5th

      Sorry if the title is a bit misleading, it is not, I admit, totally from scratch. However the whole idea about this guide is how to build a wordpress theme, not a xhtml template. To do both would repeat a lot of content already covered on this site.

      ( Reply )
  76. PG

    MiK March 5th

    Or you can also use a wordpress theme generator! I found this one : http://www.theme-generator.net/wordpress. You don’t need to know anything about css, html or even php. At last, it generates the zip file containing all the required files ti activate the theme in your blog.

    ( Reply )
  77. PG

    Charles Clarkson April 9th

    Sam,

    language_attributes() defaults to ‘html’.

    For xhtml documents use:

    language_attributes(’xhtml’)

    Thanks for a great tutorial.

    ( Reply )
  78. PG

    Adam Winogrodzki April 20th

    wow ! its the tutorial i was looking for !

    ( Reply )
  79. PG

    AlexClarke April 22nd

    Thanks for the tutorial. Very, very helpful. Especially for beginners.

    ( Reply )
  80. PG

    sewu April 23rd

    Thanks for nice share.. It’s really useful for me..

    ( Reply )
  81. PG

    Ryan April 23rd

    This made making my first template very quick. Thanks for the help!

    ( Reply )
  82. PG

    chaithanya April 24th

    This is a very good tutorial

    ( Reply )
  83. PG

    snowflake April 26th

    great tutorial…thank u so much

    ( Reply )
  84. PG

    nuibam April 26th

    How to create 3 column wp theme?

    ( Reply )
  85. nice work ^_^

    ( Reply )
  86. PG

    Admiyn May 7th

    after reading this post everyone may says, I got in the wordpress theme development.

    ( Reply )
  87. Nice and clear to make a wp theme from the scratch and i will get this help to make my own theme developed , thanks.

    ( Reply )
  88. PG

    Anton Agestam May 11th

    Exactly what I needed! Thanks!

    ( Reply )
  89. PG

    Derek May 23rd

    That is one of the simplest introductions to WordPress I’ve ever read - it presents a simple, easy way to create the frame of a theme, leaving the fancy stuff like CSS and plugins to the designer.

    +1.

    ( Reply )
  90. PG

    arvind May 26th

    hi
    really it is interesting to learn beginner
    thanks

    Thanks

    ( Reply )
  91. PG

    Lea June 10th

    Hie! I’m a newbie and really trying to focus on the tutorial - so I have a q. Just at step 2, u said “We’re now going to add the WordPress template tags to header.php, these tell WordPress where to inject the various content into the theme. Also remember to change that link to the stylesheet.” <- my q, what link? I mean, you said to change that link to the stylesheet. Sorry I’m a bit slow but i know am getting there… (i think! LOL!) Thanks in advance!

    ( Reply )
  92. PG

    Lea June 10th

    I’m sorry - I think I get it now. The css/1.css link - sorry. My bad! ;)

    ( Reply )
  93. PG

    Nurlea June 11th

    Hi again. Just wondering. Am I the only one seeing the theme/ template’s text alignment being displayed ‘ok’ when viewed using firefox but turns out to be aligned ‘center’ when viewed with IE? So sorry to be a troublesome commentator. :)

    ( Reply )
  1. Arrow
    Gravatar

    Your Name
    June 11th