• Resolved greencode

    (@greencode)


    Although this doesn’t affect the site I’d like to get it removed from the error log, permanently. I get the following error appear daily. I don’t use the index.php file in my theme so I could, ultimately, just remove all content from that file but it’d be nice to know why it happens.

    [25-Oct-2016 23:15:38 UTC] PHP Fatal error: Call to undefined function get_header() in /home/mysite/public_html/site/wp-content/themes/sitetheme/index.php on line 1

    Here’s the code on that page:

    
    <?php get_header(); ?>
    
    <div id="primary" role="main">
    
    <?php while ( have_posts() ) : the_post(); ?>
    
    <h1 class="title"><?php the_title(); ?></h1>
    
    <div class="entry-content">
    
    <?php the_content(); ?>
    <?php wp_link_pages(array('before' => '<p><strong>'.__('Pages:', 'framework').'</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
    <!--END .entry-content -->
    </div>
    
    <?php endwhile; // end of the loop. ?>
    
    </div><!-- #content -->
    
    <?php get_footer(); ?>
    

    Any ideas?

Viewing 5 replies - 1 through 5 (of 5 total)
  • I believe that error is generated when someone–probably a bot–tries to access the theme’s index.php file directly, in which case the get_header() function isn’t available.

    You could change this part of the theme’s index.php code:

    
    <?php get_header(); ?>
    

    to something like the following:

    
    <?php
      if (function_exists('get_header')) {
        get_header();
      } else {
        // Whatever you want to do when the file is accessed directly would go here. Maybe redirect them to your home page?
      }
    ?>
    
    Thread Starter greencode

    (@greencode)

    Thanks for this. Is this how I direct it to another page:

    
    <?php
      if (function_exists('get_header')) {
        get_header();
      } else {
        header("Location: https://");
       exit;
      }
    ?>
    

    Yes, you could use the header function to do that:

    
    <?php
      if (function_exists('get_header')) {
        get_header();
      } else {
        $url = "https://yourdomain.com";
        header("Location: " . $url);
        exit();
      }
    ?>
    

    Or, since nobody should be accessing your theme’s index.php directly (it’s probably a ‘bot), you can just kill the process and save the resources that a redirect would require:

    
    <?php
      if (function_exists('get_header')) {
        get_header();
      } else {
        die();
      }
    ?>
    
    • This reply was modified 8 years, 4 months ago by linux4me2.
    Thread Starter greencode

    (@greencode)

    Thanks for this. Works perfectly. No more errors ??

    The error is caused by somebody calling directly your index.php in the themes folder.

    f.e. https://yourwebsite.com/wp-content/themes/theme-name/index.php

    The fix is easy and you only need to add this in the top of the index.php just after the first <?php and before the get_header(); call, so you get this.

    <?php 
    if ( ! defined( 'ABSPATH' ) ) {
    	exit;
    }
    get_header();

    If you want to use the linux4me solution the code should be like this as nobody want to add their website url into that file on every update of the theme and you also want to cover https as well as http websites.

       if (function_exists('get_header')) {
        get_header();
      } else {
        $url = "/";
        header("Location: " . $url);
        exit();

    }`

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Fatal error undefined function get_header() in index.php on line 1’ is closed to new replies.