add_meta_box() 是 WordPress 的核心函数,主要用于为屏幕(Screen)增加一些元数据信息。一般应用于 Post 文章编辑器,定义一些基础信息,比如自定义 SEO 信息,自定义文章来源信息、自定义产品信息及其他信息。函数语法为:

add_meta_box( string $idstring $titlecallable $callback,string|array|WP_Screen $screen = nullstring $context = 'advanced',string $priority = 'default'array $callback_args = null )

$id:字符串(必填)。元数据框的 ID 值。用于元数据的 ID 属性。

$title:字符串(必填)。元数据框的标题。

$callback:回调函数(必填)。添加到设置区域的显示函数(回调函数)。

$screen字符串、数组或 WP_Screen(选填)。元数据应用的屏幕(Screen),评论、文章、菜单等。默认为空( null )。

$context:字符串(选填)。元数据框显示的位置。后台编辑器的话包含 ‘normal’、’side’ 以及 ‘advanced’ ;评论的话包含 ‘normal’ 及 ‘side’ ,菜单的话均为 ‘side’ 。默认为 ‘advanced’ 。

$priority:字符串(选填)。优先级顺序,可以是 ‘high’ 或者 ‘low’ 。默认值为 ‘default’ 。

$callback_args:数据(选填)。元数据框的 $args 属性,传递给回调函数的其他参数。默认为空( null )。

该函数定义在 wp-admin/includes/template.php 文件中,具体代码如下:

function add_meta_box( $id, $title, $callback, $screen = null, $context = 'advanced', $priority = 'default', $callback_args = null ) {
global $wp_meta_boxes;

if ( empty( $screen ) ) {
$screen = get_current_screen();
} elseif ( is_string( $screen ) ) {
$screen = convert_to_screen( $screen );
} elseif ( is_array( $screen ) ) {
foreach ( $screen as $single_screen ) {
add_meta_box( $id, $title, $callback, $single_screen, $context, $priority, $callback_args );
}
}

if ( ! isset( $screen->id ) ) {
return;
}

$page = $screen->id;

if ( !isset($wp_meta_boxes) )
$wp_meta_boxes = array();
if ( !isset($wp_meta_boxes[$page]) )
$wp_meta_boxes[$page] = array();
if ( !isset($wp_meta_boxes[$page][$context]) )
$wp_meta_boxes[$page][$context] = array();

foreach ( array_keys($wp_meta_boxes[$page]) as $a_context ) {
foreach ( array('high', 'core', 'default', 'low') as $a_priority ) {
if ( !isset($wp_meta_boxes[$page][$a_context][$a_priority][$id]) )
continue;

// If a core box was previously added or removed by a plugin, don't add.
if ( 'core' == $priority ) {
// If core box previously deleted, don't add
if ( false === $wp_meta_boxes[$page][$a_context][$a_priority][$id] )
return;

/*
* If box was added with default priority, give it core priority to
* maintain sort order.
*/
if ( 'default' == $a_priority ) {
$wp_meta_boxes[$page][$a_context]['core'][$id] = $wp_meta_boxes[$page][$a_context]['default'][$id];
unset($wp_meta_boxes[$page][$a_context]['default'][$id]);
}
return;
}
// If no priority given and id already present, use existing priority.
if ( empty($priority) ) {
$priority = $a_priority;
/*
* Else, if we're adding to the sorted priority, we don't know the title
* or callback. Grab them from the previously added context/priority.
*/
} elseif ( 'sorted' == $priority ) {
$title = $wp_meta_boxes[$page][$a_context][$a_priority][$id]['title'];
$callback = $wp_meta_boxes[$page][$a_context][$a_priority][$id]['callback'];
$callback_args = $wp_meta_boxes[$page][$a_context][$a_priority][$id]['args'];
}
// An id can be in only one priority and one context.
if ( $priority != $a_priority || $context != $a_context )
unset($wp_meta_boxes[$page][$a_context][$a_priority][$id]);
}
}

if ( empty($priority) )
$priority = 'low';

if ( !isset($wp_meta_boxes[$page][$context][$priority]) )
$wp_meta_boxes[$page][$context][$priority] = array();

$wp_meta_boxes[$page][$context][$priority][$id] = array('id' => $id, 'title' => $title, 'callback' => $callback, 'args' => $callback_args);
}

参考文档:https://developer.wordpress.org/reference/functions/add_meta_box/