Open In App

Step by Step guide to Write your own WordPress Template

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Writing your own WordPress template from scratch is fairly simple. If you are into Web Development industry, you might’ve already heard what “WordPress” is. Maybe a client has mentioned, but you’re not familiar with it. Maybe you’ve already worked with it before, but don’t know how to make a theme from scratch. Or maybe you’re a complete newbie. Whatever is the case, this post is for you.

Prerequisites:

Before we begin, you’ll need to fulfill the following set of requirements.

  • You need to have a fully fledged WordPress setup, either on localhost or live hosting. If you want to learn more about starting with WordPress, refer to this article.
  • A conceptual design, either as PSD or HTML CSS to follow throughout the development process.
  • A little introduction to PHP programming. However, it’s not a necessity for this particular post but still recommended.

Scope

Designing a WordPress theme is a long, tedious, never ending but a great programming challenge. The development process depends entirely on how you want your theme to look like. This post is just a tutorial and does not cover all the bits and pieces required for a standard WordPress theme. After going through this article, you have to heavily rely on

WordPress Codex

and

WordPress StackExchange

for your further queries.

Getting Started

With the prerequisites in mind, let’s get started. The very first thing you need to know is the fact that almost everything you do in WordPress is inside the

wp-content

directory. Everything else is the WordPress CMS itself and you don’t want to mess with that. When you’ll open

wp-content -> theme

directory, you’ll find default WordPress themes, like

twentyfifteen, twentyfourteen, twentythirteen

, etc. To start with one of your own, create a directory with whatever name you prefer. For this post, we’ll call it

wpstart

.

A WordPress theme atleast needs two files to exist – style.css and index.php

So go into

wpstart

folder and create these two files. In

style.css

, insert the following comment. This tells the WordPress dashboard about the theme details and meta information.

CSS




/*
  Theme Name: WP Start
  Author: FedUser
  Description: A bare bone WordPress theme
  Version: 0.0.1
*/


Now switch to your WordPress dashboard, and click on

Appearance > Themes

. You’ll find

WP Start

in your theme collection.

WP Start Preview

Go ahead and activate this theme, and then visit the site. And Voila! You’ve technically created a custom theme, all by yourself. Of course, it doesn’t do anything except it has a blank white screen. This is where

index.php

comes into action. Open

index.php

in text editor and write in the following code.

html




<!DOCTYPE html>
<html>
<body>
  <h1>This is a sample WordPress theme.</h1>
</body>
</html>


Visit the site once again and get your first WordPress template up and running.

Divide and Conquer

To develop a standard WordPress theme, you need to divide all your work into sections. This is not necessary, as you can do everything in

index.php

, but a good programming practice involves modularity. For this particular post, we will divide our entire work into

four

sections, viz. header, footer, sidebar and content. Corresponding to these sections, we will create four different files, namely

header.php

,

footer.php

,

sidebar.php

and

content.php

