メタボックス 削除・登録
デフォルトのメタボックスを削除して、改良版等を登録します。
・開催日メタボックス登録
・カスタムフィールドメタボックス(簡易版)登録
・作成者メタボックス削除
・作成者メタボックス(改良版)登録
・更新日操作メタボックス登録
・カテゴリメタボックス削除
・カテゴリメタボックス(HK版)登録
開催日メタボックスは、カテゴリタイプが、'date'の場合に登録します。
作成者メタボックス(改良版)の id は、削除したメタボックスと同じにしています。また、削除したメタボックスの priority が core なので、登録するメタボックスの priority を core にすると追加できません。このため、priority を high にしています。
カテゴリメタボックス(HK版)も同様です。
コード
// -------------------------------- // メタボックス 削除・登録 // -------------------------------- function hk_functions_post_meta_boxes( $post_type, $context ) { if (( $post_type != 'post' ) && ( $post_type != 'page' )) return; if ( $context != 'normal' ) return; // 開催日メタボックス登録 // 通常の新規投稿を除外 'post'以外を除外 if (( isset( $_GET['post'] ) ) && ( $post_type == 'post' )) { $post_id = $_GET['post']; $post = get_post( $post_id ); // 序文、テンプレートを除外 $post_title = $post->post_title; if (( $post_title != '序文' ) && ( $post_title != 'テンプレート' )) { // カテゴリ $cats = get_the_category( $post_id ); if ( $cats ) { $cat_id = $cats[0]->cat_ID; // カテゴリタイプ $cat_type = hk_func_get_cat_type( $cat_id ); if ( $cat_type == 'date' ) add_meta_box( 'hk_event_date', '年月日・ファイル', 'hk_functions_event_date_meta_box', 'post', 'normal', 'high' ); } } } // カスタムフィールドメタボックス(簡易版)登録 if ( current_user_can('administrator') ) add_meta_box( 'hk_postcustom', 'カスタムフィールド(簡易版)', 'hk_functions_postcustom_meta_box', null, 'normal', 'high' ); // 作成者メタボックス削除 remove_meta_box( 'authordiv', 'post', 'normal' ); remove_meta_box( 'authordiv', 'page', 'normal' ); // 作成者メタボックス(改良版)登録 if ( current_user_can('administrator') ) add_meta_box( 'authordiv', '作成者', 'hk_functions_post_author_meta_box', null, 'normal', 'high' ); // 更新日操作メタボックス登録 if ( current_user_can('administrator') ) add_meta_box( 'hk_postmodified', '更新日操作', 'hk_functions_post_modified_meta_box', null, 'side', 'high' ); // カテゴリメタボックス削除 remove_meta_box( 'categorydiv', 'post', 'side'); // カテゴリメタボックス(HK版)登録 if ( current_user_can('administrator') ) add_meta_box( 'categorydiv', 'カテゴリ', 'hk_functions_post_cat_meta_box', 'post', 'side', 'high' ); return; } add_action( 'do_meta_boxes', 'hk_functions_post_meta_boxes', 10, 2 );
開催日メタボックス
各種イベント投稿を作成する場合、開催日、掲載終了日、案内パンフレットを入力すると応用範囲が広がります。開催日はトップページのお知らせや、イベントカレンダーに利用できます(お知らせは、テーマのカスタマイズが必要です)。掲載終了日は、トップページへのお知らせの掲載終了や、イベント投稿に「終了しました」の文字を挿入するのに利用できます(文字の挿入は、テーマのカスタマイズが必要です)。案内パンフレットは、イベント投稿の最後にリンクを挿入しダウンロードできるようにしています。
開催日、終了日、パンフレットのファイル名称は、カスタムフィールドに次の名称で保存しています。
・開催日:'01_開催日'
・終了日:'01_終了日'
・ファイル名称:'01_添付ファイル'
コード
// === メタボックス =============== // 開催日メタボックス // 開催日入力欄を表示 // 終了日入力欄を表示 // 添付ファイル入力欄を表示 // (カテゴリタイプ date) // ================================ function hk_functions_event_date_meta_box() { global $post; // post_id if ( $post ) $post_id = $post->ID; else $post_id = 0; // 本日の年月日 date_default_timezone_set('Asia/Tokyo'); $this_year = (int) date( 'Y' ); // 表示開始 echo "\n\n".'<div class="hk_plugin_admin"><div class="hk_event">'."\n"; echo " <br />\n"; // 開催日入力欄を表示 // カスタムフィールドデータ $ymd = get_post_meta( $post_id, '01_開催日', true ); if ( $ymd != '' ) { list( $year, $month, $day ) = explode( '-', $ymd ); $year = (int) $year; $month = (int) $month; $day = (int) $day; } else { $year = 0; $month = 0; $day = 0; } // 開催日入力フォーム echo '<p>開催日:イベントカレンダーに表示する場合に入力<br />'."\n"; // 年 echo '<select size="1" name="hk_event_year">'."\n"; hk_functions_event_date_ymd_form( $year, 0, '', '選択'); for ( $i=$this_year-1; $i<=$this_year+1; $i++ ) hk_functions_event_date_ymd_form( $year, $i, $i, $i); echo '</select>年 '."\n"; // 月 echo '<select size="1" name="hk_event_month">'."\n"; hk_functions_event_date_ymd_form( $month, 0, '', '選択'); for ( $i=1; $i<=12; $i++ ) { $ii = sprintf( "%02d", $i ); hk_functions_event_date_ymd_form( $month, $i, $ii, $ii); } echo '</select>月 '."\n"; // 日 echo '<select size="1" name="hk_event_day">'."\n"; hk_functions_event_date_ymd_form( $day, 0, '', '選択'); for ( $i=1; $i<=31; $i++ ) { $ii = sprintf( "%02d", $i ); hk_functions_event_date_ymd_form( $day, $i, $ii, $ii); } echo '</select>日</p>'."\n"; // 終了日入力欄を表示 // カスタムフィールドデータ $ymd = get_post_meta( $post_id, '01_終了日', true ); if ( $ymd != '' ) { list( $year, $month, $day ) = explode( '-', $ymd ); $year = (int) $year; $month = (int) $month; $day = (int) $day; } else { $year = 0; $month = 0; $day = 0; } // 終了日入力フォーム echo '<p>終了日:開催日と同じ場合は入力不要<br />'."\n"; // 年 echo '<select size="1" name="hk_event_end_year">'."\n"; hk_functions_event_date_ymd_form( $year, 0, '', '選択'); for ( $i=$this_year-1; $i<=$this_year+1; $i++ ) hk_functions_event_date_ymd_form( $year, $i, $i, $i); echo '</select>年 '."\n"; // 月 echo '<select size="1" name="hk_event_end_month">'."\n"; hk_functions_event_date_ymd_form( $month, 0, '', '選択'); for ( $i=1; $i<=12; $i++ ) { $ii = sprintf( "%02d", $i ); hk_functions_event_date_ymd_form( $month, $i, $ii, $ii); } echo '</select>月 '."\n"; // 日 echo '<select size="1" name="hk_event_end_day">'."\n"; hk_functions_event_date_ymd_form( $day, 0, '', '選択'); for ( $i=1; $i<=31; $i++ ) { $ii = sprintf( "%02d", $i ); hk_functions_event_date_ymd_form( $day, $i, $ii, $ii); } echo '</select>日</p>'."\n"; // 添付ファイル入力欄を表示 echo ' <p>添付ファイルアップロード<br />'."\n"; $file = get_post_meta( $post_id, '01_添付ファイル', true ); $file = htmlspecialchars( $file ); if ( $file != '' ) { echo ' 既アップロードファイル名:'.$file."<br />\n"; echo ' <input type="checkbox" name="hk_event_file_del" value="yes" /> 既アップロードファイル削除'."<br /><br />\n"; echo ' 更新する場合はファイルを指定してください。'."<br />\n"; } echo '<input type="file" name="hk_event_file" size="80" class="hk_width100" /></p>'."\n"; echo '</div></div>'."\n"; return; } // --- サブルーチン --------------- // option フォームを表示 // -------------------------------- function hk_functions_event_date_ymd_form( $var, $case, $value, $label) { if ( $var == $case ) $selected = ' selected="selected"'; else $selected = ''; echo ' <option value="'.$value.'"'.$selected.'>'.$label."</option>\n"; return; }
カスタムフィールドメタボックス(簡易版)
カスタムフィールドの名前と値の表示だけを行います。
コード
// === メタボックス =============== // カスタムフィールドメタボックス(簡易版) // 名前と値の表示のみ // ================================ function hk_functions_postcustom_meta_box( $post ) { $metadata = has_meta( $post->ID ); foreach ( $metadata as $key => $meta ) { if ( is_protected_meta( $meta['meta_key'], 'post' ) ) unset( $metadata[ $key ] ); else if ( is_serialized( $meta['meta_value'] ) ) { if ( is_serialized_string( $meta['meta_value'] ) ) $meta['meta_value'] = maybe_unserialize( $meta['meta_value'] ); else unset( $metadata[ $key ] ); } } unset( $key ); unset( $meta ); if ( ! $metadata ) { echo "<p>ありません</p>\n"; return; } echo '<table style="width:auto;border-style:none;">'."\n"; echo ' <thead>'."\n"; echo ' <tr>'."\n"; echo ' <th style="border-bottom:1px solid;">名前</th>'."\n"; echo ' <th style="border-bottom:1px solid;">値</th>'."\n"; echo ' </tr>'."\n"; echo ' </thead>'."\n"; echo ' <tbody>'."\n"; foreach ( $metadata as $meta ) { $meta_key = htmlspecialchars( $meta['meta_key'] ); $meta_value = htmlspecialchars( $meta['meta_value'] ); echo ' <tr>'."\n"; echo ' <td style="padding: 6px 12px;">'.$meta_key. "</td>\n"; echo ' <td style="padding: 6px 12px;">'.$meta_value."</td>\n"; echo ' </tr>'."\n"; } unset( $meta ); echo ' </tbody>'."\n"; echo '</table>'."\n"; return; }
作成者メタボックス(改良版)
作成者を氏名で表示します。
関数 hk_func_author_select_sub は、“共通関数”に記載します。
コード
// === メタボックス =============== // 作成者メタボックス(改良版) // ================================ function hk_functions_post_author_meta_box( $post ) { // 作成者ID global $user_ID; if ( empty( $post->ID ) ) $selected = $user_ID; else $selected = $post->post_author; // 表示開始 echo '<label class="screen-reader-text" for="post_author_override">作成者</label>'."\n"; // SELECT BOX 表示 echo '<select name="post_author_override" id="post_author_override" class="">'."\n"; hk_func_author_select_sub( $selected ); echo '</select>'."\n"; return; }
更新日操作メタボックス
投稿を修正する際、特に非本質的な修正の場合、更新日を変更したくない場合があります。
コード
// === メタボックス =============== // 更新日操作メタボックス // ================================ function hk_functions_post_modified_meta_box( $post ) { echo '<p>更新日:'.$post->post_modified."</p>\n"; echo '<p><input type="checkbox" name="hk_post_modified_1" value="yes" /> 作成日と同じにする'."</p>\n"; echo '<p><input type="checkbox" name="hk_post_modified_2" value="yes" /> 更新日を変更しない'."</p>\n"; return; }
カテゴリメタボックス(HK版)
投稿のカテゴリを複数許容すると、パンくずや、投稿ナビゲーションに支障が出ます。また、テンプレートの自動読み込みができないなど使い勝手の良いシステムが作りにくくなります。このため、投稿のカテゴリをひとつに限定します。
コード
// === メタボックス =============== // カテゴリメタボックス(HK版) // ================================ function hk_functions_post_cat_meta_box( $post ) { // カテゴリのデータ配列初期化 $id_data = array(); $id_data[ 0 ] = array( 'title' => '', 'parent' => '', 'child' => array(), ); // カテゴリ一覧取得 ID順 $args = array( 'orderby' => 'id', 'order' => 'ASC', 'hide_empty' => 0, ); $categories = get_categories( $args ); // 各カテゴリの配列初期設定 foreach( $categories as $category ) { $id = $category->cat_ID; // ID $title = $category->cat_name; // タイトル // 親カテゴリID $parent = $category->parent; if ( $parent == '' ) $parent = 0; // データセット $id_data[ $id ] = array( 'title' => $title, 'parent' => $parent, 'child' => array(), ); } unset( $category ); // 子データ配列作成 foreach( $id_data as $id => $data ) { if ( $id == 0 ) continue; $parent = $data['parent']; $id_data[ $parent ]['child'][] = $id; } unset( $id ); unset( $data ); // カテゴリID $selected = get_option('default_category'); if ( $post->ID ) { $cats = get_the_category( $post->ID ); if ( $cats ) $selected = $cats[0]->cat_ID; } // SELECT BOX 表示 echo '<select name="hk_cat_div">'."\n"; $level = 0; $children = $id_data[ 0 ]['child']; if ( ! empty( $children ) ) hk_functions_post_cat_meta_box_recursive( $children, $level, $id_data, $selected ); echo '</select>'."\n"; return; } // --- サブルーチン --------------- // カテゴリメタボックス再帰処理 // -------------------------------- function hk_functions_post_cat_meta_box_recursive( $children, &$level, $id_data, $selected ) { foreach( $children as $id ) { $html = ''; for ( $i=1; $i<=$level; $i++ ) $html .= '・'; $title = $id_data[ $id ]['title']; $title = htmlspecialchars( $title ); $html .= $title; if ( $id == $selected ) echo ' <option selected="selected" value="'.$id.'">'.$html.'</option>'."\n"; else echo ' <option value="'.$id.'">'.$html.'</option>'."\n"; // 孫の再帰処理 $grandchildren = $id_data[ $id ]['child']; if ( ! empty( $grandchildren ) ) { ++$level; $map_order = hk_functions_post_cat_meta_box_recursive( $grandchildren, $level, $id_data, $selected ); --$level; } } unset( $id ); return; }
このプログラムをお使いになる場合は、お使いになる方の自己責任でお願いします。