0

I have a site-plugin to centralize most of the theme customization. I want to override the default footer.php template with a customfooter.php (removes the Proudly Powered by WordPress) stored inside the /plugins directory structure. I found this resource on StackExchange, one here, and tried some. Obviously, I am making errors. Could someone please advise how to achieve this without touching the theme files / directories?

add_filter( 'theme_page_templates', 'force_customization', 101 );
function force_customization( $newtemplate ) {
       if ( is_singular( 'footer' ) ) {
        $newtemplate = plugin_dir_path( __FILE__ ) . 'customization/customfooter.php';
     }
     return $newtemplate;
}

Any / all help is welcome.

Kind regards

Abhijeet

2 Answers 2

0

You cannot override header / footer templates from a plugin, you'll have to change the theme.

theme_page_templates can be used to change the general template for the post type page. That template then uses get_header() and get_footer(), which in turn include header.php / footer.php (optionally header-$name.php, if a parameter has been passed to get_header(), same for get_footer()).

3
  • Thank you for the insight. Just to clarify - Are you pointing to a constraint for theme_page_templates or instead suggesting that footer.php cannot be overridden using any WordPress method? For instance, I tried using the is_page_template too, but it does not work. I am a newb with WP and coding and trying to centralize customization to ease ongoing management of my site. Is it possible to achieve? Any / all suggestions are welcome. Thank you Oct 22, 2017 at 7:17
  • The latter. You cannot make WP use a different file than footer.php from outside the theme. If you're working in the theme/child-theme, you can use get_footer("special") and it will prefer footer-special.php over footer.php (but still fall back on footer.php if the other one doesn't exist). You'll have to create a child theme and make your modifications there.
    – janh
    Oct 22, 2017 at 8:25
  • Thank you very much. I have been trying to work this for some time and your response helps to move on :) Kind regards. Oct 22, 2017 at 9:24
0

Here's how I added a custom header and footer to my custom plugin.

plugin.php:

function _get_header($name, $require_once = TRUE, $args = []) {
  return load_template(dirname(__FILE__) . "/includes/{$name}.php", $require_once, $args);
}

function _get_footer($name, $require_once = TRUE, $args = []) {
  return load_template(dirname(__FILE__) . "/includes/{$name}.php", $require_once, $args);
}

Create your templates:

  • /includes/my-header.php
  • /includes/my-footer.php

In your template file, e.g. single-custom.php:

_get_header('my-header'); //<- name of file without extension.
_get_footer('my-footer'); //<- name of file without extension.

Here are the twentytwentyone files from WP:

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.