問題描述
是否有獲取註冊的元框列表並刪除它們的功能?我看到有一種添加和刪除的方法。
http://codex.wordpress.org/Function_Reference/remove_meta_box
http://codex.wordpress.org/Function_Reference/add_meta_box
最佳解決方案
不是真的,但你可以自己定義。所有元框都存儲在全局變量 $wp_meta_boxes 中,它是一個 multi-dimensional 數組。
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 隔離元框。
所以我們假設你想得到一個數組,它包含管理儀錶板上”normal” 優先級的所有元框。你會調用以下內容:
$dashboard_boxes = get_meta_boxes( 'dashboard', 'normal' );
這與全局陣列 $wp_meta_boxes['dashboard']['normal']相同,它也是一個 multi-dimensional 陣列。
刪除核心元框
假設您要刪除一堆元框。上面的功能可以略微調整,以便:
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' );
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。