メディアのカテゴライズ(後編)

URLが404ですが別にNot Foundな訳ではありません。

とくだらない前置きは置いておいて。
メディアのカテゴライズ(前編)では、メディアのタクソノミー管理画面をメニューに加える方法を紹介いたしました。

後編は、メディアの編集画面でタクソノミー登録方法のユーザビリティーを劇的に向上させる方法をドドンと紹介します。

デフォルトでは、メディアに対してタクソノミーを適用した場合、ヒエラルキーの有無にかかわらず、下記のようなカンマ区切りのテキスト入力方式になります。

テキスト入力の場合は、新しい分類を入力フォームから追加することが利点ですが、既存の分類を簡単にミスなく選択するには少々難があり、変換ミスなどで容易に異なる分類になってしまうことになります。

これを、テキスト入力ではなく、投稿画面のカテゴリー選択のようにチェックボックスにするには、テーマのfunctions.phpに下記 CODE 1 を追加します。

■ CODE 1

function replace_attachement_taxonomy_input_to_check( $form_fields, $post ) {
	if ( $form_fields ) {
		foreach ( $form_fields as $taxonomy => $obj ) {
			if ( isset( $obj['hierarchical'] ) && $obj['hierarchical'] ) {
				$terms = get_terms( $taxonomy, array( 'get' => 'all' ) );
				$taxonomy_tree = array();
				$branches = array();
				$term_id_arr = array();

				foreach( $terms as $term ) {
					$term_id_arr[$term->term_id] = $term;
					if ( $term->parent == 0 ) {
						$taxonomy_tree[$term->term_id] = array();
					} else {
						$branches[$term->parent][$term->term_id] = array();
					}
				}

				if ( count( $branches ) ) {
					foreach( $branches as $foundation => $branch ) {
						foreach( $branches as $branche_key => $val ) {
							if ( array_key_exists( $foundation, $val ) ) {
								$branches[$branche_key][$foundation] = &$branches[$foundation];
								break 1;
							}
						}
					}
				
					foreach ( $branches as $foundation => $branch ) {
						if ( isset( $taxonomy_tree[$foundation] ) ) {
							$taxonomy_tree[$foundation] = $branch;
						}
					}
				}

				$html = walker_media_taxonomy_html( $post->ID, $taxonomy, $term_id_arr, $taxonomy_tree );
				$form_fields[$taxonomy]['input'] = 'checkbox';
				$form_fields[$taxonomy]['checkbox'] = $html;
			}
		}
	}
	return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'replace_attachement_taxonomy_input_to_check', 100, 2 );

function walker_media_taxonomy_html( $post_id, $taxonomy,  $term_id_arr, $taxonomy_tree, $html = '', $cnt = 0 ) {
	
	foreach ( $taxonomy_tree as $term_id => $arr ) {
		$checked = is_object_in_term( $post_id, $taxonomy, $term_id ) ? ' checked="checked"' : '';
		$html .= str_repeat( '—', count( get_ancestors( $term_id, $taxonomy ) ) );
		$html .= ' <input type="checkbox" id="attachments[' . $post_id . '][' . $taxonomy . ']-' . $cnt . '" name="attachments[' . $post_id . '][' . $taxonomy . '][]" value="' . $term_id_arr[$term_id]->name . '"' . $checked . ' /><label for="attachments[' . $post_id . '][' . $taxonomy . ']-' . $cnt . '">' . $term_id_arr[$term_id]->name . "</label><br />\n";
		$cnt++;
		if ( count( $arr ) ) {
			$html = walker_media_taxonomy_html( $post_id, $taxonomy, $term_id_arr, $arr, $html, &$cnt );
		}
	}
	return $html;
}


function join_media_taxonomy_datas() {
	global $wp_taxonomies;

	if ( $_POST['action'] != 'editattachment' ) { return; }
	$attachment_id = (int)$_POST['attachment_id'];
	$media_taxonomies = array();
	if ( $wp_taxonomies ) {
		foreach ( $wp_taxonomies as $key => $obj ) {
			if ( count( $obj->object_type ) == 1 && $obj->object_type[0] == 'attachment' ) {
				$media_taxonomies[$key] = $obj;
			}
		}
	}

	if ( $media_taxonomies ) {
		foreach ( $media_taxonomies as $key => $media_taxonomy ) {
			if ( isset( $_POST['attachments'][$attachment_id][$key] ) ) {
				if ( is_array( $_POST['attachments'][$attachment_id][$key] ) ) {
					$_POST['attachments'][$attachment_id][$key] = implode( ', ', $_POST['attachments'][$attachment_id][$key] );
				}
			} else {
					$_POST['attachments'][$attachment_id][$key] = '';
			}
		}
	}
	
}
add_action( 'load-media.php', 'join_media_taxonomy_datas' );

すると、ご覧の通り

チェックボックスの選択式になって、入力が簡単になりますし、見た目での階層構造も分かりやすくなりますね。

「メディアのカテゴライズ(後編)」への1件のフィードバック

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です