GaryJones / Gamajo-Template-Loader

A class to copy into your WordPress plugin, to allow loading template parts with fallback through the child theme > parent theme > plugin.
GNU General Public License v2.0
292 stars 61 forks source link

Cannot use in head.php or wp_head() #42

Open CullenJWebb opened 3 years ago

CullenJWebb commented 3 years ago

Hello,

First off, thank you so much for a valuable tool such as this!

However, I ran into a hickup today as I attempted to use a template part in the header.php file. I run into this error:

PHP Fatal error: Uncaught Error: Call to a member function get_template_part() on null in [MY PLUGIN FILE.PHP]:144\nStack trace:\n#0 [MY WP DIR]/wp-includes/class-wp-hook.php(305): [MYPREFIX]_banner_images(NULL)\n#1 

[MY WP DIR]/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters('', Array)\n#2 

[MY WP DIR]/wp-includes/plugin.php(470): WP_Hook->do_action(Array)\n#3 

[MY WP DIR]/wp-includes/general-template.php(38): do_action('get_header', NULL, Array)\n#4 

[MY THEME DIR]/home-page.php(6): get_header()\n#5 

[MY WP DIR]/wp-includes/template-loader.php(106): include('/nas/content/li...')\n#6 

[MY WP DIR]/wp-blog-header.php(19): require_once('/nas/content/li...')\n#7 

[MY WP DIR]/index.php(17): require('/nas/content/li...')\n#8 

{main}\n thrown in [MY PLUGIN FILE.PHP] on line 144

In this specific example it mentions line 144 in my plugin only because I attempted to add an action to the header as a workaround. Here's what I tried in addition to editing header.php directly:

add_action('get_header', '[MY FUNCTION WITH TEMPLATE PART]');

Is there any way to get this working in the header?

GaryJones commented 3 years ago

There's not enough information here to reproduce or determine the issue. I'd need to see the custom template loader class that extends the Gamajo_Template_Loader, find out where those files are located, and see how you're are instantiating the custom template loader class and how/where you're calling get_template_part().