Modify your WordPress theme without impacting production

As a developer, I like to have a sandbox environment where I can make changes that only I can see, and once I’ve verified that everything is working correctly, then roll them into production.

This morning I wanted to make changes to the appearance of my WordPress theme’s main page (index.php), but I didn’t want casual visitors of my site to see my changes as I was experimenting, so I needed a way to make changes that only I could see, and noone else, until I was ready to display them.

There may be an easier way to do this - but this is how I accomplished it.


Within your WordPress theme (wordpress/wp-content/themes/yourtheme), your main page is called index.php.

Do the following:

  • make a copy of index.php and name it indexPROD.php (youre production version)
  • make a copy of index.php and name it indexDEVEL.php (your development version)
  • Now, open up index.php and replace all of its content with the following:
    • <?php
      if ($_REQUEST['TESTIT']) {
      include (”indexDEVEL.php”);
      }
      else {
      include (”indexPROD.php”);
      }
      ?>

What will now happen is that for normal viewers, they will see the content in the indexPROD.php page.  The code above is simply saying - if a variable called TESTIT was passed in with a value anywhere with the URL query, then include the indexDEVEL.php version of the page - otherwise, display the indexPROD.php version.  When you want to experiment with changes to your content, do the following:

  • Make your modifications to your indexDEVEL.php file.
  • To view them as you’re saving changes, go to www.yourwebsite.com/?TESTIT=1
    • replace the above URL with whatever URL actually takes you to your WordPress blog. And then you append ?TESTIT=1 to the end of it. This passes a variable named TESTIT to WordPress and sets it to 1. When you do this, it will trigger your index.php file to direct you to your Development code rather than your production code.

    When you’re happy with the changes you’ve made, copy indexPROD.php to indexPROD.php.deprecated_yyymmdd or something to make it easy to retrieve your old version of the page in case you need it.

  • Now, copy indexDEVEL.php over indexPROD.php to roll your changes into production.

NOTE!: If you have caching enabled for your site, you’ll need to disable it while you’re editing things, or you’ll wonder why none of your changes are visible. Don’t forget to turn caching back on when you’re finished! You may want to simply exclude your indexDEVEL.php from caching permanently, as it should only be seen by you when you need it.

I reccommend changing the TESTIT keyword in the examples with something meaningful to you, and not easy to guess. This will simply help prevent users from guessing it and seeing something that isn’t ready if they believe you might be using this method. Of course, it’s not a really big deal if they do, as they’ll see it soon anyway ;)
I haven’t done it yet, but the same principle above could apply to your category.php, etc. pages as well.

I hope this helps others out there. If you know of an easier/better way to do this, I’d love to hear it.

No comments yet.

Write a comment:

(moderated, please be patient for your comment to appear)