How can I add a PHP page to WordPress?
I want to create a custom page for my WordPress blog that will execute my PHP code in it, whilst remaining a part of the overall site CSS/theme/design.
The PHP code will make use of third-party APIs (so I need to include other PHP files).
How do I accomplish this?
N.B.: I do not have a specific need to interact with the WordPress API - apart from including certain other PHP libraries, I need I have no other dependencies in the PHP code I want to include in a WordPress page. So obviously any solution that didn't require learning the WordPress API would be the best one.
You don't need to interact with the API or use a plugin.
First, duplicate post.php
or page.php
in your theme folder (under /wp-content/themes/themename/
).
Rename the new file as templatename.php
(where templatename is what you want to call your new template). To add your new template to the list of available templates, enter the following at the top of the new file:
<?php
/*
Template Name: Name of Template
*/
?>
You can modify this file (using PHP) to include other files or whatever you need.
Then create a new page in your WordPress blog, and in the page editing screen you'll see a Template dropdown in the Attributes widget to the right. Select your new template and publish the page.
Your new page will use the PHP code defined in templatename.php
Source: Creating Custom Page Templates for Global Use
If you wanted to create your own .php file and interact with WordPress without 404 headers and keeping your current permalink structure there is no need for a template file for that one page.
I found that this approach works best, in your .php file:
<?php
require_once(dirname(__FILE__) . '/wp-config.php');
$wp->init();
$wp->parse_request();
$wp->query_posts();
$wp->register_globals();
$wp->send_headers();
// Your WordPress functions here...
echo site_url();
?>
Then you can simply perform any WordPress functions after this. Also, this assumes that your .php file is within the root of your WordPress site where your wp-config.php
file is located.
This, to me, is a priceless discovery as I was using require_once(dirname(__FILE__) . '/wp-blog-header.php');
for the longest time as WordPress even tells you that this is the approach that you should use to integrate WordPress functions, except, it causes 404 headers, which is weird that they would want you to use this approach. Integrating WordPress with Your Website
I know many people have answered this question, and it already has an accepted answer, but here is a nice approach for a .php file within the root of your WordPress site (or technically anywhere you want in your site), that you can browse to and load without 404 headers!
Update: There is a way to use
wp-blog-header.php
without 404 headers, but this requires that you add in the headers manually. Something like this will work in the root of your WordPress installation:
<?php
require_once(dirname(__FILE__) . '/wp-blog-header.php');
header("HTTP/1.1 200 OK");
header("Status: 200 All rosy");
// Your WordPress functions here...
echo site_url();
?>
Just to update you all on this, a little less code needed for this approach, but it's up to you on which one you use.