問題描述

這似乎相當簡單,但是我無法找到有關如何將分類法描述欄位新增到快速編輯框的明確指南。

我已經遵循了幾個關於在自定義快速編輯框中新增欄位的教程,但是核心欄位 (名稱,描述和小數) 不符合將自定義欄位新增到分類快速編輯框中的相同準則。

甚至有可能這樣做嗎?如果沒有 – 我想最好的替代是建立另一個’Description’ meta-field,並將其新增到分類快速編輯框。

最佳解決方案

通常要向快速編輯欄位新增欄位,因此我們應該使用僅針對自定義列觸發的'quick_edit_custom_box'動作鉤子,因為明確排除了核心列 (see code) 。

如果我們新增一個自定義列,那麼它將顯示在列表中,但是沒有任何意義,因為列描述已經存在。

但是,我們有可能使用 2 個技巧新增一個不可見的列:

  1. 將其標籤設定為空字串 (以這種方式,它不會顯示在”Screen Option” 設定中)

  2. 強制列隱藏在 get_user_option_manageedit-{$taxonomy}columnshidden 過濾鉤上

首先建立隱形列:

/*
 * This is NOT required, but I'm using it to easily let you customize the taxonomy
 * where to add the inline description.
 * You can replace $the_target_tax in all the code with the name of your taxonomy,
 * with no need to use the global variable.
 */
global $the_target_tax;
$the_target_tax = 'category';

add_filter( "manage_edit-{$the_target_tax}_columns", function( $columns ) {
    $columns['_description'] = '';
    return $columns;
});

add_filter( "manage_{$the_target_tax}_custom_column", function( $e, $column, $term_id ) {
    if ( $column === '_description' ) return '';
}, 10, 3 );

add_filter( "get_user_option_manageedit-{$the_target_tax}columnshidden", function( $r ) {
    $r[] = '_description';
    return $r;
});

現在我們有一個不可見的自定義列'_description',但可以透過'quick_edit_custom_box'鉤子使用其他欄位:

但是,這個鉤子並沒有傳遞任何當前值到 pre-fill 的欄位與當前描述,但我們可以使用幾行 jQuery 來做到這一點:

add_action( 'quick_edit_custom_box', function( $column, $screen, $tax ) {
    if ( $screen !== 'edit-tags' ) return;
    $taxonomy = get_taxonomy( $tax );
    if ( ! current_user_can( $taxonomy->cap->edit_terms ) ) return;
    global $the_target_tax;
    if ( $tax !== $the_target_tax || $column !== '_description' ) return;
    ?>
    <fieldset>
        <div class="inline-edit-col">
        <label>
            <span class="title"><?php _e('Description'); ?></span>
            <span class="input-text-wrap">
            <textarea id="inline-desc" name="description" rows="3" class="ptitle"></textarea>
            </span>
        </label>
        </div>
    </fieldset>
    <script>
    jQuery('#the-list').on('click', 'a.editinline', function(){
        var now = jQuery(this).closest('tr').find('td.column-description').text();
        jQuery('#inline-desc').text( now );
    });
    </script>
    <?php
}, 10, 3 );

現在我們有形式,我們需要在提交時儲存資料,很容易使用"edited_{$taxonomy}"鉤子:

function save_inline_description( $term_id ) {
    global $the_target_tax;
    $tax = get_taxonomy( $the_target_tax );
    if (
        current_filter() === "edited_{$the_target_tax}"
        && current_user_can( $tax->cap->edit_terms )
    ) {
        $description = filter_input( INPUT_POST, 'description', FILTER_SANITIZE_STRING );
        // removing action to avoid recursion
        remove_action( current_filter(), __FUNCTION__ );
        wp_update_term( $term_id, $the_target_tax, array( 'description' => $description ) );
    }
}
add_action( "edited_{$the_target_tax}", 'save_inline_description' );

程式碼被快速測試,似乎工作,請注意它需要 PHP 5.3+。

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。