メディアの追加画面でタームをチェックボックスで選択したい
-
先日こちらで助けていただき、無事にメディアにタクソノミーを追加できました。
メディアの追加画面でタームを選択するメタボックス?をチェックボックスにしたく、同時進行で奮闘しておりました。こちらのコードを利用させていただいているのですが、チェックボックスは創れるものの、選択したタームが反映されません。
チェックをして、挿入ボタンをおすと、画像に関連付けられたはずのタームが消えてしまっています。
色々と試してみているのですが、保存などに関してはまだ知識が浅くお手上げ状態です。http://www.warna.info/archives/404/
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' );4行目はhierarchicalの有る無しにかかわらず、チェックボックスを使いたかったので、コメントアウトしています。
どなたかお分かりになる方がおりましたら、お教えいただけますか。
宜しくお願い致します。
トピック「メディアの追加画面でタームをチェックボックスで選択したい」には新たに返信することはできません。