获取已注册的元框列表并删除它们


9

是否有获取已注册元框列表并将其删除的功能?我看到有添加和删除的方法。

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

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


1
请回滚您的更改并提出一个新问题,而不仅仅是添加到现有问题中。获取屏幕选项与获取元框非常不同。
EAMann 2012年

Answers:


9

并非如此,但是您可以定义自己的。所有元框都存储在全局变量中$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']也与多维数组相同。

删除核心元框

假设您要删除一堆meta框。上面的功能可以稍作调整以利用:

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' );

嘿,我知道你回答了很久,但是能否请您看看我关于这个完全相同的问题的问题?该global工作不适合我!谢谢。wordpress.stackexchange.com/questions/318834/…–
Middlelady

1

在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);查看已注册的元框列表。


1
此代码并没有提供可用的元框的列表。
fuxia

@toscho 如果您这样做的print_r($wp_meta_boxes);
Michael Ecklund 2012年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.