Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

WordPress Snippet

ワードプレスをカスタマイズしよう

カテゴリーへのフィールドの追加

WordPress 4.4 クリフォードの紹介ビデオには、以下のようなシーンがあります。

category-icon

さて、このような機能はコアに実装されていないのですが、カテゴリーカラーみたいなのは、時々使われる機能で「どうやったらこういう風にできるんだろう」と思っている人も、少なからずいるのではないかと思います。

従来だと、オプションフィールドを追加したりして工夫していたわけですが、add_term_meta()等新しい関数が使えるようになっており、新しいやり方で

こんな工夫にチャレンジしたい人のためと、備忘を兼ねて、、、


WordPress4.4で、カテゴリー等でカスタムメタデータを扱うために新しい関数が追加されました。

タームメタ

タームは投稿のようなメタデータをサポートするようになりました。詳細は add_term_meta()get_term_meta()update_term_meta() を参照してください。

対応したコードを書いている人がいましたのでリンクします。

Need a simple but complete example of adding metabox to taxonomy

<?php
// source: http://wordpress.stackexchange.com/questions/211703/need-a-simple-but-complete-example-of-adding-metabox-to-taxonomy
// code authored by jgraup - http://wordpress.stackexchange.com/users/84219/jgraup
// REGISTER TERM META
add_action( 'init', '___register_term_meta_text' );
function ___register_term_meta_text() {
register_meta( 'term', '__term_meta_text', '___sanitize_term_meta_text' );
}
// SANITIZE DATA
function ___sanitize_term_meta_text ( $value ) {
return sanitize_text_field ($value);
}
// GETTER (will be sanitized)
function ___get_term_meta_text( $term_id ) {
$value = get_term_meta( $term_id, '__term_meta_text', true );
$value = ___sanitize_term_meta_text( $value );
return $value;
}
// ADD FIELD TO CATEGORY TERM PAGE
add_action( 'category_add_form_fields', '___add_form_field_term_meta_text' );
function ___add_form_field_term_meta_text() { ?>
<?php wp_nonce_field( basename( __FILE__ ), 'term_meta_text_nonce' ); ?>
<div class="form-field term-meta-text-wrap">
<label for="term-meta-text"><?php _e( 'TERM META TEXT', 'text_domain' ); ?></label>
<input type="text" name="term_meta_text" id="term-meta-text" value="" class="term-meta-text-field" />
</div>
<?php }
// ADD FIELD TO CATEGORY EDIT PAGE
add_action( 'category_edit_form_fields', '___edit_form_field_term_meta_text' );
function ___edit_form_field_term_meta_text( $term ) {
$value = ___get_term_meta_text( $term->term_id );
if ( ! $value )
$value = ""; ?>
<tr class="form-field term-meta-text-wrap">
<th scope="row"><label for="term-meta-text"><?php _e( 'TERM META TEXT', 'text_domain' ); ?></label></th>
<td>
<?php wp_nonce_field( basename( __FILE__ ), 'term_meta_text_nonce' ); ?>
<input type="text" name="term_meta_text" id="term-meta-text" value="<?php echo esc_attr( $value ); ?>" class="term-meta-text-field" />
</td>
</tr>
<?php }
// SAVE TERM META (on term edit & create)
add_action( 'edit_category', '___save_term_meta_text' );
add_action( 'create_category', '___save_term_meta_text' );
function ___save_term_meta_text( $term_id ) {
// verify the nonce --- remove if you don't care
if ( ! isset( $_POST['term_meta_text_nonce'] ) || ! wp_verify_nonce( $_POST['term_meta_text_nonce'], basename( __FILE__ ) ) )
return;
$old_value = ___get_term_meta_text( $term_id );
$new_value = isset( $_POST['term_meta_text'] ) ? ___sanitize_term_meta_text ( $_POST['term_meta_text'] ) : '';
if ( $old_value && '' === $new_value )
delete_term_meta( $term_id, '__term_meta_text' );
else if ( $old_value !== $new_value )
update_term_meta( $term_id, '__term_meta_text', $new_value );
}
// MODIFY COLUMNS (add our meta to the list)
add_filter( 'manage_edit-category_columns', '___edit_term_columns' );
function ___edit_term_columns( $columns ) {
$columns['__term_meta_text'] = __( 'TERM META TEXT', 'text_domain' );
return $columns;
}
// RENDER COLUMNS (render the meta data on a column)
add_filter( 'manage_category_custom_column', '___manage_term_custom_column', 10, 3 );
function ___manage_term_custom_column( $out, $column, $term_id ) {
if ( '__term_meta_text' === $column ) {
$value = ___get_term_meta_text( $term_id );
if ( ! $value )
$value = '';
$out = sprintf( '<span class="term-meta-text-block" style="" >%s</div>', esc_attr( $value ) );
}
return $out;
}
// ADD JAVASCRIPT & STYLES TO COLUMNS
add_action( 'admin_enqueue_scripts', '___admin_enqueue_scripts' );
function ___admin_enqueue_scripts( $hook_suffix ) {
if ( 'edit-tags.php' !== $hook_suffix || 'category' !== get_current_screen()->taxonomy )
return;
// ADD YOUR SUPPORTING CSS / JS FILES HERE
// wp_enqueue_style( 'wp-color-picker' );
// wp_enqueue_script( 'wp-color-picker' );
add_action( 'admin_head', '___meta_term_text_print_styles' );
add_action( 'admin_footer', '___meta_term_text_print_scripts' );
}
// PRINT OUR CUSTOM STYLES
function ___meta_term_text_print_styles() { ?>
<style type="text/css">
.column-__term_meta_text { background-color:rgb(249, 249, 249); border: 1px solid lightgray;}
.column-__term_meta_text .term-meta-text-block { display: inline-block; color:darkturquoise; }
</style>
<?php }
// PRINT OUR CUSTOM SCRIPTS
function ___meta_term_text_print_scripts() { ?>
<script type="text/javascript">
jQuery( document ).ready( function( $ ) {
$input_field = $( '.term-meta-text-field' );
// console.log($input_field); // your input field
} );
</script>
<?php }

関連

Introduction to WordPress term meta

[emulsion_relate_posts]