The cure for the pesky “Call to undefined function get_header()” in your error logs

getheaderb

You’ve set up your site and all is going rosy until you realize that there’s a extra file in your theme folder name error_logs. Opening the file, you find that there’s lines and lines of the same error that’s glaring you in the eye, and you wonder what’s wrong. Is it the theme, or a plugin or what?

If there’s errors on your site you need to know about it right? And here’s an error so what now.

How does this look like?

Just to ensure that we’re talking about the same thing i posted a screenshot of what this error looks like:

What’s happening here?

A large installation like wordpress loads a huge amount of programs, code and subcode into the program stream to output your site in the end. It starts with a simply file in the base of the wordpress folder called index.php, and loads programs and services till it gets to plugins and themes folder where in the theme folder it also loads an index.php file to add the theme files.

When you go to the site root and load this site’s address you will see the site as it should:

http://www.cro.ma

We have loaded the theme and see all properly and what we’ve loaded is the index.php file of the site.

now lets load the theme index file directly:

http://www.cro.ma/wp-content/themes/themeappers/index.php

You have loaded the theme index file directly and are getting a huge error in stead of the site:

The reason for the error is the fact that you visited the site from the “bottom up” and the files that you have visited up to now, did not have the opportunity of the program files being loaded first, and for that reason the error displays.

Who causes this?

This error is mostly caused by robots that transverse the web looking for pages to index, there’s a possibility that some redirection plugins and maintenance mode redirects also will cause this.

Generally speaking, themes will not cause this error.

What is the remedy?

First we’ll look at a coding solution for this.

in your index.php file right at the top you will find the following:

<?php get_header(); ?>

or

get_header();

You should replace it with the follwing:

<?php if (function_exists('get_header')) {
   get_header();
}else{
    header("Location: http://" . $_SERVER['HTTP_HOST'] . "");
    exit;
}; ?>

Controlling robots

Another less invasive action is to create a file called robots.txt wit ha text editor upload it t othe root directory of wordpress and add the following content in it:

User-agent: *
Disallow: /wp-content/themes/