0

From this Stack Overflow qestion It shows a bunch of ways to make page templates. But I don't want to use a page template that uses Gutenberg. Is there another way to make a PHP file like a tos.php, and link to it by <?php echo get_template_directory_uri() . '/tos.php'; ?> I tried to link the page that way but gives be a blank page.

    <?php 
    /* Template Name: ghfjghjg*/

    get_header();
    ?>

    <main id="primary" class="site-main main-header">
        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor    incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

    </main><!-- #main -->

    <?php
    get_footer();

Also tried, adding this to my functions.php.

function include_TOS() {
    return  __DIR__ . 'tos.php';

}
apply_filters( 'template_include', 'include_TOS' );
7
  • what are you trying to do that requires this? esc_html_e is an escaping/localisation function, it's not a way to load and output files. It's unclear what you want to do or why you want to do it, use the edit link underneath the tags to improve and update your question.
    – Tom J Nowell
    Commented Feb 27, 2022 at 21:14
  • are you sure because I got esc_html_e from the underscores template? And I added more.
    – Void
    Commented Feb 27, 2022 at 21:38
  • The Link I posted is not what I want I just want to link to the file in my theme. Is there another way?
    – Void
    Commented Feb 27, 2022 at 21:46
  • You shouldn't be using variables in esc_html_e, that function does not do what you think it does. Are you trying to create a example.com/tos page by putting a tos.php file in your theme? echo does not include a file, it just prints out a string of text, in this case the location of the file. Also you don't use apply_filters to add a filter, you use add_filter
    – Tom J Nowell
    Commented Feb 27, 2022 at 22:54
  • 1
    indeed, it is not possible, that's not how PHP theme templates work. The absolute closest you could get is a page-tos.php that would load for a page with the slug tos. Otherwise you would need to build a rewrite rule and a virtual page which would involves multiple filters, and is not simple to do. I know you want to save yourself a step but you're actually taking something that takes 5 minutes and turning it into 5 hours of work plus more of research
    – Tom J Nowell
    Commented Feb 28, 2022 at 0:51

1 Answer 1

0

get_header() and get_footer() are WordPress functions, not native PHP. If the code snippet you've posted is the entire content of tos.php, you're not loading WordPress at all, the functions don't exist, and so tos.php is throwing a fatal error. It's likely that your web host is configured to not display fatal errors, so you see an entirely blank page.

Further: Gutenberg is an editor, and shouldn't affect the display of pages on the front end. If you're trying to specify that a given post or page should use your tos.php template, you should have a look at the template_include filter.

2
  • From the link, you posted there are some comments on how to use it. Do I need a function for it to work? Or just add it somewhere(You didn't say where) will make the link work on the home page of my site.
    – Void
    Commented Feb 27, 2022 at 20:52
  • I updated the question.
    – Void
    Commented Feb 27, 2022 at 21:47

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.