Reorder dashboard widgets

时间:2013-01-20 作者:Steef

我最近试图通过编写插件对仪表板小部件进行重新排序。我做到了这一点:

enter image description here

使用以下代码:

<?php
/*
 * Plugin Name: Custom Dashboard
 * Description: Custom dashboard for Avare sites.
 * Author: Avare
 * Version: 1.0
 */

function sort_dashboard_widgets() {

 $left_column_widgets[] = \'dashboard_right_now\';
 $left_column_widgets[] = \'dashboard_recent_comments\';
 $left_column_widgets[] = \'dashboard_incoming_links\';

 $right_column_widgets[] = \'dashboard_quick_press\';
 $right_column_widgets[] = \'dashboard_recent_drafts\';
 $right_column_widgets[] = \'dashboard_primary\';
 $right_column_widgets[] = \'dashboard_secundary\';

// Global the $wp_meta_boxes variable (this will allow us to alter the array)
global $wp_meta_boxes;

// We then unset that part of the array
unset($wp_meta_boxes[\'dashboard\'][\'normal\'][\'core\']);
unset($wp_meta_boxes[\'dashboard\'][\'side\'][\'core\']);

// Then we make a backup of the widget areas
$left_dashboard = $wp_meta_boxes[\'dashboard\'][\'normal\'][\'core\'];
$right_dashboard = $wp_meta_boxes[\'dashboard\'][\'side\'][\'core\'];

// Then we merge them in some sort of way (is this necessary?)
$sorted_left_dashboard = array_merge((array)$left_column_widgets, (array)$left_dashboard);
$sorted_right_dashboard = array_merge((array)$right_column_widgets, (array)$right_dashboard);

// Now we add the sorted widgets back in
$wp_meta_boxes[\'dashboard\'][\'normal\'][\'core\'] = $sorted_left_dashboard;
$wp_meta_boxes[\'dashboard\'][\'side\'][\'core\'] = $sorted_right_dashboard;

}

add_filter(\'wp_dashboard_setup\', \'sort_dashboard_widgets\');
正如您在我制作的屏幕截图中所看到的,该插件似乎只查看$left_column_widgets$right_column_widgets, 这会导致错误(因为“d”不是有效的小部件段塞)。有没有办法解决这个问题?

干杯

2 个回复
最合适的回答,由SO网友:Steef 整理而成

哎哟,我想出来了。:)虽然我以前就知道了kaiser 回答了我的问题,我仍然要感谢他给出了清晰而令人敬畏的答案。下面是我为重新排序仪表板小部件而编写的代码。

<?php
/**
 * Plugin Name: Custom Dashboard
 * Description: Custom dashboard for Avare sites.
 * Author: Avare
 * Version: 1.0
 */

function sort_dashboard_widgets() {

    // Global the $wp_meta_boxes variable (this will allow us to alter the array)
    global $wp_meta_boxes;

    // Then unset everything in the array
    unset($wp_meta_boxes[\'dashboard\'][\'normal\'][\'core\']);
    unset($wp_meta_boxes[\'dashboard\'][\'side\'][\'core\']);

    // Add widgets to left column
    add_meta_box(\'dashboard_right_now\', __(\'Right Now\'), \'wp_dashboard_right_now\', \'dashboard\', \'normal\', \'core\');
    add_meta_box(\'dashboard_recent_comments\', __(\'Recent Comments\'), \'wp_dashboard_recent_comments\', \'dashboard\', \'normal\', \'core\');

    // Add widgets to right column
    add_meta_box(\'dashboard_quick_press\', __(\'QuickPress\'), \'wp_dashboard_quick_press\', \'dashboard\', \'side\', \'core\');
    add_meta_box(\'dashboard_recent_drafts\', __(\'Recent Drafts\'), \'wp_dashboard_recent_drafts\', \'dashboard\', \'side\', \'core\');

}

add_action(\'wp_dashboard_setup\', \'sort_dashboard_widgets\');
请注意,此代码并没有使用所有可用的仪表板小部件。

SO网友:kaiser

首先,有一点误解。wp_dashboard_setup 是一个动作,而不是过滤器。如果它是一个过滤器,它将有一个或多个参数,并且需要返回第一个参数。

如何#1

有关此操作的示例,请参阅我使用的以下mu插件:

<?php
/**
 * Plugin Name: Remove Dashboard Widgets
 * Description: Removes all Dashboard Widgets
 */
function dashboard_widgets()
{
    remove_meta_box( \'dashboard_browser_nag\',     \'dashboard\', \'normal\' );
    remove_meta_box( \'dashboard_right_now\',       \'dashboard\', \'normal\' );
    remove_meta_box( \'dashboard_recent_comments\', \'dashboard\', \'normal\' );
    remove_meta_box( \'dashboard_plugins\',         \'dashboard\', \'normal\' );

    remove_meta_box( \'dashboard_quick_press\',     \'dashboard\', \'side\' );
    remove_meta_box( \'dashboard_recent_drafts\',   \'dashboard\', \'side\' );
    remove_meta_box( \'dashboard_primary\',         \'dashboard\', \'side\' );
    remove_meta_box( \'dashboard_secondary\',       \'dashboard\', \'side\' );
}
add_action( \'wp_dashboard_setup\', \'dashboard_widgets\' );
删除这些小部件后,可以使用

add_dashboard_widget( $widget_id, $widget_name, $callback );
欲了解更多信息,请参阅Codex和Dashboard Widgets API. 要查看默认值,请查看/wp-admin/includes/dashboard.php -> wp_setup_dashboard(). 然后简单地将它们添加回您喜欢/需要的位置。

如何#2

还有其他不同的选项:过滤器。

但首先,我们应该弄清楚您的目标是哪个仪表盘。在某些情况下(插件等),所有这些都会有不同的仪表板和过滤器

网络管理员(又称超级管理员)->wp_network_dashboard_widgetswp_user_dashboard_widgets

  • 用户->wp_dashboard_widgets$dashboard_widgets 数组(并应返回它)。

    扩展默认窗口小部件如果要扩展默认窗口小部件,则需要使用\'do_meta_boxes\' 钩子,它有三个参数(屏幕ID、像side这样的位置、normal等)和一个空的第三个参数。

    缺点和限制您必须接受这样一个事实,即用户可以重新排序这些小部件(甚至停用它们)。这件事应该是那样的,并保持这样。

  • 结束

    相关推荐

    Wp_admin edit.php速度慢,有很多查询

    我有大约4000个帖子。目前,当我点击所有帖子(edit.php)时,加载大约需要10秒,我显示了大约1000个查询!大多数看起来像。。。SELECT t.*, tt.* FROM wp_terms AS t INNER JOIN wp_term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN wp_term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy