Home arrow Web Authoring arrow Page 2 - Create PHP Pages that Use WordPress Templates
WEB AUTHORING

Create PHP Pages that Use WordPress Templates


In this programming tutorial, you will learn how to create standalone PHP pages that use (or inherit) WordPress template files.

Author Info:
By: Codex-M
Rating: 5 stars5 stars5 stars5 stars5 stars / 7
June 10, 2011
TABLE OF CONTENTS:
  1. · Create PHP Pages that Use WordPress Templates
  2. · Using wp-load.php WordPress Theme Loader

print this article
SEARCH DEVARTICLES

Create PHP Pages that Use WordPress Templates - Using wp-load.php WordPress Theme Loader
(Page 2 of 2 )

If you are using wp-load.php, you can tell WordPress that you will be using your Wordpress theme elements in a stand-alone PHP page. These WordPress theme elements are the headers, sidebars, and footers, as shown on the previous screenshot. So how you are going to use wp-load? Follow the steps below:

Step1.) Open standalonetemplate.php using a PHP editor. You will require the standard wp-loader script in your stand-alone PHP pages. This is the first and most important thing you should do in any stand-alone PHP pages that depends on WordPress.  Once you have loaded wp-load.php, you can then start using the remaining script of standalonetemplate.php script to load first your WordPress theme headers.

Example:

<?php
//Load Wordpress theme environment in your stand-alone PHP pages
require( '/home/phpdevel/public_html/wp-load.php' );

//Load wordpress header
get_header();
?>
<div id="content" class="section">
<?php arras_above_content() ?>
<?php
if ( arras_get_option('single_meta_pos') == 'bottom' ) add_filter('arras_postfooter', 'arras_postmeta');
else add_filter('arras_postheader', 'arras_postmeta');
?>
<br /><br />
<br />
?>

You need to make sure that the absolute server path to your WordPress loader script (wp-load.php) is correct. You can read this tutorial: http://www.php-developer.org/how-to-get-the-absolute-server-path-name-of-an-executing-script-in-php/ about absolute server paths and getting the correct path in your server (this is different on every web host).

Step2.) What you are going to do next is to create space for your ACTUAL content. This content is hard-coded (contains HTML, PHP scripts, etc to render the content). For example using the standalonetemplate.php, below is the space created where you will put your content:

<?php
require( '/home/phpdevel/public_html/wp-load.php' );
get_header();
?>
<div id="content" class="section">
<?php arras_above_content() ?>
<?php
if ( arras_get_option('single_meta_pos') == 'bottom' ) add_filter('arras_postfooter', 'arras_postmeta');
else add_filter('arras_postheader', 'arras_postmeta');
?>
<br /><br />
<br />
<!--THIS IS THE START OF THE HARD CODED CONTENT-->
<?php
//Your PHP source code, HTML here
?>
<!--THIS IS THE END OF THE HARD CODED CONTENT-->

Note: The unique page content of your standalone PHP pages should be found between <!--THIS IS THE START OF THE HARD CODED CONTENT-->
 and <!--THIS IS THE  END OF THE APPLICATION SOURCE CODE-->

Step3.) Finish your standalonetemplate.php by putting Wordpress footer and sidebar:

</div><!-- #content -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>

Final standalonetemplate.php template source code

Below is the final standalonetemplate.php template source, ready to be used in creating PHP pages  that uses your WordPress theme:

<?php
require( '/home/phpdevel/public_html/wp-load.php' );
get_header();
?>
<div id="content" class="section">
<?php arras_above_content() ?>
<?php
if ( arras_get_option('single_meta_pos') == 'bottom' ) add_filter('arras_postfooter', 'arras_postmeta');
else add_filter('arras_postheader', 'arras_postmeta');
?>
<br /><br />
<br />
<!--THIS IS THE START OF THE HARD CODED CONTENT-->
<?php
?>
<!--THIS IS THE END OF THE HARD CODED CONTENT-->
</div><!-- #content -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>

