sammela.com header image 2

Make a WordPress Template Sandbox

September 30th, 2009 · No Comments · WordPress, article

Are you learning how do make code modifications in WordPress?

Sometimes you just want to experiment.

But it is messy and risky to make changes in the standard template pages such as index.php, page.php, archive.php, etc. So the best thing to do is make yourself a code sandbox.

A sandbox template should pull in all the infrastructure you need but allow you to write custom code and try things out. In WordPress, a custom template fulfills these requirements.  This article tells you how to build one.

First, if you are not familiar with the directory structure of themes in WordPress, read my article titled The Theme Directory Structure.

Build Your Sandbox

Armed with knowledge from my Theme directory structure article, go to the directory of your favorite  theme and copy the index.php file to a file called sandbox.php.

Add the following line to the top of the sandbox.php file.

1
2
3
4
5
<?php
/*
Template Name: SandBox
*/

?>

That’s it. You have created the basic sandbox.

If you are working locally on a PC or MAC, upload sandbox.php to your site.

Using Your Sandbox Template

To use your sandbox template, you can either create a new WordPress page or modify and old one. I suggest creating a new one for starters. Let’s make a page called “sb”.

Now, if you want to view the results of your experimental changes to sandbox.php, you can just view your new page.

Now you can start making changes in your sandbox.php page.

At the bottom of my sandbox.php page is the code that displays the sidebar, shown below:

1
<?php get_sidebar(); ?>

What happens if you add a put some html code just above the get_sidebar function?

Now our code looks like the following?

1
2
3
4
5
6
7
8
9
10
11
<table border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>This is a table</td>
    <td>above the right sidebar</td>
  </tr>
  <tr>
    <td>This is row 2</td>
    <td>of the table</td>
  </tr>
</table>
<?php get_sidebar(); ?>

When you view the “sb” page, you should see a little table up above the sidebar. Basically, you can try out any idea you want on the sandbox page and it won’t affect your main blog pages.

TIP: When you create a test page based on the sandbox template, make it “private”, so visitors to your blog won’t see it in the menu.

 

Tags:

No Comments so far ↓

There are no comments yet...Kick things off by filling out the form below.

Leave a Comment