钩子文档

default_hidden_meta_boxes

💡 云策文档标注

概述

default_hidden_meta_boxes 是一个 WordPress 过滤器,用于修改默认隐藏的 meta boxes 列表。它允许开发者根据当前屏幕(如文章编辑页面)自定义哪些 meta boxes 在初始状态下被隐藏,用户仍可通过“屏幕选项”显示它们。

关键要点

  • 过滤器名称:default_hidden_meta_boxes
  • 参数:$hidden(数组,默认隐藏的 meta boxes ID 列表)和 $screen(WP_Screen 对象,表示当前屏幕)
  • 用途:控制 meta boxes 的默认可见性,常用于自定义文章类型或特定屏幕
  • 版本:自 WordPress 3.1.0 引入

代码示例

add_filter('default_hidden_meta_boxes','hide_meta_box',10,2);
function hide_meta_box($hidden, $screen) {
    // 确保处理正确的屏幕
    if ( ('post' == $screen->base) && ('my-custom-post_type' == $screen->id) ){
      // 隐藏所有 meta boxes
      $hidden = array('postexcerpt','slugdiv','postcustom','trackbacksdiv', 'commentstatusdiv', 'commentsdiv', 'authordiv', 'revisionsdiv');
      $hidden[] ='my_custom_meta_box'; // 对于自定义 meta box,使用 add_meta_box() 中定义的 ID
    }
    return $hidden;
  }

注意事项

用户可以通过“屏幕选项”选项卡重新显示被默认隐藏的 meta boxes,因此此过滤器仅影响初始状态,不强制永久隐藏。


📄 原文内容

Filters the default list of hidden meta boxes.

Parameters

$hiddenstring[]
An array of IDs of meta boxes hidden by default.
$screenWP_Screen
WP_Screen object of the current screen.

Source

$hidden = apply_filters( 'default_hidden_meta_boxes', $hidden, $screen );

Changelog

Version Description
3.1.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    This filter is used to hide metabox by default, an admin user can then select the ‘Screen Options’ tab in the top right hand corner of the post edit screen and they will see the designated ‘hidden’ metaboxes checked in the list of options,

    add_filter('default_hidden_meta_boxes','hide_meta_box',10,2);
    function hide_meta_box($hidden, $screen) {
        //make sure we are dealing with the correct screen
        if ( ('post' == $screen->base) && ('my-custom-post_type' == $screen->id) ){
          //lets hide everything
          $hidden = array('postexcerpt','slugdiv','postcustom','trackbacksdiv', 'commentstatusdiv', 'commentsdiv', 'authordiv', 'revisionsdiv');
          $hidden[] ='my_custom_meta_box';//for custom meta box, enter the id used in the add_meta_box() function.
        }
        return $hidden;
      }