0

I have created a plugin that registers a custom post type. I would like to load a custom header and footer for the custom post type from the plugin.

After some google research I can see that it's not possible to use get_header() to load and so I have tried using:

<?php get_template_part('header', 'trademanager'); ?>

Which I can now see is exactly the same as using get_header() and so it obviously fails. The codex has pointed me towards load_template() https://codex.wordpress.org/Function_Reference/load_template

I don't really understand how to use it, can someone help me figure this out please?

My custom theme file is header-trademanager.php and is located in plugin/templates

1

I don't believe you can change the loaded file for get_header/get_footer to something outside the stylesheet/theme directory.

If you put header-trademanager.php in the theme folder, get_header("trademanager") should work.

I had expected to see a filter applied somewhere in get_header or locate_template, but it seems that it's not customizable at this point.

  • I can't place the file in a theme folder, it must be in the plugin folder. Do you know anything about load_template()? – lukgoh Oct 7 '17 at 14:19
  • I don't believe load_template is what you want. It'll allow you to use another template file entirely, but not to just override the header that's loaded. What exactly is the difference between your header and the default header? Maybe it can be achieved in a different manner. – janh Oct 7 '17 at 14:25
  • The plugin kind of acts as a backend display for the custom post type so I dont want the themes header to load but I dont want to have to code the head section on every page. – lukgoh Oct 7 '17 at 14:33
  • But you're using a custom template already, why use get_header() at all instead of simply requiring your header-trademanager.php and executing wp_head() in there? – janh Oct 7 '17 at 14:49
  • Im using load_template() and its working nicely so far. Thanks for the help. – lukgoh Oct 7 '17 at 16:16
0

There is no filter that can be used to change the location of the header template. You can change the name of the template, but it has to be in the theme or child theme directory. This is unlike all the other theme templates that can be filtered, header and footer templates have to be in theme or child theme folders.

Function load_template() doesn't have filters to change the file path.

0

In general front end should not be handled in a plugin except for widgets and shortcodes. Front end is the sole responsibility of a theme.

You can play with this or that hook and get something that works somtimes, but it is unlikely to work with all themes.

Best bet is to utilize the page.php or single.php templates of the theme. Load it for your post type, and manipulate whatever is possible there. Still this is at best just to be able to have some output, the chances of it to actually look good for more than the few themes you will test it with are slim.

0

Old question, but maybe my answer will help someone.


Short answer is:

use load_template() function with full file server path as first argument passed to it.


Long answer:

get_header() using the function locate_template() which uses the function load_template() which uses the function require_once(). Thus, to load the header layout from a plugin or any other directory, you can directly use require_once() and this will be completely correct if global variables like $post and $wp_query are not needed, otherwise case, use the load_template() function passing the full server path to the file as the first argument.


P.S.

For more certainty, you can look at the files wp-includes/general-template.php and wp-includes/template.php, which contain all these functions. And you will see that there is no complicated logic in them, everything is very simple. The only reason it’s better to use standard features is support. Support for the development of WP code lies on the shoulders of the team of developers, and your own functions are on yours.

But in this case, when you need to require your own header file - this may be a justifiable solution.

New contributor
Пашка is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.