Last active
December 9, 2017 01:28
-
-
Save sxidsvit/8a165ab50989bdd35b4d939bed8e56de to your computer and use it in GitHub Desktop.
Добавление дополнительных полей к таксономии вручную
This file contains hidden or 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 | |
| // Добавление дополнительных полей к таксономии $taxname | |
| // и создание фильтра для вывод пользовательского поля title в заголовок страницы архива | |
| // Название (слаг, id ) таксономии, к которой будем добавлять поля | |
| $taxname = 'product_cat'; | |
| // Поля при добавлении элемента таксономии | |
| add_action("{$taxname}_add_form_fields", 'add_new_custom_fields'); | |
| // Поля при редактировании элемента таксономии | |
| add_action("{$taxname}_edit_form_fields", 'edit_new_custom_fields'); | |
| // Сохранение при добавлении элемента таксономииè | |
| add_action("create_{$taxname}", 'save_custom_taxonomy_meta'); | |
| // Сохранение при редактировании элемента таксономии | |
| add_action("edited_{$taxname}", 'save_custom_taxonomy_meta'); | |
| // Фильтр позволяет выводить в title архива страницы заданной таксономии | |
| // содержание созданных пользователем полей | |
| // <?php single_term_title() != null ? single_term_title() : 'Каталог товаров';?> | |
| add_filter('single_term_title', 'mayak_filter_single_cat_title', 10, 1 ); | |
| // Функции, прикреплённые к хукам/событиям | |
| function add_new_custom_fields( $taxonomy_slug ){ | |
| // print_r($taxonomy_slug); echo "<br/>"; | |
| ?> | |
| <div class="form-field"> | |
| <label for="tag-title">Заголовок категории</label> | |
| <input name="extra[title]" id="tag-title" type="text" value="" /> | |
| <p>для вывода во фронтенде (title)</p> | |
| </div> | |
| <div class="form-field"> | |
| <label for="tag-description">Краткое описание категории </label> | |
| <input name="extra[description]" id="tag-description" type="text" value="" /> | |
| <p>для вывода во фронтенде (description)</p> | |
| </div> | |
| <?php | |
| } | |
| function edit_new_custom_fields( $term ) { | |
| // print_r($term); echo "<br/>"; | |
| ?> | |
| <tr class="form-field"> | |
| <th scope="row" valign="top"><label>Заголовок категории</label></th> | |
| <td> | |
| <input type="text" name="extra[title]" value="<?php echo esc_attr( get_term_meta( $term->term_id, 'title', 1 ) ) ?>"><br /> | |
| <span class="description">для вывода во фронтенде (title)</span> | |
| </td> | |
| </tr> | |
| <tr class="form-field"> | |
| <th scope="row" valign="top"><label>Краткое описание категории </label></th> | |
| <td> | |
| <input type="text" name="extra[description]" value="<?php echo esc_attr( get_term_meta( $term->term_id, 'description', 1 ) ) ?>"><br /> | |
| <span class="description">для вывода во фронтенде (description)</span> | |
| </td> | |
| </tr> | |
| <?php | |
| } | |
| function save_custom_taxonomy_meta( $term_id ) { | |
| // print_r($term_id); echo "<br/>"; | |
| if ( ! isset($_POST['extra']) ) return; | |
| if ( ! current_user_can('edit_term', $term_id) ) return; | |
| if ( | |
| ! wp_verify_nonce( $_POST['_wpnonce'], "update-tag_$term_id" ) && // wp_nonce_field( 'update-tag_' . $tag_ID ); | |
| ! wp_verify_nonce( $_POST['_wpnonce_add-tag'], "add-tag" ) // wp_nonce_field('add-tag', '_wpnonce_add-tag'); | |
| ) return; | |
| // Все ОК! Теперь, нужно сохранить/удалить данные | |
| $extra = wp_unslash($_POST['extra']); | |
| foreach( $extra as $key => $val ){ | |
| // проверка ключа | |
| $_key = sanitize_key( $key ); | |
| if( $_key !== $key ) wp_die( 'bad key'. esc_html($key) ); | |
| // очистка переданных данных перед записью их в БД | |
| if( $_key === 'tag_posts_shortcode_links' ) | |
| $val = sanitize_textarea_field( strip_tags($val) ); | |
| else | |
| $val = sanitize_text_field( $val ); | |
| // удаление или обновление | |
| if( ! $val ) | |
| delete_term_meta( $term_id, $_key ); | |
| else | |
| update_term_meta( $term_id, $_key, $val ); | |
| } | |
| return $term_id; | |
| } | |
| function mayak_filter_single_cat_title($term_name) { | |
| $queried_object = get_queried_object(); | |
| $taxonomy = $queried_object->taxonomy; | |
| $term_id = $queried_object->term_id; | |
| $terms = get_term( $term_id, $taxonomy); | |
| $term_title = get_term_meta( $term_id, 'title', true); | |
| $term_description = get_term_meta( $term_id, 'description', true); | |
| $term_name = $term_title; | |
| return $term_name; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Отбор страниц для вывода заголовка: