0

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!

3 Answers 3

1

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();
    }
}
2
  • Yes i get this and i have already done this but my question is, is it possible to to use a .php file instead? so i dont need to create a page & assign a template for all the pages i needed? Commented Feb 28, 2017 at 10:27
  • If you create a template and assign it to a page WordPress automatically loads the php file. Also the first time the post meta gets the 111 value it will not change so your if condition will be true every time no matter the template. Better to check with is_page_template('your-template')
    – Laxmana
    Commented Feb 28, 2017 at 10:32
1

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. https://developer.wordpress.org/files/2014/10/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.

1

@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.

1
  • Thank you for the explanations! all the answer here are helpful. I think i've got the answer i've been looking for. Also, Thanks everyone! Commented Mar 1, 2017 at 6:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.