ユーザー毎に表示するアドミニメニューを変えます。
・コアメニューのチェック
・メニュー並び替え
・カテゴリ別投稿一覧(簡易版)のメニュー登録
・個別ページのメニュー登録
【独自用語の説明】
・コアメニュー:WordPress のメニュー
・一般メニュー:コアメニュー、カテゴリメニュー、ページメニュー以外のメニュー
WordPressのバージョンアップでコアメニューに変更がある場合に備え、コアメニューチェックを行っています。変更がある場合には、何もしません。
セパレータのデータ取得は、セパレータの追加用です。セパレータを5個まで追加できるようにしています。
処理しやすくするため、メニューにデータを追加しています。
カテゴリ別投稿一覧(簡易版)には次の子メニューを登録しています。
・新規作成
・ゴミ箱
・序文
・テンプレート
フック'admin_menu'のフィルターの優先順位は、デフォルトの最大優先順位が 101 のため、それ以降にしています。
parent_file の設定を行わないとアドミニメニューの表示に支障が出ます。
関数 hk_func_hex2spchar は、“共通関数”に記載します。
下記関数は、“カテゴリ別投稿一覧(簡易版)”をご覧ください。
hk_easy_get_link_cat_post_list
hk_easy_get_link_page
コード
$hk_core_menu_check = true; // コアメニューチェック結果 // ================================ // コアメニューチェック // アドミニメニュー並び替え // カテゴリ・ページ登録 // ================================ function hk_admin_menu_disp() { global $menu; global $hk_core_menu_check; // コアメニュー $wp_core_menu = array( 2 => 'ダッシュボード', 4 => '', 5 => '投稿', 10 => 'メディア', 15 => 'リンク', 20 => '固定ページ', 25 => 'コメント', 59 => '', 60 => '外観', 65 => 'プラグイン', 70 => 'ユーザー', 75 => 'ツール', 80 => '設定', 99 => '', ); // コアメニューチェック データ追加 $hk_core_menu_check = false; $base_order_org = array(); $base_order_new = array(); foreach( $wp_core_menu as $order => $title ) { if ( ! isset( $menu[ $order ] ) ) return; $title_1 = $menu[ $order ][ 0 ]; $pos = strpos( $title_1, ' ' ); if ( $pos !== false ) $title_1 = substr( $title_1, 0, $pos ); if ( $order != 70 ) { if ( $title_1 != $title ) return; } else { if (( $title_1 != $title ) && ( $title_1 != 'プロフィール' )) return; } $id_str = 'core_'.$order; $base_order_org[ $id_str ] = $order; $base_order_new[ $id_str ] = $order; $menu[ $order ]['hk_group'] = 'core'; $menu[ $order ]['hk_order_org'] = $order; $menu[ $order ]['hk_title'] = $title; } unset( $order ); unset( $title ); $hk_core_menu_check = true; // 一般メニュー取得 データ追加 foreach( $menu as $order => $each_menu ) { if ( ! isset( $each_menu['hk_group'] ) ) { $title = $each_menu[ 0 ]; $id_str = 'other_'.$title; $base_order_org[ $id_str ] = $order; $base_order_new[ $id_str ] = $order; $menu[ $order ]['hk_group'] = 'other'; $menu[ $order ]['hk_order_org'] = $order; } } unset( $order ); unset( $each_menu ); // カテゴリ一覧取得 $cat_titles = array(); $args = array( 'orderby' => 'id', 'order' => 'ASC', 'hide_empty' => 0, ); $categories = get_categories( $args ); foreach( $categories as $category ) { $id = $category->cat_ID; // タイトル $cat_titles[ $id ] = $category->cat_name; } unset( $category ); // 固定ページ一覧取得 $page_titles = array(); $args = array( 'sort_column' => 'ID', 'sort_order' => 'ASC', 'hierarchical' => 0, 'post_status' => 'publish,draft', ); $pages = get_pages( $args ); foreach( $pages as $page ) { // パスワード保護の場合 スキップ $passwd = $page->post_password; if ( $passwd != '' ) continue; $id = $page->ID; // タイトル $title = $page->post_title; if ( $title == '' ) $title = '(無題)'; $page_titles[ $id ] = $title; } unset( $page ); // 順序データ読み出し $db_str = get_option( 'hk_admin_menu_order', '' ); if ( $db_str == '' ) return; $custom_menu_data = array(); $lines = explode( '&', $db_str ); foreach( $lines as $line ) { list( $id_str, $order ) = explode( ',', $line ); list( $group, $id ) = explode( '_', $id_str, 2 ); // コアメニュー if ( $group == 'core' ) $base_order_new[ $id_str ] = $order; // 一般メニュー else if ( $group == 'other' ) { // タイトル($id) 逆文字変換 $id = hk_func_hex2spchar( $id ); $id_str = 'other_'.$id; // 存在チェック if ( ! isset( $base_order_new[ $id_str ] ) ) continue; // 注意:order重複の可能性がある $base_order_new[ $id_str ] = $order; } // カテゴリ else if ( $group == 'cat' ) { if ( ! isset( $cat_titles[ $id ] ) ) continue; $custom_menu_data[ $id_str ] = $order; } // ページ else if ( $group == 'page' ) { if ( ! isset( $page_titles[ $id ] ) ) continue; $custom_menu_data[ $id_str ] = $order; } // セパレータ else if ( $group == 'sepa' ) { $custom_menu_data[ $id_str ] = $order; } else wp_die('ERROR:menu group'); } unset( $line ); // ソート asort( $base_order_new ); // order の再付番 $order_last = 0; foreach( $base_order_new as $id_str => &$order ) { if ( $order <= $order_last ) $order = $order_last + 1; $order_last = $order; } unset( $id_str ); unset( $order ); // 権限データ読み出し $current_user_data = array(); // user_id $user_id = get_current_user_id(); // DB読み込み $db_data = get_user_meta( $user_id, 'hk_admin_menu', true ); if ( $db_data == '' ) { if ( current_user_can('administrator') ) { $current_user_data['core'] = 'all'; $current_user_data['other'] = 'all'; $current_user_data['cat'] = 'all'; $current_user_data['page'] = 'all'; } else { $current_user_data['core'] = ''; $current_user_data['other'] = ''; $current_user_data['cat'] = ''; $current_user_data['page'] = ''; } } else { $lines = explode( '&', $db_data ); foreach( $lines as $line ) { list( $group, $value ) = explode( '=', $line ); if (( $value == 'all' ) || ( $value == '' )) $current_user_data[ $group ] = $value; else { $array_1 = explode( ',', $value ); // 一般 if ( $group == 'other' ) // 逆文字変換 $array_1 = array_map( 'hk_func_hex2spchar', $array_1 ); $current_user_data[ $group ] = $array_1; } } unset( $line ); } // メニュー表示・非表示 判定 サブ function hk_admin_menu_check( $ids, $id ) { if ( $ids == 'all' ) return true; if ( $ids == '' ) return false; if ( in_array( $id, $ids ) ) return true; return false; } // 新メニュー作成 $new_menu = array(); foreach( $base_order_new as $id_str => $order ) { $order_org = $base_order_org[ $id_str ]; $title = $menu[ $order_org ][0]; // ダッシュボード・セパレータ if (( $order_org == 2 ) || ( $title == '' )) $new_menu[ $order ] = $menu[ $order_org ]; // コア・一般 else { list( $group, $id ) = explode( '_', $id_str, 2 ); if ( $group == 'core' ) { if ( hk_admin_menu_check( $current_user_data['core'], $id ) ) $new_menu[ $order ] = $menu[ $order_org ]; } else { if ( hk_admin_menu_check( $current_user_data['other'], $id ) ) $new_menu[ $order ] = $menu[ $order_org ]; } } } unset( $id_str ); unset( $order ); // 新メニュー $menu = $new_menu; // プラグイン確認 if ( is_plugin_inactive('hk-easy/hk-easy.php') ) return; // メニューセパレータのデータ取得 $separator = $menu[4]; // カテゴリ・ページ登録 foreach( $custom_menu_data as $id_str => &$order ) { while ( isset( $menu[ $order ] ) ) $order++; list( $group, $id ) = explode( '_', $id_str, 2 ); // カテゴリ if ( $group == 'cat' ) { if ( ! hk_admin_menu_check( $current_user_data['cat'], $id ) ) continue; // 親メニューデータ $page_title = $cat_titles[ $id ]; $menu_title = $cat_titles[ $id ]; $capability = 'edit_posts'; $menu_slug = hk_easy_get_link_cat_post_list( $id ); $function = ''; $icon_url = 'dashicons-edit'; add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $order ); // メニューにデータ追加 $menu[ $order ]['hk_group'] = $group; $menu[ $order ]['hk_id'] = $id; // 子メニュー 共通データ $parent_slug = $menu_slug; $capability = 'edit_posts'; $function = ''; // 子メニュー 新規作成 $page_title = '新規作成'; $menu_title = '新規作成'; $menu_slug = hk_easy_get_link_cat_post_new( $id ); add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function ); // 子メニュー ゴミ箱 $page_title = 'ゴミ箱'; $menu_title = 'ゴミ箱'; $menu_slug = hk_easy_get_link_cat_post_trash_list( $id ); add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function ); // 子メニュー 序文 $page_title = '序文'; $menu_title = '序文'; $menu_slug = 'admin.php?action=hk_post_new_pref&hk_cat_id='.$id.'&hk_light_type=cat_post_list'; $capability = 'create_users'; add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function ); // 子メニュー テンプレート $page_title = 'テンプレート'; $menu_title = 'テンプレート'; $menu_slug = 'admin.php?action=hk_post_new_temp&hk_cat_id='.$id.'&hk_light_type=cat_post_list'; $capability = 'create_users'; add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function ); } // ページ else if ( $group == 'page' ) { if ( ! hk_admin_menu_check( $current_user_data['page'], $id ) ) continue; // 親メニューデータ $page_title = $page_titles[ $id ]; $menu_title = $page_titles[ $id ]; $capability = 'edit_pages'; $menu_slug = hk_easy_get_link_page( $id ); $function = ''; $icon_url = 'dashicons-edit'; add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $order ); // メニューにデータ追加 $menu[ $order ]['hk_group'] = $group; $menu[ $order ]['hk_id'] = $id; } // セパレータ else { $menu[ $order ] = $separator; $menu[ $order ]['hk_group'] = $group; $menu[ $order ]['hk_id'] = $id; } } unset( $id_str ); unset( $order ); return; } add_action( 'admin_menu', 'hk_admin_menu_disp', 200 ); // -------------------------------- // parent_file の設定 // -------------------------------- function hk_admin_menu_parent_file( $parent_file ) { // プラグイン確認 if ( is_plugin_inactive('hk-easy/hk-easy.php') ) return $parent_file; // 簡易版確認 if ( ! isset( $_REQUEST['hk_light_type'] ) ) return $parent_file; $light_type = $_REQUEST['hk_light_type']; // カテゴリ if ( $light_type == 'cat_post_list' ) { if ( isset( $_REQUEST['cat'] ) ) $cat_id = $_REQUEST['cat']; else if ( isset( $_REQUEST['hk_cat_id'] ) ) $cat_id = $_REQUEST['hk_cat_id']; else $cat_id = 0; if ( $cat_id != 0 ) $parent_file = hk_easy_get_link_cat_post_list( $cat_id ); } // ページ else if (( $light_type == 'edit_page' ) && ( isset( $_REQUEST['post'] ) )) { $post_id = $_REQUEST['post']; if ( $post_id != 0 ) $parent_file = hk_easy_get_link_page( $post_id ); } return $parent_file; } add_filter( 'parent_file', 'hk_admin_menu_parent_file' );
このプログラムをお使いになる場合は、お使いになる方の自己責任でお願いします。