.

  • header.php: For this particular example, this file will do the following;
    • Define all the meta and link tags inside <head> for HTML.
    • Display site branding like name and description.
    • Provide navigation to different pages.

    With these points in mind, let’s code our theme header.

    html




    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8" />
      <meta name="viewport" content="width=device-width, initial-scale=1" />
     
      <title>WP Start</title>
     
            integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
            crossorigin="anonymous" /> 
    </head>
     
    <body>
     
    <nav></nav>

    
    

    Now there is one thing I want to bring your attention to. You can see how “hard-coded” our site title is. Meaning, the title is going to remain the same “WP Start”, no matter what site you apply this theme on. If the author has to change it, he has to manually edit the code to do so. In order to avoid these manual tweaking of templates, WordPress provides various function calls to deal with these situations dynamically. In this particular case, I want the title to be the name of the site/blog. For this, I’ll replace

    <title>WP Start<title>

    with

    <title> <?php echo get_bloginfo( "name" ); ?> </title>

    This is called embedding small php excerpt into HTML. (Technically, we are writing HTML in php file. So we’re embedding HTML in php code). So the header.php, with some additional code, becomes;

    html




    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8" />
      <meta name="viewport" content="width=device-width, initial-scale=1" />
     
      <title> <?php echo get_bloginfo( "name" ); ?> </title>
     
            integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
            crossorigin="anonymous" />
     
      <link rel="stylesheet" href="<?php echo get_bloginfo( 'template_directory' ); ?>/style.css" />
      <?php wp_head(); ?>
    </head>
     
    <body>
     
      <nav class="navbar navbar-default">
        <div class="container">
          <div class="navbar-header">
            <a class="navbar-brand" href="<?php echo esc_url( home_url() ); ?>">
              <h3 class="site-branding"> <?php echo get_bloginfo( "name" ); ?> </h3>
            </a>
          </div>
     
          <ul class="nav navbar-nav navbar-right">
            <li class="active"><a href="#">Home</a></li>
            <li><a href="#">Contact</a></li>
            <li><a href="#">About</a></li>
          </ul>
        </div>
      </nav>

    
    

    Additional php excerpts used in this code are;

    <?php echo get_bloginfo( 'template_directory' ); ?>

    This is to get the directory of the template, so that addition resources like CSS, JS, fonts, etc. can be located.

    <?php echo esc_url( home_url() ); ?>

    This will echo the homepage url of the site.

  • footer.php: This is the file where we will add whatever we want in the site footer, like custom footer, script tags, etc. Also, the HTML tags that started in header.php are closed in this file.

    html




      <footer class="site-footer">
        <div class="container">
          <div class="row row-30">
            <div class="col-md-4 col-xl-5">
              <div class="pr-xl-4">
                <h3>
                  <a href="<?php echo esc_url( home_url() ); ?>">
                    <?php echo get_bloginfo( "name" ); ?>
                  </a>
                </h3>
                <p><?php echo get_bloginfo( "description" ); ?></p>
                <p>© 2018 FedUser. All Rights Reserved.</p>
              </div>
            </div>
     
            <div class="col-md-4">
              <h5>Contacts</h5>
              <dl class="contact-list">
                <dt>Address:</dt>
                <dd>798 ABC Nagar, JKL, Rajasthan</dd>
              </dl>
              <dl class="contact-list">
                <dt>e-Mail:</dt>
                <dd><a href="mailto:#">someone@example.com</a></dd>
              </dl>
              <dl class="contact-list">
                <dt>Phone No.:</dt>
                <dd><a href="tel:#">+91 1234567890</a>
                </dd>
              </dl>
            </div>
     
            <div class="col-md-4 col-xl-3">
              <h5>Links</h5>
              <ul class="nav-list">
                <li><a href="#">About</a></li>
                <li><a href="#">Projects</a></li>
                <li><a href="#">Blog</a></li>
                <li><a href="#">Contacts</a></li>
                <li><a href="#">Pricing</a></li>
              </ul>
            </div>
          </div>
        </div>
      </footer>
     
        integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ="
        crossorigin="anonymous">
      </script>
     
        integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
        crossorigin="anonymous">
      </script>
     
      </body>
    </html>

    
    

    The additional php excerpt used in this file is;

    <?php echo get_bloginfo( "description" ); ?>

    This will fetch and place the site description. Another thing to mention here is that I have used “hard-coded” sub-sections like “Contacts” and “Links” in the footer.php file. Instead, you can use WordPress Widgets to automate and make them modifiable directly via Customizer. This, however, is beyond the scope of this post and we’ll discuss it any time in the future articles.

  • sidebar.php: Most of the websites have a sidebar, so do ours. Often sidebars display archive links, recent posts, social media accounts, advertisements, etc. In our case, we’ll go with archive links and social media links. Again, a WordPress widget is way better than the “hard-coded” junk! But for the sake of clarity, we’ll stick to the latter.

    html




    <div class="sidebar">
      <div class="widget">
        <h3 class="widget-title">Archives</h3>
        <div class="widget-content">
          <ul>
            <li><a href="#">October 2018</a></li>
            <li><a href="#">November 2018</a></li>
            <li><a href="#">December 2018</a></li>
          </ul>
        </div>
      </div>
     
      <div class="widget">
        <h3 class="widget-title">Social</h3>
        <div class="widget-content">
          <ul>
            <li><a href="#">Facebook</a></li>
            <li><a href="#">Twitter</a></li>
            <li><a href="#">LinkedIn</a></li>
            <li><a href="#">Github</a></li>
          </ul>
        </div>
      </div>
    </div>

    
    

  • content.php: Now that header, footer and sidebar are all set up, we’ll move towards the main content of the site. For the moment, we will stick to some dummy content inside this file.

    html




    <div class="main-content">
      <h3>Sample Title</h3>
      <p>Sample text goes here.......</p>
    </div>

    
    

Integration

