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

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

ダッシュボードウィジェット API

提供: WordPress Codex 日本語版
2010年3月27日 (土) 07:51時点におけるMizuno (トーク | 投稿記録)による版 (用例: の途中まで)

移動先: 案内検索

このページ「ダッシュボードウィジェット API」は未翻訳です。和訳や日本語情報を加筆してくださる協力者を求めています

WordPress 2.7 のリリースで、新しいダッシュボードウィジェット API が導入されました。 管理画面ダッシュボード に新しいウィジェットを追加するのがシンプルになりました。PHP と WordPress プラグイン API の知識が必要ですが、アクションフックやフィルターフックに精通したプラグイン/テーマ作成者なら数分でできるでしょう。これにより、プラグインを一層使いやすくなるでしょう。

概要

関数

ダッシュボードの追加に必要な主要なツールは wp_add_dashboard_widget() 関数です。

wp_add_dashboard_widget($widget_id, $widget_name, $callback, $control_callback = null)
  • $widget_id - ウィジェットを一意識別するスラッグ。これは css クラス、ウィジェットの配列のキーに使用します。
  • $widget_name - ウィジェットのヘッダに表示される名前。
  • $callback - ウィジェットのコンテンツを表示するように作成した関数の名前。
  • $control_callback - (オプション) ウィジェットオプションフォームの送信を処理し、フォーム要素を表示する関数の名前。

動作

関数を実行するには、適切な動作をフックする必要があります。この場合 'wp_dashboard_setup' です。コアコードは以下の通りです。

do_action( 'wp_dashboard_setup' );

用例

ウィジェットを追加する

ウィジェットのもっとも簡単なアウトラインです。

// ダッシュボードウィジェットにコンテンツを出力する関数を作成する

function example_dashboard_widget_function() {
	// Display whatever it is you want to show
	echo "Hello World, I'm a great Dashboard Widget";
} 

// アクションフックで使用する関数を作成する

function example_add_dashboard_widgets() {
	wp_add_dashboard_widget('example_dashboard_widget', 'Example Dashboard Widget', 'example_dashboard_widget_function');	
} 

// 'wp_dashboard_setup' アクションにフックし、登録する

add_action('wp_dashboard_setup', 'example_add_dashboard_widgets' );

Advanced: Forcing your widget to the top

Normally you should just let the users of your plugin put your Dashboard Widget wherever they want by dragging it around. There currently isn't an easy API way to pre-sort the default widgets, meaning your new widget will always be at the bottom of the list. Until sorting is added to the API its a bit complicated to get around this problem.

Below is an example hooking function that will try to put your widget before the default ones. It does so by manually altering the internal array of metaboxes (of which dashboard widgets are one type) and putting your widget at the top of the list so it shows first.

function example_add_dashboard_widgets() {
	wp_add_dashboard_widget('example_dashboard_widget', 'Example Dashboard Widget', 'example_dashboard_widget_function');
	
	// Globalize the metaboxes array, this holds all the widgets for wp-admin

	global $wp_meta_boxes;
	
	// Get the regular dashboard widgets array 
	// (which has our new widget already but at the end)

	$normal_dashboard = $wp_meta_boxes['dashboard']['normal']['core'];
	
	// Backup and delete our new dashbaord widget from the end of the array

	$example_widget_backup = array('example_dashboard_widget' => $normal_dashboard['example_dashboard_widget']);
	unset($normal_dashboard['example_dashboard_widget']);

	// Merge the two arrays together so our widget is at the beginning

	$sorted_dashboard = array_merge($example_widget_backup, $normal_dashboard);

	// Save the sorted array back into the original metaboxes 

	$wp_meta_boxes['dashboard']['normal']['core'] = $sorted_dashboard;
} 

Unfortunately this only works for people who have never re-ordered their widgets. Once a user has done so their existing preferences will override this and they will have to move your widget to the top for it to stay there.

Advanced: Removing Dashboard Widgets

In some situations, especially on multi-user blogs, it may be useful to completely remove widgets from the interface. Each individual user can, by default, turn off any given widget using the "Screen Options" tab at the top, but if you have a lot of non-technical users it might be nicer for them to not see it at all.

At this point (2.7.0) there is no function to easily remove the default dashboard widgets. To remove them you need to manually unset() their items from the general $wp_meta_box array. You can do so inside the wp_dashboard_setup action similar to how we added widgets.

Here are the names of the meta boxes on the dashboard:
Main column
$wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']
$wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_comments']
$wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']
$wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']

Side Column
$wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']
$wp_meta_boxes['dashboard']['side']['core']['dashboard_recent_drafts']
$wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']
$wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']

To print the list of names in either the normal or side columns, you can use this code (subbing in normal or core for the second position in the array):

foreach (array_keys($wp_meta_boxes['dashboard']['normal']['core']) as $name){
		echo ("<p>");
		echo ($name);
		echo("</p>");
		
	}

Here is an example function that removes the quickpress and incomming_links widgets by unsetting their array items.

// Create the function to use in the action hook

function example_remove_dashboard_widgets() {
	// Globalize the metaboxes array, this holds all the widgets for wp-admin

	global $wp_meta_boxes;

	// Remove the quickpress widget

	unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']);

	// Remove the incomming links widget

	unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']);	
} 

// Hoook into the 'wp_dashboard_setup' action to register our function

add_action('wp_dashboard_setup', 'example_remove_dashboard_widgets' );

Aggregating RSS feeds in the dashboard

Because so many of the default dashboard widgets are designed to aggregate RSS feeds about WordPress development and plugins there is already a lot of work done to make this easier. I don't have time to figure out a full example, but if you need to aggregate RSS in your widget I recommend looking at the way the existing plugins are set up with caching in /wp-admin/includes/dashboard.php.

最新英語版: WordPress Codex » Dashboard Widgets API最新版との差分