PHP-containing, thus dynamic, WordPress Pages! 1
The problem ….
You are using WordPress as the base for a site. Your client wants some custom pages created that contain PHP, HTML, JavaScript and the like. Of course you cannot just go into the admin panel and create a new dynamic page with your scripting in it, now can you? So what to do?
The Solution(s) ….
1. Plug it!
There are a couple (literally 3 as far as I searched) plug-ins available that will render your PHP in your page on the fly – but I found them troublesome. You have issues like not being able to use the WYSIWYG editor, contributors can mess with the code and so forth. I much prefer the custom page templates solution.
2. Custom Page Templates
Here is a link to an example of just such a custom page:
http://www.michaelbain.co.za/test/?1=enter-any-text-you-like-here-to-see-it-work
Let me walk you through the process. The basic idea here is that you are going to create a page template for each custom page with PHP in it. The scripting/logic is going to reside in this page template. The template is not going to display any page content. You are also going to create a WordPress page for each custom page with PHP in it. This page will have no content in it and it will display with it’s own custom page template. Lastly, if so required, you are going to hide this page from your nav menu and other places.
For the purposes of this article, when I am referring to a “custom page”, I am referring to a custom-page-php-template and normal WordPress page together as a pair. Don’t worry .. it will soon become clear.
a) Create a custom page template
- Go to the themes folder of the theme that you are using.
- Copy the page.php file and rename it (let’s call it test-page.php).
- Edit this renamed file. Of importance is the following:
- Leave everything “else” in the file intact – we want our custom page to look like our other pages – but just process PHP script.
- Right at the top, add the following so that your template has a name and may show up later as an option for the template to be used by your WordPress page:
<?php
/*
Template Name: testpage
*/
?> - Look for the line where the page content is being called, usually “<?php the_content(‘continue reading »’);?>.
- change that line, by commenting it out and including your own “custom page” code like this:
<?php
//php the_content(‘continue reading »’);
//custom content here
echo(‘hallo world – this is php wrapped in the theme’);
echo(‘<br><br>’);
bloginfo(‘show’);
echo(‘<br><br>’);
echo($_GET['1']);
?> - Now you can see where your dynamic content goes. Remember this is going to “be your page content”, instead of any page content created when we later create the page through the admin panel.
- As you can see, I am using a $_GET variable there, so your custom page can accept GET variables. Obviously, you can have one custom page with a POST method form that either posts to itself, or to another custom page. Dynamic pages! Yes!
- All the built-in WP tags, functions, hooks etc. are available for use also, in this template file
b) Create a WordPress page
- Create a WordPress page for you custom page. Title it, fill other metadata, even add content if you like. (The custom page template is of course not going to use this content – but you might want to put some description there of what the page is for like: “this is the custom page test page”).
- Make sure that in the create/edit panel you specify the template to be used. In our example it is “testpage” (as named in the template file).
- Publish the page: it will now be displayed with the custom page template, instead of the default one. It will be displayed without it’s own content, but with the custom content you put into the template. It is also now nicely wrapped in your theme as a page.
c) Remove the link to the custom page from your nav and sidebars (this is optional – but was certainly a requirement for me)
- In my case this involved editing my theme’s header.php and amending the wp_list_pages function call’s parameters to exclude page 131 (my test page) and amending the options of my “pages” widget through the admin panel to exclude page 131.
- Depending on where “page lists” and “page link lists” are being called in your template or widgets, you need to amend those function calls’ parameters to exclude your WordPress page that is displayd by the custom template.
And THAT as they say is THAT!
–update (2010/08/25): we can of course also use WordPress shortcodes … update to follow soon


Apologize for my bad english, I think its a gracious piece of your writing. Well I have faced alot of difficulties in this train but your article resolution definately relieve me in future. Offer You