0

I'm creating a custom WordPress theme, more specifically the index.php file. When I navigate to my website main page, the header is displayed correctly, but occasionally errors show in the server log:

PHP Fatal error:  Uncaught Error: Call to undefined function get_header() in D:\home\site\wwwroot\wp-content\themes\OryTheme\index.php:1
Stack trace:
#0 {main}
  thrown in D:\home\site\wwwroot\wp-content\themes\OryTheme\index.php on line 1

I don't know why it gives an error if it displays the header. Also, if I remove <?php get_header(); ?>, the header won't get displayed. Can I ignore these errors?

index.php

<?php get_header(); ?>

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    <div class="col-lg-6 col-sm-6 col-md-6 col-xs-12">
    <h1><?php the_title(); ?></h1>
    <h4>Posted on <?php the_time('F jS, Y') ?></h4>
    <p><?php the_content(__('(more...)')); ?></p>
    </div>
    <?php endwhile; else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>

<?php endif; ?>
<?php get_sidebar(); ?>

<?php get_footer(); ?>

1 Answer 1

1

If you or someone else tries to visit a URL like this: https://yoursite.dev/wp-content/themes/OryTheme

You should get an Uncaught Error: Call to undefined function get_header() in .... error.

Most likely you or someone else is hitting that direct theme directory of your website and it's executing index.php but unfortunately, you won't get any wordpress functions there. So it's logging an error.

If you would like to get rid of this types of errors. You can always put a code like this in the beginning of PHP files of your custom theme and plugins.

<?php 
if ( !defined( 'ABSPATH' ) ) exit; // don't allow to call the file directly

// start coding here 
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer. I don't really understand what you mean by hitting the direct theme directory. I'm hosting the server in Azure, and sometimes when I navigate to my main page (thus loading my theme's index.php), the azure server console shows the error. Though, as I said, the header gets loaded perfectly fine, and the theme won't work without that line.
By the word 'hitting', I mean to visit that URL. Just try using these code, into index.php and you should never get that error in your log. pastebin.com/jhuJbHz3
FYI, if ( !defined( 'ABSPATH' ) ) exit; this lines of code just to check if WordPress is executing/including that file. If this file is getting called manually from somewhere it will just exit rather than throwing an error.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.