Create PHP Pages that Use WordPress Templates (Page 1 of 2 )
If you are a regular user of WordPress, you might find yourself wondering if it is possible to create a PHP template file (with a .php extension) that uses or inherits WordPress template files. Before we answer that question let's look at what the word “inherits” means:
1.) The created standalone PHP template will use the theme used by your main Wordpress website. 2.) Every elements is dynamic. This means if you update your main Wordpress website by adding new posts, it will also be reflected - automatically - on your created template files. 3.) The header, sidebar, and footer element will be using your main Wordpress website theme files.
The only thing that is “unique” is, of course, the content. This type of page has a lot of applications. You know already that the default WordPress installation will not allow you to execute your own customized written PHP scripts directly inside the WordPress post. Although there are plug-ins that support this, they can be prone to incompatibility issues and also a security risk.
By utilizing standalone pages to use your own Wordpress themes, you can basically run PHP scripts in the content section of your pages, independent of the WordPress database. To have all your created PHP template files have the same look and feel as your main Wordpress website, you should follow this structure:
Your standalone PHP page contains hard-coded content (the PHP script or HTML code). This means that the content is not stored in the Wordpress database. The rest of the page elements (header, sidebar, and footer) all inherit from your Wordpress theme files and are pulled from the Wordpress database.
Getting Started: Create a Blank PHP template that depends on Wordpress page template
All newly created dynamic and standalone pages (such as those shown in the screenshot above) will be created based on your existing Wordpress page template (this is known as “page.php” and it is found in your Wordpress theme files). The following are the steps you need to take:
1.) Login to your WordPress admin panel. 2.) Go to Appearance, click Editor. 3.) Under “Templates”, you will see a lot of WordPress theme files. 4.) Click “Page Template” to view the source code of the page.php. 5.) Control + A to select all source code and then copy it to your favorite PHP editor (gPHPedit, for example in Ubuntu). Save it as “standalonetemplate.php” on your desktop computer.
6.) Depending on your WordPress theme, below is the standard page.php code (it might look slightly different with other WordPress themes):
Examining the source code above, it will start loading your WordPress headers, then your WordPress content. After loading the content, it will load the sidebar and footer at the end. So the question is: “How will you able to modify this template to work with standalone PHP pages outside your normal WordPress installation?”