How to use custom footer template in a site-plugin?

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

Topic page-template plugin-development customization plugins Wordpress

Category Web


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:


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()).

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.