問題描述

編輯:WordPress v4.4 以上的重要說明

這個問題使用了一個很好的解決方法,因為在 WordPress 的舊版本中缺少真正的術語 meta,但自從 WordPress v4.4,real term meta has been introduced 釋出以來,絕對是現在的方式。

Here’s a post that looks like a good starting point for term meta in WordPress 4.4+

我要離開這個問題,因為它可能仍然對於處理較舊程式碼的人有用,但是想要新增上面的註釋,以便開始從新的後設資料實現開始的開發人員將處於正確的軌道。

編輯結束


我已經能夠利用 @Bainternet 的 this 超級教程來新增自定義欄位進行分類。我已經調整了標記,並新增了單獨的回撥,將欄位新增到 「新增新類別」 管理頁面。我的程式碼是在這個問題的結尾。

我已經陷入困境了自定義欄位輸入顯示在新增新類別頁面上,但該欄位未儲存。

我注意到新增新的分類頁面使用 ajax,這可能是問題的一部分。我嘗試停用 JS,但仍遇到同樣的問題。我一直在挖掘核心尋找一個鉤子,但找不到一個。網上有各種各樣的教程,但是他們似乎依靠分類編輯介面進行出價。

關於如何使這項工作有任何想法?

/**
 * Add extra fields to custom taxonomy edit and add form callback functions
 */
// Edit taxonomy page
function extra_edit_tax_fields($tag) {
    // Check for existing taxonomy meta for term ID.
    $t_id = $tag->term_id;
    $term_meta = get_option( "taxonomy_$t_id" ); ?>
    <tr class="form-field">
    <th scope="row" valign="top"><label for="cat_Image_url"><?php _e( 'Category Navigation Image URL' ); ?></label></th>
        <td>
            <input type="text" name="term_meta[img]" id="term_meta[img]" value="<?php echo esc_attr( $term_meta['img'] ) ? esc_attr( $term_meta['img'] ) : ''; ?>">
            <p class="description"><?php _e( 'Enter the full URL to the navigation image used for this category.' ); ?></p>
        </td>
    </tr>
<?php
}
add_action( 'category_edit_form_fields', 'extra_edit_tax_fields', 10, 2 );

// Add taxonomy page
function extra_add_tax_fields( $tag ) {
    // Check for existing taxonomy meta for term ID.
    $t_id = $tag->term_id;
    $term_meta = get_option( "taxonomy_$t_id" ); ?>
    <div class="form-field">
        <label for="cat_Image_url"><?php _e( 'Category Navigation Image URL' ); ?></label>
        <input type="text" name="term_meta[img]" id="term_meta[img]" value="<?php echo esc_attr( $term_meta['img'] ) ? esc_attr( $term_meta['img'] ) : ''; ?>">
        <p class="description"><?php _e( 'Enter the full URL to the navigation image used for this category.' ); ?></p>
    </div>
<?php
}
add_action( 'category_add_form_fields', 'extra_add_tax_fields', 10, 2 );

// Save extra taxonomy fields callback function.
function save_extra_taxonomy_fields( $term_id ) {
    if ( isset( $_POST['term_meta'] ) ) {
        $t_id = $term_id;
        $term_meta = get_option( "taxonomy_$t_id" );
        $cat_keys = array_keys( $_POST['term_meta'] );
        foreach ( $cat_keys as $key ) {
            if ( isset ( $_POST['term_meta'][$key] ) ) {
                $term_meta[$key] = $_POST['term_meta'][$key];
            }
        }
        // Save the option array.
        update_option( "taxonomy_$t_id", $term_meta );
    }
}
add_action( 'edited_category', 'save_extra_taxonomy_fields', 10, 2 );
//add_action( '...Can't find hook to enable saving custom fields when adding a new term

最佳解決方案

解決了!

add_action( 'create_category', 'save_extra_taxonomy_fields', 10, 2 );

支援 Category Meta plugin 。我從 WP repo 下載了各種類別的 meta 外掛,並且可以在新增新螢幕上新增後設資料。我挖掘了程式碼,發現了難以捉摸的 create_ {term} 鉤子。

次佳解決方案

我認為你引用的教程很棒,我已經基於這個程式碼了。它將分類 menu_category 的後設資料儲存在該分類法的新增和編輯表單上,並將其顯示在編輯表單中。選項表項將為術語 ID 24 的 taxomomy_24_metas

add_action('menu_category_edit_form_fields','menu_category_edit_form_fields');
add_action('menu_category_add_form_fields','menu_category_edit_form_fields');
add_action('edited_menu_category', 'menu_category_save_form_fields', 10, 2);
add_action('created_menu_category', 'menu_category_save_form_fields', 10, 2);

function menu_category_save_form_fields($term_id) {
    $meta_name = 'order';
    if ( isset( $_POST[$meta_name] ) ) {
        $meta_value = $_POST[$meta_name];
        // This is an associative array with keys and values:
        // $term_metas = Array($meta_name => $meta_value, ...)
        $term_metas = get_option("taxonomy_{$term_id}_metas");
        if (!is_array($term_metas)) {
            $term_metas = Array();
        }
        // Save the meta value
        $term_metas[$meta_name] = $meta_value;
        update_option( "taxonomy_{$term_id}_metas", $term_metas );
    }
}

function menu_category_edit_form_fields ($term_obj) {
    // Read in the order from the options db
    $term_id = $term_obj->term_id;
    $term_metas = get_option("taxonomy_{$term_id}_metas");
    if ( isset($term_metas['order']) ) {
        $order = $term_metas['order'];
    } else {
        $order = '0';
    }
?>
    <tr class="form-field">
            <th valign="top" scope="row">
                <label for="order"><?php _e('Category Order', ''); ?></label>
            </th>
            <td>
                <input type="text" id="order" name="order" value="<?php echo $order; ?>"/>
            </td>
        </tr>
<?php
}

參考文獻

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