This is a common question of many WordPress beginners, HOW TO ADD A PHP PAGE TO WORDPRESS? Today you’ll learn the process of adding a PHP page in WordPress using the page editor without messing up other parts of the site. This will give you the ability to add your desired PHP codes to execute on a single specific page.
You should have basic knowledge of PHP programming and how WordPress works to implement this process.
First of all, you really don’t need to interact with any kind of API calls or use a plugin. You’ll create a custom template in your theme so you’ll be able to select this while creating a page. The codes inside this template will be executed on this page only after publishing it.
Here is the method of adding PHP page:
First, you need to duplicate the post.php or the page.php file in the theme folder.
Rename this file with your desired template name. Something related to the script or anything you like, It would be yourcustomname.php. Don’t forgot to put the .php extension.
After then add following lines at the top of the file:
<?php /* Template Name: Name of Template */ ?>
Then you can modify anything in this file and can add your desired PHP codes as well.
After then go to edit a page or add a new page and select this template from the right sidebar of the editing screen. You’ll find this template using a dropdown which will appear under the attribute tab on the sidebar.
Pages with this specific template will have the codes defined in the yourcustomname.php file and will execute them as well.
You can create as many templates as you want, for different scripts or things to do. This way you also will safeguard your blog from being messed up by other authors who don’t have any knowledge about coding. So using a plugin can be risky at some point.
Here is a way to add working PHP file at the root of your WP installation:
We can simply put a PHP file at the root but we can not access it because it’ll return 404 error and browsers won’t find it on the server because of the functions and permalink structure of WordPress. So here is a simple way to add a PHP file on the root and access it without getting “404 page not found” error.
To do this you won’t need to create any template files, just add following lines to your .php file and it will be accessible over the internet.
<?php require_once(dirname(__FILE__) . '/wp-config.php'); $wp->init(); $wp->parse_request(); $wp->query_posts(); $wp->register_globals(); $wp->send_headers(); // Your WordPress Functions here... //echo site_url(); ?>
It assumes your desired file is in the same folder where the wp-config.php file situated. Otherwise, just add the path before /wp-config.php. You can access your file from any part of the site.
Here’s how to add a PHP page to WordPress in a simple way, if you find this article helpful then you can subscribe to our newsletter for being a part of the WPExplored community.