WordPress and it’s plugins do a great job for 99% of the things you’d want in a web page, but occasionally you need something special.
You might need to run custom PHP, respond to AJAX queries, generate downloadable content, or any number of other things that wordpress just doesn’t do out of the box.
Here are three easy steps to add a custom php page to a wordpress installation.
1. Find your theme directory
The first step will be to find where the new php file will go. If you don’t know which theme you’re using, you can check in your wp-admin dashboard. The “at a glance” box should say something like “WordPress 4.1 running Twenty Fifteen theme.”
The name of the theme should be similar to a directory in the
wp-content/themes/ subdirectory of your wordpress installation.
2. Make your custom php page
This is where the actual effort comes in to play. Create the PHP code that does the job you need done. The file name can by anything you want, wordpress will scan all the files. You will need to give your file a “Template Name” with a special comment inside the php tag at the beginning of the page.
/** Template Name: My Special Page */
“My Special Page” can by any name you want. It needs to be unique and I recommend you make it descriptive.
If you want the page to render in a normal looking fashion, you should make sure that the first actual PHP function called is “
Otherwise, you can omit the get_header() call and emit plain text or JSON for an AJAX request. You could even set the Content-Type header and send an image, PDF, CSV, or anything else you want to serve from the URL.
If you’re making your first custom page, it might be best to do a simple “Hello World” to test. The full document might look like this:
<?php /** Template Name: Hi, World! */ get_header(); ?> Hi, world!
3. Link the URL to your custom php
The next step is to decide on a slug for the permalink url. You’ll be adding this using the Title field and the permalink slug will be generated from the title you give it.
Back in the wp-admin page, select “Pages” from the side menu and “Add New”. Add the title and make sure the permalink is acceptable. Once it is, click on the template selector in the “Page Attributes” on the right side of the page and pick the template you created in the previous step. The body of the page will be left empty.
With the slug and template in place, just hit “Publish” and try it out!