Last active
December 8, 2021 11:57
-
-
Save chomovva/29985715e0e9f9f59f19be9b6d630263 to your computer and use it in GitHub Desktop.
Dynamic selection of category templates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define( 'THEME_TEXTDOMAIN', 'theme_custom_textdomain' ); | |
/** | |
* Получаем список шаблонов | |
* */ | |
function get_category_templates( $templates ) { | |
$theme_path = get_stylesheet_directory(); | |
$templates = array_merge( $templates, array_map( 'basename', glob( $theme_path . '/category-*.php', GLOB_BRACE ) ) ); | |
return $templates; | |
} | |
add_filter( 'category_custom_templates', 'get_category_templates', 10, 1 ); | |
/** | |
* Добавление поля для выбора шаблона в форме РЕДАКТИРОВАНИЯ категории | |
*/ | |
function edit_category_custom_template( $term ) { | |
// получаем идентификатор уже прикреплённого шаблона | |
$custom_template = get_term_meta( $term->term_id, '_custom_template', true ); | |
// получаем список созданных шаблонов | |
$templates = apply_filters( 'category_custom_templates', [] ); | |
// формируем html-код поля выбора сайдбара | |
if ( is_array( $templates ) && ! empty( $templates ) ) : ?> | |
<tr class="form-field"> | |
<th scope="row" valign="top"> | |
<label for="custom_template"><?php _e( 'Шаблон', THEME_TEXTDOMAIN ); ?></label> | |
</th> | |
<td> | |
<select class="resume-control" id="custom_template" name="custom_template"> | |
<option value=""><?php _e( 'Стандартный шаблон', THEME_TEXTDOMAIN ); ?></option> | |
<?php foreach ( $templates as $value ) : ?> | |
<option value="<?php echo $value; ?>" <?php selected( $custom_template, $value, true ); ?> ><?php echo $value; ?></option> | |
<?php endforeach; ?> | |
</select> | |
</td> | |
</tr> | |
<?php endif; | |
} | |
add_action( 'category_edit_form_fields', 'edit_category_custom_template' ); | |
/** | |
* Добавление поля для выбора шаблона в форме СОЗДАНИЯ категории | |
*/ | |
function add_category_custom_template( $taxonomy_slug ) { | |
// получаем список созданных шаблонов | |
$templates = apply_filters( 'category_custom_templates', [] ); | |
// формируем html-код поля выбора сайдбар | |
if ( is_array( $templates ) && ! empty( $templates ) ) : ?> | |
<div class="form-field"> | |
<label for="custom_template"><?php _e( 'Шаблон', THEME_TEXTDOMAIN ); ?></label> | |
<select class="resume-control" id="custom_template" name="custom_template"> | |
<option value=""><?php _e( 'Стандартный шаблон', THEME_TEXTDOMAIN ); ?></option> | |
<?php foreach ( $templates as $value ) : ?> | |
<option value="<?php echo $value; ?>" ><?php echo $value; ?></option> | |
<?php endforeach; ?> | |
</select> | |
</div> | |
<?php endif; | |
} | |
add_action( 'category_add_form_fields', 'add_category_custom_template' ); | |
/** | |
* Функция сохранения идентификатора прикреплённого шаблона в метаполе категории | |
*/ | |
function save_category_custom_template( $term_id ) { | |
if ( ! current_user_can( 'edit_term', $term_id ) ) return; | |
if ( | |
( isset( $_POST[ '_wpnonce' ] ) && ! wp_verify_nonce( $_POST[ '_wpnonce' ], "update-tag_$term_id" ) ) || | |
( isset( $_POST[ '_wpnonce_add-tag' ] ) && ! wp_verify_nonce( $_POST[ '_wpnonce_add-tag' ], "add-tag" ) ) | |
) return; | |
$custom_template = ( isset( $_POST[ 'custom_template' ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ 'custom_template' ] ) ) : ''; | |
if ( empty( $custom_template ) ) { | |
delete_term_meta( $term_id, '_custom_template' ); | |
} else { | |
update_term_meta( $term_id, '_custom_template', $custom_template ); | |
} | |
return $term_id; | |
} | |
add_action( 'create_category', 'save_category_custom_template' ); | |
add_action( 'edited_category', 'save_category_custom_template' ); | |
/** | |
* Выбор шаблона в публичной части сайта | |
* */ | |
function category_custom_template_include( $default_template ) { | |
if ( is_category() ) { | |
$category = get_queried_object(); | |
$custom_template = get_term_meta( $category->term_id, '_custom_template', true ); | |
$templates = apply_filters( 'category_custom_templates', [] ); | |
if ( ! empty( $custom_template ) && in_array( $custom_template, $templates ) ) { | |
$default_template = get_stylesheet_directory() . '/' . $custom_template; | |
} | |
} | |
return $default_template; | |
} | |
add_filter( 'template_include', 'category_custom_template_include' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment