Advertisement
HarunRRayhan

Create Template from WP Plugin

Jul 25th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.72 KB | None | 0 0
  1. <?php
  2.  
  3. if(!class_exists('HRXPageTemplater')):
  4. class HRXPageTemplater {
  5.  
  6.     /*
  7.      * A reference to an instance of this class.
  8.      */
  9.     private static $instance;
  10.  
  11.     /**
  12.      * The array of templates that this plugin tracks.
  13.      */
  14.     protected $templates;
  15.  
  16.     /**
  17.      * Returns an instance of this class.
  18.      */
  19.     public static function get_instance() {
  20.  
  21.         if ( null == self::$instance ) {
  22.             self::$instance = new HRXPageTemplater();
  23.         }
  24.  
  25.         return self::$instance;
  26.  
  27.     }
  28.  
  29.     /**
  30.      * Initializes the plugin by setting filters and administration functions.
  31.      */
  32.     private function __construct() {
  33.  
  34.         $this->templates = array();
  35.  
  36.  
  37.         // Add a filter to the attributes metabox to inject template into the cache.
  38.         if ( version_compare( floatval( get_bloginfo( 'version' ) ), '4.7', '<' ) ) {
  39.  
  40.             // 4.6 and older
  41.             add_filter(
  42.                 'page_attributes_dropdown_pages_args',
  43.                 array( $this, 'register_project_templates' )
  44.             );
  45.  
  46.         } else {
  47.  
  48.             // Add a filter to the wp 4.7 version attributes metabox
  49.             add_filter(
  50.                 'theme_page_templates', array( $this, 'add_new_template' )
  51.             );
  52.  
  53.         }
  54.  
  55.         // Add a filter to the save post to inject out template into the page cache
  56.         add_filter(
  57.             'wp_insert_post_data',
  58.             array( $this, 'register_project_templates' )
  59.         );
  60.  
  61.  
  62.         // Add a filter to the template include to determine if the page has our
  63.         // template assigned and return it's path
  64.         add_filter(
  65.             'template_include',
  66.             array( $this, 'view_project_template')
  67.         );
  68.  
  69.  
  70.         // Add your templates to this array.
  71.         $this->templates = array(
  72.             'templates/postal-code.php' => 'Postal Code',
  73.             'templates/driveway-form.php' => 'Driveway Form',
  74.         );
  75.            
  76.     }
  77.  
  78.     /**
  79.      * Adds our template to the page dropdown for v4.7+
  80.      *
  81.      */
  82.     public function add_new_template( $posts_templates ) {
  83.         $posts_templates = array_merge( $posts_templates, $this->templates );
  84.         return $posts_templates;
  85.     }
  86.  
  87.     /**
  88.      * Adds our template to the pages cache in order to trick WordPress
  89.      * into thinking the template file exists where it doens't really exist.
  90.      */
  91.     public function register_project_templates( $atts ) {
  92.  
  93.         // Create the key used for the themes cache
  94.         $cache_key = 'page_templates-' . md5( get_theme_root() . '/' . get_stylesheet() );
  95.  
  96.         // Retrieve the cache list.
  97.         // If it doesn't exist, or it's empty prepare an array
  98.         $templates = wp_get_theme()->get_page_templates();
  99.         if ( empty( $templates ) ) {
  100.             $templates = array();
  101.         }
  102.  
  103.         // New cache, therefore remove the old one
  104.         wp_cache_delete( $cache_key , 'themes');
  105.  
  106.         // Now add our template to the list of templates by merging our templates
  107.         // with the existing templates array from the cache.
  108.         $templates = array_merge( $templates, $this->templates );
  109.  
  110.         // Add the modified cache to allow WordPress to pick it up for listing
  111.         // available templates
  112.         wp_cache_add( $cache_key, $templates, 'themes', 1800 );
  113.  
  114.         return $atts;
  115.  
  116.     }
  117.  
  118.     /**
  119.      * Checks if the template is assigned to the page
  120.      */
  121.     public function view_project_template( $template ) {
  122.        
  123.         // Get global post
  124.         global $post;
  125.  
  126.         // Return template if post is empty
  127.         if ( ! $post ) {
  128.             return $template;
  129.         }
  130.  
  131.         // Return default template if we don't have a custom one defined
  132.         if ( ! isset( $this->templates[get_post_meta(
  133.             $post->ID, '_wp_page_template', true
  134.         )] ) ) {
  135.             return $template;
  136.         }
  137.  
  138.         $file = plugin_dir_path( __FILE__ ). get_post_meta(
  139.             $post->ID, '_wp_page_template', true
  140.         );
  141.  
  142.         // Just to be safe, we check if the file exist first
  143.         if ( file_exists( $file ) ) {
  144.             return $file;
  145.         } else {
  146.             echo $file;
  147.         }
  148.  
  149.         // Return template
  150.         return $template;
  151.  
  152.     }
  153.  
  154. }
  155. add_action( 'plugins_loaded', array( 'HRXPageTemplater', 'get_instance' ) );
  156. endif;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement