Friday, May 11, 2012

Creating Custom PHP Page In Wordpress


situation:

suppose you want to integrate some PHP code in your wordpress page. or you want to have some PHP functionality which cant be done using wordpress alone.
 for example, you need to communicate with your database from your wordpress page. it can be simple CRUD application or anything.
you can create php page but want to have look and feel of your wordpress theme.
you can do that by dynamically linking wordpress theme's header, sidebar and footer into your custom PHP page.
Solution:
 i recently came across the situation. here is the example code,


/*
//for header
require("wp-blog-header.php");// loads wordpress environment and template 

include $_SERVER['DOCUMENT_ROOT']."/wp-content/themes/YOUR_THEME_NAME/header.php";   //loads theme header

//for sidebar
require("wp-blog-header.php");
include $_SERVER['DOCUMENT_ROOT']."/wp-content/themes/ YOUR_THEME_NAME /sidebar.php";    // loads theme sidebar


//you can place your custom PHP code here  

//for footer
require("wp-blog-header.php");
include $_SERVER['DOCUMENT_ROOT']."/wp-content/themes/ YOUR_THEME_NAME /footer.php";  // loads footer 

*/

note: add PHP starting & closing tags at appropriate places in above code.
you can create custom PHP page using above code with the LOOK and feel of your theme.
 in above code i have used PHP SERVER variable called "DOCUMENT_ROOT", which gives the document root directory under which the current script is executing, as defined in the server's configuration file. you can have more information about PHP SERVER VARIABLES from here 
visit http://codex.wordpress.org/ for more references.

 you can also use wordpress PATH constants like ABSPATH and TEMPLATEPATH as per your requirement. you can have more information about them from http://yoast.com/paths-urls-wordpress/


cya soon!


No comments:

Post a Comment