Of course the above generated standalonetemplate.php is derived from Arras Wordpress Theme. Your own Wordpress theme maybe be a lot simpler than what is shown above because of the absence of this “Arras-theme specific feature”:

<?php arras_above_content() ?>
<?php
if ( arras_get_option('single_meta_pos') == 'bottom' ) add_filter('arras_postfooter', 'arras_postmeta');
else add_filter('arras_postheader', 'arras_postmeta');
?>

If you have hard time imagining what section of the original page.php source code to be removed; see the screenshot below:

The gray transparent section in the above screenshot is the one that will be removed so that you can create standalone PHP pages with Wordpress.

Known Issues: The Title Tag of your StandAlone Page

Since you now depends on Wordpress theme header, you will not be able to assign a specific title tags to your standalone pages. The solution is to revise the title script in your Wordpress header (header.php) to incorporate the standalone page title. Example below (scroll down at the last section of the PHP title tag script in header.php:

<?php
// homepage title tag
if (is_front_page()) {
echo "<title>Your homepage title</title>";
} elseif (is_single()) {
//Post title
echo "<title>";
wp_title('',true);
echo "</title>";
} elseif (is_page()) {
// Page title
echo "<title>";
wp_title('',true);
echo "</title>";
} elseif (is_archive()) {
// Archive title
echo "<title>";
wp_title('',true);
echo "</title>";
} elseif (is_404()) {
//404 page title
echo '<title>Page not found:  Your site.com</title>';
} else {
//DEFINE TITLE TAG FOR YOUR STAND-ALONE PAGES HERE
// query MySQL for title tags where $pageurlx is the permalink URL of your Wordpress post.

$pageurlx=mysql_real_escape_string($pageurlx);
$result = mysql_query("SELECT `titletag` FROM `specialpages` WHERE `url`='$pageurlx'")
or die(mysql_error());
$row = mysql_fetch_array($result)
or die("Invalid query: " . mysql_error());
$titletag = $row['titletag'];
//output title tags to browser
echo "<title>$titletag</title>";
}
?>

Then in your WordPress MySQL database, you need to create a MySQL table named as “specialpages”. This is the table structure:

http://www.php-developer.org/screenshot/specialpages.jpg

And this is the table with title tags for your standalone pages:

http://www.php-developer.org/screenshot/specialpages_titles.jpg

You can further and write your own script to pull-out the unique meta description for your standalone pages (from the “description” field).

To implement standalonetemplate.php in your WordPress website:

1.) Put your content. It can be PHP script, text , etc. Only put it between <!--THIS IS THE START OF THE HARD CODED CONTENT-->
 and <!--THIS IS THE  END OF THE APPLICATION SOURCE CODE-->

Make sure the require path to your Wordpress wp-load.php is correct.

2.) Save it in any name you like. For example, mystandalonepage.php.
3.) Upload it to your website root directory.
4.) Define the title tag, meta description tags, etc on MySQL table special pages for your standalone PHP page.
5.) Access it in the browser: http://www.thisisyourwordpresswebsite.com/mystandalone.php
7.) You should be able to see your content and the page is using your Wordpress theme.

A fully working demo of this standalone pages can be found here: http://www.php-developer.org/ajaxrecaptcha/, that page is stand-alone but inherit characteristics from the main Wordpress template. The content of that page is a PHP script. The title tags are also defined and unique. The rest of the tools found in http://www.php-developer.org/ are also stand-alone pages.


 


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
WEB AUTHORING ARTICLES

- Top WordPress Plugins for Social Media
- How to Install WordPress on WAMPP
- More of the Best Wordpress Plugins for Comme...
- Top Wordpress Plugins to Fight Spam
- More of the top Drupal modules for SEO
- Top Drupal Modules for SEO
- More Top Wordpress Plugins for SEO
- Website Optimization Tips
- Backup and Restore a Drupal Website and Data...
- 5 Best Web Font Services
- Web Font Embedding Services
- Joomla Site Administration
- Create PHP Pages that Use WordPress Templates
- Top Joomla Extensions for Social Networking
- Drupal Modules for Site Navigation

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials