How To Create PHP Page in WordPress

By -

If you’d like to learn how to create a custom PHP page in WordPress or implement custom PHP code in your WordPress pages, here’s a simple guide to help you get started:

1. Create the Custom PHP File

First, navigate to your theme folder (wp-content/themes/your-theme/) and create a new file called custom-page.php.

<?php
    /* Template Name: Custom Page */
    echo 'This is my custom PHP page!';
?>
how to create php page in wordpress

Notice the /* Template Name: Custom Page */ part in your custom-page.php. If you don’t write this line it won’t show up in your template list in the next step.

2. Create a New Page in WordPress

Once the PHP file is created, go to the WordPress admin panel and create a new page. On the right-hand side of the page editor, you will find a “Template” option under the “Page Attributes” section. From the dropdown, select the template that matches the name of the file you created, i.e., Custom Page (the name of your PHP file).

how to create php page in wordpress

3. Publish the Page

After selecting the template, click “Publish.” Your custom page will now be live on your WordPress site. (Don’t forget to set a title for your WordPress page, sometimes the Publish button is greyed out if you don’t have a title for the page)

4. Optional: Separate the Page Template and Content

If you want to further organize your code, you can create two separate files: custom-page.php (the page template) and custom-page-content.php (where the page content will go). Once the page is created, you can simply update the content by editing custom-page-content.php without touching the template file.

Here’s how you would structure the custom-page.php file:

<?php
/* Template Name: Custom Page */
echo 'This is my custom PHP page!';
?>
<?php get_header(); ?>

<div id="content" role="main">
<div class="post-box">
<div class="block loop-single">
<br/>
<?php include 'custom-page-content.php'; ?>
</div>
</div>
</div>

<?php get_footer(); ?>

In this structure:

  • custom-page.php serves as the template for the page, including the header and footer of your site.
  • custom-page-content.php will contain the actual content of your page, which you can modify as needed.

5. Customize the Page Content

With the custom-page-content.php file, you have full control over the page content. You can add HTML, CSS, and PHP code to customize it to your needs. For example here is a custom PHP page example that displays user’s IP address

<?php echo 'Your IP address is: ' . $_SERVER['REMOTE_ADDR'] ; ?>

This allows you to embed any additional functionalities or elements that you want to appear within your custom page.

Fully Customizable Page

With this setup, you now have a fully customizable WordPress page where you can edit HTML, CSS, and PHP code directly. This is a great way to integrate custom features or layouts into your WordPress site.

There are many ways on how to create php page in WordPress but this one is the easiest I think. So, I highly recommend this approach. Other approaches I have tried sucked, to be honest.

how to create php page in wordpress

If you have any questions, feel free to ask me in the comment section! I love answering questions 🙂