WordPress being a powerful CMS is used extensively for creating websites ,blog ,ecommerce store or any other type of application.Though wordpress provides many action hooks and filters for modifying core functionality there are situations where we need to add some custom code to a page.The custom PHP page can be used to create a unique layout or show specific content for any particular page.So in this guide we will learn how to add a custom php page with some content in wordpress without breaking the current theme.
Prerequisites
1.Wordpress Installed locally : You should have a basic local installation of wordpress in your PC
2.Knowledge of PHP and wordpress :You should have basic working knowledge of WordPress and PHP
Let’s dive into learning to create a custom PHP page in wordpress step-by-step.
Step 1: Understanding WordPress Page Templates
WordPress follows templating system .So you can create custom page templates.
You can add custom functionalities and layouts for pages as per custom requirements.
You can also add PHP code in the page template to control the display of any page.
Step 2: Create a Custom Page Template
I have basic installation of wordpress located inside xampp folder in C:\xampp\htdocs\phpgurublog.
Start xampp and open the url http://localhost/phpgurublog/ in your browser.
You can see the default wordpress theme page for home page.
Now login to admin and check which child theme is activated.In my dashboard I am using astra theme.It is a fully customizable theme which you can download from here https://wordpress.org/themes/astra/ .
Create a child theme and name it as atra-child theme Activate this child theme in dashboard.
Now navigate to astra-child folder under wp-content/themes folder.Create a folder custom-templates inside astra-child folder .

To create a custom page template create a page named as custom-page.php inside custom-templates folder.See the screenshot for this:

Now we need to add template header to this template file so that it can be shown under templates in admin section .Now when we edit any specific page this template will be shown under Template.
We need to add header template to this file.
Find the code below for adding template header:
/**
* Template Name: Custom Page
*/
This commented code specifies the wordpress that it is not a regular page .It specifies that this is a template page and can be selected from page attributes section in admin area.
Step 3: Adding PHP Code to Custom Page Template
Now we need to add some custom PHP code to this page.
This code will be shown in the Page.
Find the code below:
get_header(); // This includes the header
// Your custom PHP code goes here
echo '<h1>Welcome to My Custom Page</h1>';
echo '<p>This is a custom page built with PHP!</p>';
get_footer(); // This includes the footer
get_header() function of wordpress includes header.php which is the header template of theme.
Then we have used some custom html code with php echo statement to display content for page in frontend.
get_footer() function includes the footer.php which is the footer of the theme.
Step 4: Assigning the Custom Template to a Page
Now our template page is ready.We need to assign it to a specific page.
Let’s create a page by navigating to Pages/Add New Page section in admin area.
Create a Home Page under Pages.
Edit the page in admin under Pages section.Select the custom page template under Template section and save.
See the screenshot below:

Now navigate to frontend area of the website .You can see your custom template for your wordpress site.See the screenshot below:

Step 5: Best Practices to Avoid Breaking of Theme
To avoid breaking of theme we should consider below tips:
1.Always use a child theme for modifications :child themes helps to add your custom modifications to any website.
2.Always backup your site :Always take a backup of your files before modifications .
You can always roll back in case of any issue.
3.Always test in the local environment : Always test changes in a test environment .
This will give you an opportunity to debug it properly .
Congratulations! You have created a custom template for your page.By this way you can add functionality and tweaks to your code.
By following this guide step-bystep you can easily modify your code without breaking your theme.
But always backup your code before making changes and always use child themes for making any change.
Happy coding!