Creating a custom template loader
First, we have to add a function to the WP_Theme
class of the plugin to include the template loader file, as shown in the following code. Also, we have to update the constructor to call the template_init
function upon plugin initialization:
public function template_init() { include_once 'class-wpwa-template-loader.php'; }
Create a new file named class-wpwa-template-loader.php
inside the plugin class to implement the template loader. Let's implement the loader by creating a new class named WPWA_Template_Loader
inside the class-wpwa-template-loader.php
file:
class WPWA_Template_Loader{ private $template_dir; public function __construct(){ $this->template_dir = "templates"; } public function render($name,$data = array() ){ include plugin_dir_path(__FILE__).$this->template_dir."/".$name.".php"; } }
The folder named templates
will be used to create all the templates required for the theme. Then, we will add a function named...