How To Setup A Secret PHP Test Area in WordPress
I’ve developed a few WordPress plugins and I love to hack WordPress, so there are times when I need to write and test PHP within the WordPress framework. Because my plugins are site-wide sidebar widgets, any errors I may introduce while testing have the potential to affect every page on my site.
Instead of testing my widgets live, I test the underlying PHP in a designated test area that his hidden from regular readers. That way, if PHP barks at me for missing an underscore, it’s more of a private message to me, and not a public broadcast to all of my readers.
Creating a WordPress test area is a simple addition of a page template and test page to your WordPress theme files. Any PHP that you put in the test template can be executed by refreshing a draft preview of the page assigned to it. Because the test page is never actually published, there’s no possibility of it reaching your readers and interrupting the flow of your site.
Want to set up your own WordPress PHP test area?
Using A WordPress Page Template As A PHP Test Area
- Put the following into a text file and save it as phptest.php:
<?php /* Template Name: PHP Test Template */ ?> <?php get_header(); ?> <div id="content" class="narrowcolumn"> <h2>This is a test File</h2> <?php //PHP test area ?> </div> <?php get_sidebar(); ?> <?php get_footer(); ?> - Upload the file into your current theme. These are typically located in /wp-content /themes/ [theme-name]/ under your WordPress installation directory.
- Go into your WordPress admin panel and click Editor from the Appearance menu on the left. Make sure that you can see the PHP Test Template listed on the in the template list on the right hand side. If it’s not there, you might’ve uploaded the file to the wrong theme directory.
- From the Pages menu in the left side column of your WordPress admin panel, click Add New.
- Title the page something like “PHP Test Page” and leave the post area blank.
- On the right hand side, under Attributes / Template, Select PHP Test Template from the drop down menu.
- Click Save Draft.
- Click Preview.
- Now go back to the PHP Test template in your theme by clicking Editor from the Appearance menu on the left side of the admin panel.
- From the list on the right, click PHP Test Template.
- Add your PHP code to the line of the test template that reads
//PHP test area. - When you’re done, click Update File.
- When you refresh the preview of your draft PHP test page (that you opened in step 8), any PHP that you put in the template will execute.
Once you’re satisfied that the PHP has been tested, it can be moved out of the test template and into a production template or widget with the confidence that it’s going to behave within the WordPress framework. You can clear your test template and leave the test page dormant until your next idea comes along.
Share, Bookmark, or E-Mail This Article
March 13th, 2009 at 12:43 pm
DON’T GO BACK TO WORK.
March 13th, 2009 at 2:43 pm
Listen to Pablo…I’m still working on that MaBeGroMo earmark. To make it easier, I need you to sell some default swaps or make an unreliable, poorly rated automobile so you can easily qualify for some bailout money too.
July 2nd, 2009 at 4:12 pm
Using this draft page for testing is a great idea.
I wonder if you happen to have encountered this situation below and what did you do for debugging:
Our test page has a form that set action to reload the test page itself with some hidden fields set. It seems it’s loading the wrong page even though the url displayed on the browser is the correct one. I know it’s loading the wrong page by checking the page’s html source.
I would like to know how WordPress processing the requests and pages. Since I am using a test draft page, I am sharing the same config with production thus I can not add to wp-config.php this line define(WP_DEBUG, true) to turn on debugging info.
Any suggestion?
Thanks.
ip
January 8th, 2010 at 8:22 am
[...] it interesting… Have you ever wanted to test some PHP code in your blog’s theme? Well, Jon Dyer has posted a tutorial on how to do that over at his blog, Dyers.org. It’s a cool idea for testing something on your site or blog to [...]