0

Ok, so i'm instantiating a class in page.php.

Then i'm calling get_header() to include header.php

now in header.php i need to access this class object and execute one of its methods but i get this error:

\nFatal error: Uncaught exception 'BadMethodCallException' with message 'Call to a member function links() on a non-object (NULL)'

should i just use require instead of get_header() ?

1
  • 1
    No, you should use get_header() because of the way it interacts with the rest of WordPress, and will make your theme much easier for other developers to interact with. If there is no alternate solution, you can just make the variable global. I would try to conditionally check what template, page, etc is being loaded in header.php and then handle all the class instantiating there. Commented Jan 17, 2015 at 21:49

1 Answer 1

0

Generally said, there should nothing be happening before the get_header() call.

So initiate your class if is_page() is true via wp_head action hook. Example:

add_action( 'wp_head', 'initiate_my_class' );
function initiate_my_class() {
    if ( is_page() ) {
        //code
    }
}

Edit: As @AndrewBartel in his comment said you can globalize your variable or implement some kind of storage system via the class or another one.

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.