Overridable plugin templates from theme

Most of the websites owners want to extend some plugins functionality as per their requirement. But as per development perspective developer can’t modify changes directly into plugin, because if plugin get updated then all changes or extra code written in plugin will erase. So whats a solution on it?

Solution is while making plugin from scratch we can build its templates or files overridable from theme, so without changing default template plugin will execute file from theme. Let’s see how it will work.

I am making one simple plugin called “Testing-plugin” for demonstration purpose. This plugin has only one main file “testing-plugin.php” and one folder of “template” with template file ie.”shortcode-content.php”, and this template we will override from theme.

Template File : Shortcode-content.php

Folder structure : testing-plugin/templates/shortcode-content.php

<?php

/**
 * Default: Template for testing plugin.
 */

echo "This content comming from template file of plugin<br><br>";

?>

In main plugin file(testing-plugin.php) we have to avoid include template directly from plugin like below.

// We have to avoid this direct include file of plugin if you want to make this template overridable from theme
$template_file = __DIR__ . '/templates/shortcode-content.php';
include $template_file;

Main Plugin File : testing-plugin.php

Folder structure : testing-plugin/testing-plugin.php

<?php
/*
  Plugin Name: Testing Plugin
  Plugin URI: http://codetrycatch.com
  description: Plugin templates override from theme
  Version: 1.0
  Author: Sagar Walzade
  Author URI: http://codetrycatch.com
  License: GPL2
 */

add_shortcode('testing_plugin_shortcode', 'testing_plugin_shortcode_callback');

function testing_plugin_shortcode_callback() {

    echo '<b>Shortcode content from template : </b><br>';

    $template_file = get_stylesheet_directory() . '/testing-plugin/shortcode-content.php';

    if (!file_exists($template_file)) {

        $template_file = __DIR__ . '/templates/shortcode-content.php';
    }

    include $template_file;
}
?>

So instead of direct template call we have to set file exist condition in plugin that checks first if same name template file is exist in theme then it will include theme file or else it will include default one that is already present in plugin. Now we have to just create one folder of our plugin name “testing-plugin” and just need to create a template file which we want to override its code/feature.
Note : while creating this folder and file in theme, its name should be same as per plugin name and template files.

Conclusion :

As you can see, to make overridable plugin templates are not a difficult task at all. So we have made one demo plugin and to demonstrate all this we have just created same name file in theme and added condition in plugin to include, this is how we can make overridable templates. I hope this information is useful to you and let me know if you try this or try any other method which is completely different from this solution. In future post we will see some more features provided by WordPress. So stay connected with my blog to get useful stuff in simple manner, and i will be more happy to get some feedback from you in comment section, and also share this post if you like, if it is useful for others. Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *