删除自定义仪表板小部件

时间:2013-04-21 作者:Jessica

下面是关于如何删除所有仪表板小部件的代码-默认和插件小部件,但我想更改代码,只删除自定义仪表板小部件,而不是默认的仪表板小部件。

<小时>

// Create the function to use in the action hook
function remove_dashboard_widgets() {
    global $wp_meta_boxes;
    if ( !array( $wp_meta_boxes ) ) return;
    foreach ( $wp_meta_boxes as $widget_section => $widget_locations ) {
        if ( $widget_section == \'dashboard\' ) {
            foreach ( $widget_locations as $widget_location => $widget_types ) {
                foreach ( $widget_types as $widget_type => $widgets ) {
                    foreach ( $widgets as $widget_name => $widget ) {
                        remove_meta_box( $widget_name, $widget_section, $widget_location );
                    }               
                }
            }
        }
    }
} 
// Hook into the \'wp_dashboard_setup\' action to register our function
add_action(\'wp_dashboard_setup\', \'remove_dashboard_widgets\', 11 );

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

内置仪表板小部件没有标记,您必须使用固定列表:

\'dashboard_right_now\',
\'dashboard_plugins\',
\'dashboard_quick_press\',
\'dashboard_recent_drafts\',
\'dashboard_recent_comments\',
\'dashboard_incoming_links\',
\'dashboard_primary\',
\'dashboard_secondary\'
因此,您的代码应该测试当前小部件是否在该列表中,如果不在该列表中,则删除该小部件:

add_action(
    \'wp_dashboard_setup\',
    \'t5_remove_custom_dashboard_widgets\',
    11
);

function t5_remove_custom_dashboard_widgets()
{
    global $wp_meta_boxes;

    $builtin = array (
        \'dashboard_right_now\',
        \'dashboard_plugins\',
        \'dashboard_quick_press\',
        \'dashboard_recent_drafts\',
        \'dashboard_recent_comments\',
        \'dashboard_incoming_links\',
        \'dashboard_primary\',
        \'dashboard_secondary\'
    );

    if ( empty ( $wp_meta_boxes[\'dashboard\'] ) )
        return;

    $widget_groups = $wp_meta_boxes[\'dashboard\'];

    foreach ( $widget_groups as $section => $widget_group )
        foreach ( $widget_group as $widgets )
            foreach ( $widgets as $id => $widget )
                if ( ! in_array( $id, $builtin ) )
                    remove_meta_box( $id, \'dashboard\', $section );
}

SO网友:s_ha_dum

WordPressregisters its widgets with the dashboard_ prefix 因此,您可以在删除小部件之前检查前缀。

// Create the function to use in the action hook
function remove_dashboard_widgets() {
  global $wp_meta_boxes;
    if ( !array( $wp_meta_boxes ) ) return;
      foreach ( $wp_meta_boxes as $widget_section => $widget_locations ) {
        if ( $widget_section == \'dashboard\' ) {
           foreach ( $widget_locations as $widget_location => $widget_types ) {
               foreach ( $widget_types as $widget_type => $widgets ) {
                 foreach ( $widgets as $widget_name => $widget ) {
                    preg_match(\'/^dashboard_/\',$widget_name,$matches);
                    if (\'dashboard_\' !== $matches[0]) {
                       remove_meta_box( $widget_name, $widget_section, $widget_location );
                    }
                  }               
               }
           }
        }
    }
} 
// Hook into the \'wp_dashboard_setup\' action to register our function
add_action(\'wp_dashboard_setup\', \'remove_dashboard_widgets\', 11 );
当然,没有什么可以阻止某个插件或主题注册具有相同前缀的小部件。

结束

相关推荐

Individual Widgets per Page

在我的Wordpress网站(仅限于页面,无帖子)中,我需要在侧边栏中放置一些静态blob。我们可以将这些“blob”称为小部件。至少他们会有一个固定的html内容,如“摘要1”、“摘要2”、“免责声明”、“foo策略”。。。我需要的是,在每一页上都有不同的安排。因此,这不仅仅是“单一”对“归档”,而是真正的“产品页面A”对“产品页面B”对“关于我们”对“服务条款”。。。我在考虑自定义元框,以便在单个编辑器页面上打开和关闭它们wp-admin/post.php?post=196&action=ed