Use .php file as page instead of wordpress page & template file?

Im trying to find a tutorial for this but i can’t seem to find one, and im just newbie in wordpress.

Is it possible to use certain php file as page on a wordpress site instead of the regular wordpress page using template file so i dont have to create a page for each?

I mean for example, i’d like to create a Control Panel page:
i’ll use: usercp.php anywhere in my theme folder and i’ll be able to open it on my site like: site.com/usercp/

If it’s possible, maybe you could send me some links on where i could read and understand how it works.

Thanks!

Solutions Collecting From Web of "Use .php file as page instead of wordpress page & template file?"

Yes you can do this but creating template like usercp.php .

 (1) First create usercp.php template and then this one line at first of template.

update_post_meta(get_the_ID(), ‘usercp’, 111);

 (2) Then create page and assign template.

 (2) Then go to your functions.php file and add this code

add_action('template_redirect', 'setPrivateTemplate');
function setPrivateTemplate()
{
    if (get_post_meta(get_the_ID(), 'usercp', true) === 111) {
       load_template(dirname(__FILE__) . '/your-folder-name-inside-theme/usercp.php');
       exit();
    }
}

You can do this by following the page hierarchy which you can find in the codex. https://developer.wordpress.org/themes/basics/template-hierarchy/

The imagelink below shows the template hierarchy which WordPress follows.
http://img.mywordpresstips.com/php/template-hierarchy.png

If you want to, for example, create a custom homepage you can create front-page.php. This will override the default page.php file in your WordPress theme.

In your case you can use page-$slug.php. Create a page with a slug of choice, for example ‘cpanel’, and create a php file called page-cpanel.php. This template file will only be used for the page with slug ‘cpanel’.

Take some time studying the template hierarchy it comes in handy if you want to start developing WordPress.

Hope this helps.

@Syourt’s answer provided a link that will greatly help you understand template heirarchy in wordpress.

In your question, you mentioned you do not want to create a page in wordpress, and I assume you mean creating a page in the cms backend. @Syourt’s answer will require you to create a page, at least one that has a slug that can be used by wordpress so that template hierarchy will work on that particular page.

If you strictly require a particular php page without having to create one in the cms, you can code up a page in php and upload to the root of your site, not the template folder. For example, you can create your cpanel.php file and put anything in it that works for your purpose, and upload it to the site’s root directory. You can then access this as yourdomain.com/cpanel.php. You will not be able to use the pretty permalink here out of the box.