• 赤色のリンクは、まだ日本語Codexに存在しないページ・画像です。英語版と併せてご覧ください。(詳細

このWikiはいつでも誰でも編集できます

関数リファレンス/add menu page

提供: WordPress Codex 日本語版
< 関数リファレンス
2015年5月16日 (土) 12:03時点におけるMiccweb (トーク | 投稿記録)による版 (ページ作成)

(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
移動先: 案内検索

このページ「関数リファレンス/add menu page」は一部未翻訳です。和訳や日本語情報を加筆してくださる協力者を求めています

説明

トップレベルメニューの追加。

具体的には、管理者メニューのサイドバーに新しいトップレベルのメニューのセクションを作成し、リンクされたメニューのページが要求されたときにページの内容を出力するための関数をコールバックしフックを登録します。 $hooknameを返します。

使い方

<?php add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position );

?>

パラメータ

$page_title
string) (必須) The text to be displayed in the title tags of the page when the menu is selected
初期値: なし
$menu_title
string) (必須) The on-screen name text for the menu
初期値: なし
$capability
string) (必須) The capability required for this menu to be displayed to the user. User levels are deprecated and should not be used here!
初期値: なし
$menu_slug
string) (必須) The slug name to refer to this menu by (should be unique for this menu). Prior to Version 3.0 this was called the file (or handle) parameter. If the function parameter is omitted, the menu_slug should be the PHP file that handles the display of the menu page content.
初期値: なし
$function
string) (optional) The function that displays the page content for the menu page.
初期値: None. Technically, the function parameter is optional, but if it is not supplied, then WordPress will assume that including the PHP file will generate the administration screen, without calling a function. Most plugin authors choose to put the page-generating code in a function within their main plugin file. In the event that the function parameter is specified, it is possible to use any string for the file parameter. This allows usage of pages such as ?page=my_super_plugin_page instead of ?page=my-super-plugin/admin-options.php.
The function must be referenced in one of two ways:
  1. if the function is a member of a class within the plugin it should be referenced as array( $this, 'function_name' )
  2. in all other cases, using the function name itself is sufficient
$icon_url
string) (optional) The icon for this menu.
初期値: empty string
  • If you have a custom image file, you can use the plugin_dir_url( __FILE__ ) function to get the URL of your plugin directory and then add the image filename to it. Icons should be 20 x 20 pixels or smaller.
  • (WP 3.8+) If 'dashicons-...', a Dashicon is shown from the collection at http://melchoyce.github.io/dashicons/. For example, the default "gear" symbol could be explicitly specified with 'dashicons-admin-generic'.
  • (WP 3.8+) If 'data:image/svg+xml;base64...', the specified SVG data image is used as a CSS background.
  • If 'none' (previously 'div'), the icon is replaced with an empty div you can style with CSS.
  • If '' (default), the "gear" Dashicon is shown (and menu-icon-generic is added to the CSS classes of the link).
$position
integer) (optional) The position in the menu order this menu should appear. By default, if this parameter is omitted, the menu will appear at the bottom of the menu structure. The higher the number, the lower its position in the menu. WARNING: if two menu items use the same position attribute, one of the items may be overwritten so that only one item displays! Risk of conflict can be reduced by using decimal instead of integer values, e.g. 63.3 instead of 63 (Note: Use quotes in code, IE '63.3').
初期値: bottom of menu structure
  • 2 - ダッシュボード
  • 4 - Separator
  • 5 - 投稿
  • 10 - メディア
  • 15 - リンク
  • 20 - 固定ページ
  • 25 - コメント
  • 59 - Separator
  • 60 - Appearance
  • 65 - Plugins
  • 70 - ユーザー
  • 75 - ツール
  • 80 - 設定
  • 99 - Separator

戻り値

文字列 
$hookname used internally to track menu page callbacks for outputting the page inside the global $menu array

用例

管理者権限ユーザー向けに、WordPressの管理メニューにカスタムメニュー項目を追加します。

例 1:

add_action( 'admin_menu', 'register_my_custom_menu_page' );

function register_my_custom_menu_page() {

	add_menu_page( 'custom menu title', 'custom menu', 'manage_options', 'myplugin/myplugin-admin.php', '', plugins_url( 'myplugin/images/icon.png' ), 6 );

}

With this method the page-generating code should be located in myplugin/myplugin-admin.php:

<?php echo "Admin Page Test"; ?>

例 2:

add_action( 'admin_menu', 'register_my_custom_menu_page' );

function register_my_custom_menu_page(){
	add_menu_page( 'custom menu title', 'custom menu', 'manage_options', 'custompage', 'my_custom_menu_page', plugins_url( 'myplugin/images/icon.png' ), 6 ); 
}

function my_custom_menu_page(){
	echo "Admin Page Test";	
}

  • If you're running into the "You do not have sufficient permissions to access this page" error, then you've hooked too early. The hook you should use is admin_menu.
  • If you only want to move existing admin menu items to different positions, you can use the admin_menu hook to unset menu items from their current positions in the global $menu and $submenu variables (which are arrays), and reset them elsewhere in the array.
  • This function takes a 'capability' (see Roles and Capabilities) which will be used to determine whether or not a page is included in the menu. The function which is hooked in to handle the output of the page must check that the user has the required 'capability' as well.
  • If you are using the Settings API to save data, and need the user to be other than the administrator, will need to modify the permissions via the hook option_page_capability_{$option_group}, where $option_group is the same as option_group in register_setting() . Check out the Settings API.

Example allowing an editor to save data:

// Register settings using the Settings API 
function register_my_setting() {
	register_setting( 'my-options-group', 'my-option-name', 'intval' ); 
} 
add_action( 'admin_init', 'register_my_setting' );

// Modify capability
function my_page_capability( $capability ) {
	return 'edit_others_posts';
}
add_filter( 'option_page_capability_my-options-group', 'my_page_capability' );

更新履歴

ソースファイル

add_menu_page()wp-admin/includes/plugin.phpにあります。


外部リソース



関連

管理メニュー: add_menu_page(), remove_menu_page(), add_submenu_page() /en, remove_submenu_page() /en, add_dashboard_page() /en, add_posts_page() /en, add_media_page() /en, add_links_page() /en, add_pages_page() /en, add_comments_page() /en, add_theme_page() /en, add_plugins_page() /en, add_users_page() /en, add_management_page() /en, add_options_page()


関数リファレンステンプレートタグ目次もご覧ください。


最新英語版: WordPress Codex » Function Reference/add_menu_page最新版との差分