Skip to content

Instantly share code, notes, and snippets.

@mohamedsalehamin
Last active August 29, 2015 14:22
Show Gist options
  • Save mohamedsalehamin/e98be3121cc93b4af84f to your computer and use it in GitHub Desktop.
Save mohamedsalehamin/e98be3121cc93b4af84f to your computer and use it in GitHub Desktop.
<?php
if (!class_exists('aduta_Category_Template')){
class aduta_Category_Template{
/*
* class constructor
*/
public function __construct()
{
/*
* template selection
*/
// Category
add_filter( 'category_template', array($this,'get_aduta_category_template' ));
// Taxonomy
add_filter( 'template_include', array($this,'get_aduta_taxonomy_template' ));
// add extra fields to category NEW/EDIT form hook
add_action ( 'edit_category_form_fields', array($this,'category_template_meta_box'));
add_action( 'category_add_form_fields', array( &$this, 'category_template_meta_box') );
add_action('admin_init',array($this,'init'),999);
// save extra category extra fields hook
add_action( 'created_category', array( &$this, 'save_category_template' ));
add_action ( 'edited_category', array($this,'save_category_template'));
//extra action on constructor
do_action('aduta_Category_Template_constructor',$this);
}
public function init(){
$args=array(
'public' => true,
'_builtin' => false
);
$output = 'names';
$operator = 'and';
$taxonomies=get_taxonomies($args,$output,$operator);
$exclude = apply_filters('custom_taxonomy_template_tax_exclude',array());
if ($taxonomies) {
foreach ($taxonomies as $taxonomy ) {
if (!in_array($taxonomy,$exclude)){
//add extra fields to taxonomy edit form hook
add_action ( $taxonomy.'_edit_form_fields', array($this,'taxonomy_template_meta_box'));
add_action( $taxonomy.'_add_form_fields', array( &$this, 'taxonomy_template_meta_box') );
// save extra taxonomy extra fields hook
add_action ( 'edited_'.$taxonomy, array($this,'save_taxonomy_template'));
add_action ( 'created_'.$taxonomy, array($this,'save_taxonomy_template'));
}
}
}
}
/**
* category_template_meta_box add extra fields to category edit form callback function
*/
public function category_template_meta_box( $tag )
{
$t_id = $tag->term_id;
$category_meta = get_option( "category_templates");
$template = isset($category_meta[$t_id]) ? $category_meta[$t_id] : false;
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="cat_Image_url"><?php _e('Category Template'); ?></label></th>
<td>
<select name="cat_template" id="cat_template">
<option value='default'><?php _e('Default Template'); ?></option>
<?php page_template_dropdown($template); ?>
</select>
<br />
<span class="description"><?php _e('Select a specific template for this category'); ?></span>
</td>
</tr>
<?php
do_action('aduta_Category_Template_ADD_FIELDS',$tag);
}
public function taxonomy_template_meta_box( $tag )
{
$t_id = isset($tag->term_id) ? $tag->term_id: 0;
$tax_meta = get_option( "taxonomy_templates");
$template = isset($tax_meta[$t_id]) ? $tax_meta[$t_id] : false;
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="cat_Image_url"><?php _e('taxonomy Template'); ?></label></th>
<td>
<select name="tax_template" id="tax_template">
<option value='default'><?php _e('Default Template'); ?></option>
<?php page_template_dropdown($template); ?>
</select>
<br />
<span class="description"><?php _e('Select a specific template for this taxonomy'); ?></span>
</td>
</tr>
<?php
do_action('Custom_Taxonomy_Template_ADD_FIELDS',$tag);
}
/**
* save_category_template save extra category extra fields callback function
*/
public function save_category_template( $term_id ) {
if ( isset( $_POST['cat_template'] )) {
$category_meta = get_option( "category_templates");
$category_meta[$term_id] = $_POST['cat_template'];
update_option( "category_templates", $category_meta );
do_action('aduta_Category_Template_SAVE_FIELDS',$term_id);
}
}
public function save_taxonomy_template( $term_id )
{
if ( isset( $_POST['tax_template'] ) ) {
$tax_meta = get_option( "taxonomy_templates");
$tax_meta[$term_id] = $_POST['tax_template'];
update_option( "taxonomy_templates", $tax_meta );
do_action('Custom_Taxonomy_Template_SAVE_FIELDS',$term_id);
}
}
/**
* get_aduta_category_template handle category template picking
*/
function get_aduta_category_template( $category_template ) {
$cat_ID = absint( get_query_var('cat') );
$category_meta = get_option('category_templates');
if (isset($category_meta[$cat_ID]) && $category_meta[$cat_ID] != 'default' ){
$temp = locate_template($category_meta[$cat_ID]);
if (!empty($temp))
return apply_filters("aduta_Category_Template_found",$temp);
}
return $category_template;
}
function get_aduta_taxonomy_Template( $taxonomy_template ) {
if (!is_tax())
return $taxonomy_template;
$term_slug = get_query_var( 'term' );
$taxonomyName = get_query_var( 'taxonomy' );
$current_term = get_term_by( 'slug', $term_slug, $taxonomyName );
$term_ID = $current_term->term_id;
$tax_meta = get_option('taxonomy_templates');
if (isset($tax_meta[$term_ID]) && $tax_meta[$term_ID] != 'default'){
$temp = locate_template($tax_meta[$term_ID]);
if (!empty($temp))
return apply_filters("Custom_Taxonomy_Template_found",$temp);
}
return $taxonomy_template;
}
}//end class
}//end if
$cat_template = new aduta_Category_Template();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment