Skip to content

Instantly share code, notes, and snippets.

@goranefbl
Created April 10, 2016 21:49
Show Gist options
  • Save goranefbl/470af4f2f6ef069113cd47d7322ec557 to your computer and use it in GitHub Desktop.
Save goranefbl/470af4f2f6ef069113cd47d7322ec557 to your computer and use it in GitHub Desktop.
<?php
/**
* @package AMRW
* @subpackage Admin functions
*/
/**
* Class AMRW_Page_Template
* This class adds Room View page template to Page Template selector
* @author Sibin Grasic [email protected]
* @since 0.1
*/
Class AMRW_Page_Template
{
/**
* @var array - List of templates we want to add to theme
*/
protected $templates;
public function __construct()
{
$this->plugin_slug = 'AMRWBRE';
$this->templates = array();
add_filter('page_attributes_dropdown_pages_args', array( $this, 'register_amrw_template'));
add_filter('wp_insert_post_data', array( $this, 'register_amrw_template' ));
add_filter('template_include',array($this,'view_amrw_template'));
$this->templates = array(
'templates/am_room_view_template.php' => 'Room View'
);
}
public function register_amrw_template($atts)
{
$cache_key = 'page_templates-'.md5( get_theme_root() . '/' . get_stylesheet() );
$templates = wp_get_theme()->get_page_templates();
if (empty($templates))
$templates = array();
wp_cache_delete( $cache_key , 'themes');
$templates = array_merge( $templates, $this->templates );
wp_cache_add( $cache_key, $templates, 'themes', 1800 );
return $atts;
}
public function view_amrw_template($template)
{
global $post;
if (!isset($this->templates[get_post_meta($post->ID, '_wp_page_template', true)]))
return $template;
$file = AMRW_PATH. get_post_meta($post->ID, '_wp_page_template', true);
if(file_exists( $file ))
return $file;
else
echo $file;
return $template;
}
}
$AMRW_PT = new AMRW_Page_Template();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment