Include Class File in WordPress

I’m including a Class file in functions.php file

require_once get_template_directory() . "/core/classes/General.php";
require_once get_template_directory() . "/core/classes/User.php";

When I do $user->getCredits($user_id); in some page, it works fine. But when I tried doing that same line of code in header.php it prompts an error of Call to a member function getCredits() on null.

So what I did is I required the same file in header.php, it still gives the same error, when I removed the included file in functions.php the error was removed and the code in header.php works ($user->getCredits($user_id)) but then in normal pages it does not detect the required file.

Any solution for this stuff?

1 Answer
1

Please clarify where you initiate the object $user in both ways (function.php and header.php).

This is another thread on stack with similar kind of issue, have a look:

Organizing Code in your WordPress Theme’s functions.php File?

Thanks,
Vee

Leave a Comment