2

I have created a custom page that I have called mypage.php.

It is in my template folder with al the other pages (index.php, page.php, ...)

I want this page to be opened when i click on the below code.

<a href="<?php site_url(); ?>/mypage.php">Go to page</a>

When I click on the link the url in my browser look like: http://localhost:8888/mypage.php which I guess is correct.

BUT it uses index.php as template ignoring the code I have into mypage.php

So all i am getting at the moment is an empty page with only my header and footer.

Is it possible to use pages in this way with Wordpress? I had a look online and on this site but I haven't been able to find a solution for this problem.

3
  • What is the purpose of that page? I mean why just don't create a page within WordPress?
    – Laxmana
    Feb 21 '17 at 16:45
  • @Laxmana Because i have some custom code in there to create a PDF from html. I am passing an ID via the url, then I use the ID to find the page i want to convert and I use some code to convert the html into PDF. Please let me know if i havent been too clear! Feb 21 '17 at 16:48
  • 1
    You can do it through WordPress custom template pages or WordPress endpoints
    – Laxmana
    Feb 21 '17 at 16:52
5

WordPress doesn't load templates that way.

First: give the template a name:

To load a page template, first you'll have to make sure your template has a name. To do that, you'll have to have the following CODE in your template file (here mypage.php):

<?php
/**
 * Template Name: My Page
 */

Once you have the above PHP comment, WordPress will recognise it as a template file.

Then, assign the template to a page:

Now you'll have to create a WordPress page (from wp-admin).

While you create the page, WordPress will give you the option to choose a custom template (if there is one). After you choose the template and Publish, that page will use your mypage.php file as a template.

Add Page Template

1
  • Thank you. this is perfect! I still need to create the page from wp-admin in order to get a custom template to work! This worked perfectly! Feb 21 '17 at 16:56
1

@Martina Sartor, you can use wordpress custom template process. Add a the below comment to the file you wish to make your custom page. Then go to the admin area and create a page, and select your custom template for that page.

Please review

<?php 
/*
Template Name: Full-width layout
Template Post Type: post, page, event
*/
// Page code here...

mention your template name here

You can also review wordpress documentation for same.

https://developer.wordpress.org/themes/template-files-section/page-template-files/

4
  • Thank you, I did try this and I have tried it again just now but it is not working. The link keeps going directing me to index.php Feb 21 '17 at 16:51
  • I have provided same solution to you but it did not work for you .when some else provided , it worked for you.. great..!! Feb 21 '17 at 17:11
  • 1
    @shishirmishra I feel for you, happened to me too :) But I guess the accepted answer explained better + the screen shot helped perhaps. Anyways, +1 from me for future motivation :)
    – Scott
    Feb 21 '17 at 17:41
  • @scott, I have also mentioned url for more detailed information.Thanks Feb 21 '17 at 18:02
0

what you are looking to do is inserting custom php files inside your theme but sadly WP will not let you use .php files easily but fortunately it provides an alternate and more efficient way for including .php files.

WordPress provides get_template_part function & custom page templates. you can use these methods instead.

above answer has explained page templates and in addition to that you can use your custom php files included into that template or anywhere else using get_template_part function. this works same as require or include native php functions.

template

<?php
/**
 * Template Name: My Page template
 */

php file to be included for example is some-file.php inside root folder. then you would write

get_template_part( 'some', 'file' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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