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 3rd 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 libs I need I have no other dependencies in the PHP code I want to include in a WP page. So obviously any solution that didn't require learning the WP API would be the best one.
Create a page call it my-page.php and save it under your theme directory. Now, edit this php file and write the following line at the top of the page
Write your PHP code under the custom page definition line, you can call your other WP template, functions inside this file.
Start like
<?php require_once("header.php");?>
ORwhatever way you are integrating your header and footer to keep the layout consistent.
Since this is a my page, you NEED TO CREATE A PAGE from WordPress admin panel. Go to Admin => Pages => Add New
Add a page title, depending upon how you have coded the custom page, you might add page body (description) as well. You can fully skip the description if it’s written in the custom php page.
At right hand side, select Template. Choose My Custom Page from the dropdown. You are all set! Go to the slug (permalink) created by [wordpress][1] and see the page.
The best way to add PHP pages in WordPress to
Page Template
in thechild-theme
folder.How to create
Page Template
in WordPress.Create a file named
template-custom.php
and put it in/wp-content/theme/my-theme/
.For more details
If you're like me, sometimes you want to be able to reference WordPress functions in a page which does not exist in the CMS. This way, it remains backend specific and cannot be accidentally deleted by the client.
This is actually simple to do just by including the
wp-blog-header.php
file using a phprequire()
.Here's an example that uses a query string to generate Facebook OG data for any post.
Take the example of a link like
http://example.com/yourfilename.php?1
where1
is the ID of a post we want to generate OG data for:Now in the contents of
yourfilename.php
which, for our convenience, is located in the root WP directory:There you have it: generated sharing models for any post using the post's actual image, excerpt and title!
We could have created a special template and edited the permalink structure to do this, but since it's only needed for one page and because we don't want the client to delete it from within the CMS, this seemed like the cleaner option.
EDIT 2017: Please note that this approach is now deprecated
For WP installations from 2016+ please see https://stackoverflow.com/a/39800534/1958998 for extra parameters to include before outputting your page data to the browser.
You can name your file newpage.php - put it in your theme directory in wp-content. You can make it a page template (see http://codex.wordpress.org/Pages...) or you can include it in one of the php files in your theme, such as header.php or single.php. Even better, create a child theme and put it in there, so you leave your theme code alone and it's easier to update.
http://codex.wordpress.org/Pages#Creating_Your_Own_Page_Templates
You can also directly use the php page, like to create the php page and run with full path. like, http://localhost/path/filename.php
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 1 page, I found that this approach works best, in your .php file:
Than 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, cept, it causes 404 headers, which is weird that they would want you to use this approach. https://codex.wordpress.org/Integrating_WordPress_with_Your_WebsiteI 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!
EDIT
Just a quick update here. 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 install:Just to update you all on this, a little less code needed for this approach, but it's up to you on which 1 you use.