获取已注册Meta箱的列表并将其删除

时间:2012-06-21 作者:Michael Ecklund

是否有获取已注册元框列表并删除它们的功能?我知道有一种添加和删除的方法。

http://codex.wordpress.org/Function_Reference/remove_meta_box

http://codex.wordpress.org/Function_Reference/add_meta_box

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

不太可能,但你可以自己定义。所有元框都存储在全局变量中$wp_meta_boxes 这是一个多维数组。

function get_meta_boxes( $screen = null, $context = \'advanced\' ) {
    global $wp_meta_boxes;

    if ( empty( $screen ) )
        $screen = get_current_screen();
    elseif ( is_string( $screen ) )
        $screen = convert_to_screen( $screen );

    $page = $screen->id;

    return $wp_meta_boxes[$page][$context];          
}
该数组将显示为特定屏幕和特定上下文注册的所有元框。您还可以进一步深入,因为此数组也是一个多维数组,它按优先级和id分隔元框。

假设您想要得到一个数组,其中包含所有的元框;“正常”;管理仪表板上的优先级。您可以调用以下命令:

$dashboard_boxes = get_meta_boxes( \'dashboard\', \'normal\' );
这与全局数组相同$wp_meta_boxes[\'dashboard\'][\'normal\'] 它也是一个多维数组。

移除核心元盒,假设您要移除一组元盒。可以稍微调整上面的函数,以便:

function remove_meta_boxes( $screen = null, $context = \'advanced\', $priority = \'default\', $id ) {
    global $wp_meta_boxes;

    if ( empty( $screen ) )
        $screen = get_current_screen();
    elseif ( is_string( $screen ) )
        $screen = convert_to_screen( $screen );

    $page = $screen->id;

    unset( $wp_meta_boxes[$page][$context][$priority][$id] );
}
如果要从仪表板中删除(例如)传入链接小部件,可以调用:

remove_meta_boxes( \'dashboard\', \'normal\', \'core\', \'dashboard_incoming_links\' );

SO网友:Michael Ecklund

WordPress仪表板上显示了元框。有一个普通列和一个侧列。

我可以获得已注册元框的列表,并使用以下代码将其从仪表板中删除:

// Remove some non-sense meta boxes
function remove_dashboard_meta_boxes(){
    global $wp_meta_boxes;
    // Dashboard core widgets :: Left Column
    unset($wp_meta_boxes[\'dashboard\'][\'normal\'][\'core\'][\'dashboard_incoming_links\']);
    unset($wp_meta_boxes[\'dashboard\'][\'normal\'][\'core\'][\'dashboard_recent_comments\']);
    unset($wp_meta_boxes[\'dashboard\'][\'normal\'][\'core\'][\'dashboard_plugins\']);
    unset($wp_meta_boxes[\'dashboard\'][\'normal\'][\'core\'][\'dashboard_right_now\']);
    // Additional dashboard core widgets :: Right Column
    unset($wp_meta_boxes[\'dashboard\'][\'side\'][\'core\'][\'dashboard_recent_drafts\']);
    unset($wp_meta_boxes[\'dashboard\'][\'side\'][\'core\'][\'dashboard_quick_press\']);
    unset($wp_meta_boxes[\'dashboard\'][\'side\'][\'core\'][\'dashboard_primary\']);
    unset($wp_meta_boxes[\'dashboard\'][\'side\'][\'core\'][\'dashboard_secondary\']);
    // Remove the welcome panel
    update_user_meta(get_current_user_id(), \'show_welcome_panel\', false);
}
add_action(\'wp_dashboard_setup\', \'remove_dashboard_meta_boxes\');
仅使用print_r($wp_meta_boxes); 查看已注册元框的列表。

结束

相关推荐

Metabox nonce PHP notice

当我尝试在两种不同的分类法中添加新帖子时,我收到了一条通知,其中一种我使用自定义元框,另一种则不使用,但是当我尝试添加新项目时,这两种分类法都会出现通知,我看到:通知:未定义索引:product\\u noncename in/Applications/MAMP/htdocs/site/wordpress/wp-content/themes/theme/functions。第259行中的php隐藏在WP Admin中顶部黑色条带的略下方Line 259 is:if ( !wp_verify_nonce(