how to add a php page in wordpress as template

1. Create Your PHP Template File

  • Go to your active theme folder (usually in wp-content/themes/your-theme/).

  • Create a new PHP file, e.g., custom-template.php.


2. Add a Template Header Comment

At the top of your PHP file, you must include a special comment so WordPress recognizes it as a template:

<?php
/*
Template Name: Custom Template
Description: A custom page template for special layout
*/

?>
  • Template Name: This is the name that will appear in WordPress when you select a template.

  • Description: Optional, but useful.


3. Add the Standard WordPress Page Structure

Typically, your template will include the header, footer, and main content area:

<?php
/* Template Name: Custom Template */
get_header();
?>

<div class="custom-page-content">
<h1><?php the_title(); ?></h1>
<?php
if ( have_posts() ) :
while ( have_posts() ) : the_post();
the_content();
endwhile;
endif;
?>
</div>

<?php get_footer(); ?>

You can now add custom HTML, CSS, and PHP code inside this template.


4. Upload/Place the File in Your Theme

  • Ensure the file is inside your active theme folder.

  • Example path:
    wp-content/themes/your-active-theme/custom-template.php


5. Assign the Template to a Page

  1. Go to WordPress admin → Pages → Add New (or edit an existing page).

  2. On the right side, in the Page Attributes box, you’ll see a Template dropdown.

  3. Select your template (e.g., “Custom Template”).

  4. Publish/Update the page.


6. Optional: Add Custom CSS/JS

You can enqueue custom CSS or JS specifically for this template using functions.php:

function custom_template_assets() {
if ( is_page_template('custom-template.php') ) {
wp_enqueue_style( 'custom-template-css', get_stylesheet_directory_uri() . '/css/custom-template.css' );
}
}
add_action( 'wp_enqueue_scripts', 'custom_template_assets' );

âś… After these steps, WordPress will use your custom template for that page.

Share on Facebook Share on Twitter