当前有isn\'t 一种简单的API方法pre-sort 默认窗口小部件,意味着您的新窗口小部件将always be at the bottom 列表的。
下面是一个挂钩函数示例,它将尝试将您的小部件放在默认小部件之前。它通过手动更改元盒的内部数组(其中仪表板小部件是一种类型)并将您的小部件放在列表的顶部以使其首先显示来实现。
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;
}
不幸的是,这只适用于从未重新订购过小部件的用户。一旦用户这样做了,他们现有的首选项将覆盖此选项,他们将不得不将您的小部件移动到顶部,使其保持在那里。
上述内容引自Codex:Forcing your widget to the top