How to stop fatal error when loading theme template file directly

If one tries to load a theme template file directly, the following error message is displayed:

Fatal error: Call to undefined function get_header() in ...

and it goes on to list the full directory path to the wordpress install. This seems undesirable. How can I prevent it? Upon googling I found a suggestion to insert the following into wp-config.php:

Read More
error_reporting(0);
@ini_set(‘display_errors’, 0);

Oddly, it did not work when I tested it.

Thank you!

Related posts

Leave a Reply

1 comment

  1. I define some variables used in my theme at the top of functions.php….one of them being

    $press_here             = 'yes';
    

    Then at the top of my template files I use

    <?php
    global $press_here;
    if( !isset( $press_here ) )
        die('You don't have permission to view this page.');
    

    Which then gives the error message I define there when being accessed directly, nothing more.
    (Thanks @t31os )