web-dev-qa-db-ja.com

分類法「クイック編集」に「説明」を追加

これはかなり簡単に思えますが、分類の説明フィールドをクイック編集ボックスに追加する方法に関する明確なガイドは見つかりません。

カスタムクイック編集ボックスへのフィールドの追加に関するいくつかのチュートリアルに従いましたが、コアフィールド(名前、説明、およびスラッグ)は、分類クイッククイック編集ボックスにカスタムフィールドを追加するのと同じガイドラインには従いません。

それも可能ですか?そうでなければ - 私は最善の代替策は別の 'Description'メタフィールドを作成することであり、そのようにしてそれを分類法クイック編集ボックスに追加することです。

7
Roc

通常、クイック編集フィールドにフィールドを追加するには、コア列が明示的に除外されるため、カスタム列に対してのみ起動される'quick_edit_custom_box'アクションフックを使用する必要があります( code を参照)。

カスタム列を追加すると、それはリストテーブルに表示されますが、列の説明が既に存在するため、意味がありません。

しかし、2つのトリックを使って 見えない 列を追加することができます。

  1. ラベルを空の文字列に設定する(これにより、[画面オプション]設定には表示されません)
  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'フックを介して追加のフィールドを配置するために使用できます。

しかし、このフックはフィールドに現在の説明を事前に入力するために現在値を渡すことはしませんが、それを行うには数行の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以降が必要です。

Add description to taxonomy quick edit

15
gmazzap

「category」パラメータを自分のカスタム分類名に変更しないとコードを機能させることができなかったことを指摘したいだけでした。

例えば:

wp_update_term( $term_id, 'category', array( 'description' => $description ) );

に置き換えられました:

wp_update_term( $term_id, 'my_custom_tax_name', array( 'description' => $description ) );

乾杯!

0
Leo