Now let’s move back to the

index.php

where we will integrate all the above sections into one. As this file is an entry point for our theme, we can cleverly choose to put these sections. Here’s how I’ve done it.

html




<?php get_header(); ?>
 
<div class="container">
  <div class="row">
    <div class="col-md-9">
      <?php get_template_part( 'content', get_post_format() ); ?>
    </div>
    <div class="col-md-3">
      <?php get_sidebar(); ?>
    </div>
  </div>
</div>
 
<?php get_footer(); ?>


The

php

excerpts used here are self explanatory.

get_header(), get_sidebar()

and

get_footer()

are predefined functions used for embedding corresponding sections. For a custom section like

content.php

, embedding is done by the following code;

<?php get_template_part( 'content', get_post_format() ); ?>

style.css

: Now that we have updated our

index.php

file, let’s add some charm with

CSS

.

CSS




/*
  Theme Name: WP Start
  Author: FedUser
  Description: A bare bone WordPress theme
  Version: 0.0.1
*/
 
nav.navbar .navbar-brand .site-branding {
  margin: 0;
}
 
footer.site-footer {
  background-color: #502550;
  color: #fff;
  padding: 40px 0 20px 0;
}
 
footer.site-footer a {
  color: #fff;
}


And Voila! The first look of your custom WordPress theme is ready.

Bare Bones WordPress theme

The Loop

This is the most exciting part of the entire WordPress theme development where you have control of all the posts.

The Loop

is a functionality with which you can dynamically insert content into your theme. Our aim in this tutorial is to present all the blog posts as a user-friendly list so that the reader can choose any one of them. Let’s see how we do it. The loop itself is self-explanatory.

php




<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
 
<!-- contents of the loop -->
 
<?php endwhile; endif; ?>


IF there are any posts, WHILE there are none left, DISPLAY them. Anything inside this loop will be repeated, till the page runs out of all the posts. We can use this concept to display our list. Here’s how I have done it.

html




<div class="panel panel-default blog-post">
  <div class="panel-heading">
    <h3 class="panel-title post-title">
 
      <?php if( !is_single() ): ?>
 
        <a href="<?php echo esc_url( get_permalink() ); ?>">
          <?php the_title(); ?>
        </a>
 
      <?php else:
        the_title();
      endif; ?>
 
    </h3>
 
    <p class="post-meta">
      <?php the_date(); ?>
      by <a href="#">
           <?php the_author(); ?>
         </a>
    </p>
  </div>
 
  <div class="panel-body">
 
    <?php if( !is_single() ):
      the_excerpt();
    else:
      the_content();
    endif; ?>
 
  </div>
</div>


And changed the

index.php

to this.

html




<?php get_header(); ?>
 
<div class="container">
  <div class="row">
    <div class="col-md-9 blog-main">
      <?php if( have_posts() ):
              while( have_posts() ):
 
                the_post();
                get_template_part( 'content', get_post_format() );
 
              endwhile;
            endif;
      ?>
    </div>
    <div class="col-md-3">
      <?php get_sidebar(); ?>
    </div>
  </div>
</div>
 
<?php get_footer(); ?>


Let’s look at what just happened! The Loop in

index.php

is calling the

content.php

everytime the page has a post. Inside

content.php

, I’ve checked if the current post

is_single()

. This condition will hold true if the current page contains only a single post to loop through. When it is not single, I wanted a link to that post via its title. So I used

get_permalink()

to get the url of that particular post. However, if the page is single, there is no need of a link and therefore, I’ve used only

the_title()

function. Moving on to the meta info of the post. I’ve displayed

the_date()

on which the article was published and its

the_author()

. Finally, I’ve used the same concept of

is_single()

to either display

the_excerpt()

or

the_content()

of the post. See, it was that easy and fun. Now with a little charm of

CSS

, I got the following result.

WP Start theme

Conclusion

:

  • We’re ending this post on this very point but you need to know that there is still a lot to learn about WordPress. It was just an example drill but a standard theme would be much complex. Still, we hope that you’ve learnt something new.
  • If there was something that you didn’t understand, do mention it in the comments. If there is something that needs to be corrected, please let us know! If you’ve any feedback or suggestions for further improvements, we would highly appreciate that as well.
  • We would love to see what you’ve learnt through this post. So do share links to your first WordPress themes. Your first steps can cheer up the new-comers.

Happy Programming!



Last Updated : 01 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads