david lano

How to Link a Worpress Page to a Custom PHP File

Sometimes the most simplistic concepts can be hard to understand. For example, linking a WordPress Page to a custom PHP file should be relatively straight forward, however if one overlooks a few small steps along the way it can be extremely frustrating locating the source of the problem.

Why would anyone want to do this?

This allows us to use the infrastructure provided by WordPress while using a totally custom PHP file. By doing so, we can take full advantage of our custom PHP file while using the current theme inside WordPress. This is especially helpful when integrating an existing website with a blog created by WordPress.

Let’s say we want to create a WordPress Page in WordPress called “Archives” and link this new Page to a custom PHP file with code to display an archived list of our posts.

1. Create PHP File and Edit Template Name

Create a file called “archives.php” and open it up with your favorite text editor. Insert the following snippet of code in the top of your file.

<?php
/*
Template Name: Archives
*/
?>

This tells WordPress that this file is a “Template” and is named “Archives”. It is absolutely crucial that this be included in our archives.php file so WordPress can pick it up and use it accordingly.

In order for our archives.php file to actually do anything we would need to tack on a bit more code that would create a list of archived posts. There are several ways to do this but for starters here is a link that explains it a bit more in detail.

2. Upload PHP File to Server

We will need to locate the file directory containing our current WordPress Theme.

Example: blog/wp-content/themes/current-theme

This directory will contain all of the custom files associated with your WordPress theme.

Upload the “archives.php” file into your theme directory via an FTP server or whatever you use to upload files to your server.

3. Create a WordPress Page

Log into your WordPress admin interface and click on the “Manage” tab and the “Pages” tab respectively. Create a new Page and enter “Archives” in the Page Title field. Leave the main body text area blank. In the right column, find the “Page Template” box and select the “Archives” option from the drop down and click the “Save” button.

4. Take a Look

Now, go to the page you just created using your browser. The URL will be something like this: http://www.yoursite.com/blog/Archives

You have now successfully linked a WordPress Page with a custom PHP file using your current theme. Well Done!

Enjoyed this article? Don't forget to subscribe (it's free).
Click here to subscribe by RSS or click here to subscribe by email.
Really enjoyed this article?
Buy me a coffee!

Categories: Blogging Web Development

One Comment, Comment or Ping

  1. So I really can do it. I am so new with wordpress that I wasn’t even aware that to create a page I am supposed to go to the write/page section. I kept looking under the manage/page section for an add button or something. Thanks for the great post.
    Chais Meyer

Reply to “How to Link a Worpress Page to a Custom PHP File”

Search Blog


-->

November Poll

Sweet Links

Thirsty for more? Check these out:

Recommended