WordPress custom plugin code to create a custom WordPress page template

WordPress provide a facility to set template and there is many template provide in any particular theme also but for any custom plugin, this is basic requirement for any plugin author to include code to create custom page template automatically as per custom requirement so now we will discuss how can we implement.

template

Custom template is the way to change normal post or page output and design own HTML code as per our requirement. template use to overwrite many type of pages like category pages or any custom post type pages but here we discuss about normal single page template design

main benefit of having own template is to use our custom page template in as many pages as we want so we need to implement code once and it will available to all pages

there are many plugins also available so we can use any plugin but as we are create our own plugin code then why we use other plugin code along with our custom plugin.
it is just simple with few line of code to implement and use to 

let's start. 

How to create custom page template with few steps below. we need to add only 2 files in our custom plugin

In this example, file names are below
1) class-template.php  // class to initiate template functionality 
2) custompagetemplate.php  //  actual template file

Step 1: Define template class 
Step 2: Create template file
Step 3: Assign template from edit/add page template dropdown option
Step 4: Done

/* Let's see in detail for each steps */ 

Step 1: Define template class 

Create one file with name "class-template.php" in own custom plugin 
include in main plugin file

Here code below use to define entire class template structure code along with all method. so just copy paste below code in your class-template.php file

<?php

class CustomPageTemplater {
private static $templateinstance;
protected $templates;
public static function get_template_instance() {

if ( null == self::$templateinstance ) {
self::$templateinstance = new CustomPageTemplater();

return self::$templateinstance;

private function __construct() {

$this->templates = array();

if ( version_compare( floatval( get_bloginfo( 'version' ) ), '4.7', '<' ) ) {

// ver 4.6 and older
add_filter('page_attributes_dropdown_pages_args',array( $this, 'addorregister_project_templates' ) );

} else {
add_filter('theme_page_templates', array( $this, 'add_new_template_func' ));

}

// Add wp_insert_post_data filter to the save post and define out template into the page 
add_filter('wp_insert_post_data',array( $this, 'addorregister_project_templates' ));

add_filter('template_include',array( $this, 'project_template_view') );


// Add mutiple templates in this array as many as you want
$this->templates = array('custompage-template.php' => 'Custom page template'); // 'FILEPATH' => 'TEMPLATE_DROPDOWN_TITLE',

/* Add our template to the page dropdown  */
public function add_new_template_func( $posts_templates ) {
$posts_templates = array_merge( $posts_templates, $this->templates );
return $posts_templates;
}
public function addorregister_project_templates( $atts ) {

// Create the key which use for cache
$cache_key = 'page_templates-' . md5( get_theme_root() . '/' . get_stylesheet() );

// If templates not exist, or empty the set an array
$templates = wp_get_theme()->get_page_templates();
if ( empty( $templates ) ) {
$templates = array();

// remove the old cache
wp_cache_delete( $cache_key , 'themes');

// Add template to the list of templates by merging our templates
$templates = array_merge( $templates, $this->templates );

// Add the modified cache to WordPress to pick up for dropdown listing
wp_cache_add( $cache_key, $templates, 'themes', 1800 );

return $atts;


public function project_template_view( $template ) {
global $post;
if ( ! $post ) {
return $template;
}

// Return default template if we don't have defined
if ( ! isset( $this->templates[get_post_meta($post->ID, '_wp_page_template', true)] ) ) {
return $template;

$file = plugin_dir_path( __FILE__ ). get_post_meta( $post->ID, '_wp_page_template', true);

// check if the file exist 
if ( file_exists( $file ) ) {
return $file;
} else {
echo $file;
}
return $template;
}

add_action( 'init', array( 'CustomPageTemplater', 'get_template_instance' ) ); 
// here we initiate template class method get_template_instance
?>

Step 2: Create template file

Now it's time to design own template  

Create one file with name "custompagetemplate.php" which is defined in above class-template.php file with below line

// $this->templates = array('custompagetemplate.php' => 'Custom page template');

Now copy paste below code in custompagetemplate.php file 

<?php
/**
 * Template Name: Custom page template
 *
 **/  
ob_start();
get_header(); 
?>
<div class="content" >
// here write your own code as per requirement
</div>

<?php
    get_footer();
?>

Above code will create template with your website header and footer. if you have to change your template completely different then do not add/include header, footer functions.

Step 3: Assign template from edit/add page template dropdown option

After done step 1&2 now it's time to assign template from admin dashboard page option

Now click on already created page or create new page 

for any WordPress page, there is one [page attribute->template] option provided to set template so now we can see our custom page template will be available in dropdown option

select our template name and save page

Step 4: done

please visit frontend to see template and enjoy


share your comment below if this post help you
Previous
Next Post »