函数文档

meta_box_prefs()

💡 云策文档标注

概述

meta_box_prefs() 函数用于输出屏幕元数据中元框的偏好设置,包括隐藏状态的复选框。它遍历指定屏幕的元框,并生成相应的 HTML 输出。

关键要点

  • 函数接受一个 WP_Screen 对象或字符串作为参数,用于指定目标屏幕。
  • 通过 get_hidden_meta_boxes() 获取隐藏元框的 ID 数组,以确定每个元框的显示状态。
  • 遍历元框的上下文(context)和优先级(priority),跳过无效或特定不可隐藏的元框(如 submitdiv 和 linksubmitdiv)。
  • 输出包含复选框的 HTML,用于控制元框的隐藏或显示,使用 checked() 和 esc_attr() 函数确保安全性。

代码示例

function meta_box_prefs( $screen ) {
    global $wp_meta_boxes;

    if ( is_string( $screen ) ) {
        $screen = convert_to_screen( $screen );
    }

    if ( empty( $wp_meta_boxes[ $screen->id ] ) ) {
        return;
    }

    $hidden = get_hidden_meta_boxes( $screen );

    foreach ( array_keys( $wp_meta_boxes[ $screen->id ] ) as $context ) {
        foreach ( array( 'high', 'core', 'default', 'low' ) as $priority ) {
            if ( ! isset( $wp_meta_boxes[ $screen->id ][ $context ][ $priority ] ) ) {
                continue;
            }

            foreach ( $wp_meta_boxes[ $screen->id ][ $context ][ $priority ] as $box ) {
                if ( false === $box || ! $box['title'] ) {
                    continue;
                }

                // Submit box cannot be hidden.
                if ( 'submitdiv' === $box['id'] || 'linksubmitdiv' === $box['id'] ) {
                    continue;
                }

                $widget_title = $box['title'];

                if ( is_array( $box['args'] ) && isset( $box['args']['__widget_basename'] ) ) {
                    $widget_title = $box['args']['__widget_basename'];
                }

                $is_hidden = in_array( $box['id'], $hidden, true );

                printf(
                    '%3$s',
                    esc_attr( $box['id'] ),
                    checked( $is_hidden, false, false ),
                    $widget_title
                );
            }
        }
    }
}

注意事项

  • 函数依赖于全局变量 $wp_meta_boxes,确保在调用前已正确初始化。
  • submitdiv 和 linksubmitdiv 元框被设计为不可隐藏,在遍历中会被跳过。
  • 使用 esc_attr() 和 checked() 函数来防止 XSS 攻击并正确输出 HTML 属性。
  • 此函数自 WordPress 2.7.0 版本引入,主要用于 WP_Screen::render_meta_boxes_preferences() 方法。

📄 原文内容

Prints the meta box preferences for screen meta.

Parameters

$screenWP_Screenrequired

Source

function meta_box_prefs( $screen ) {
	global $wp_meta_boxes;

	if ( is_string( $screen ) ) {
		$screen = convert_to_screen( $screen );
	}

	if ( empty( $wp_meta_boxes[ $screen->id ] ) ) {
		return;
	}

	$hidden = get_hidden_meta_boxes( $screen );

	foreach ( array_keys( $wp_meta_boxes[ $screen->id ] ) as $context ) {
		foreach ( array( 'high', 'core', 'default', 'low' ) as $priority ) {
			if ( ! isset( $wp_meta_boxes[ $screen->id ][ $context ][ $priority ] ) ) {
				continue;
			}

			foreach ( $wp_meta_boxes[ $screen->id ][ $context ][ $priority ] as $box ) {
				if ( false === $box || ! $box['title'] ) {
					continue;
				}

				// Submit box cannot be hidden.
				if ( 'submitdiv' === $box['id'] || 'linksubmitdiv' === $box['id'] ) {
					continue;
				}

				$widget_title = $box['title'];

				if ( is_array( $box['args'] ) && isset( $box['args']['__widget_basename'] ) ) {
					$widget_title = $box['args']['__widget_basename'];
				}

				$is_hidden = in_array( $box['id'], $hidden, true );

				printf(
					'<label for="%1$s-hide"><input class="hide-postbox-tog" name="%1$s-hide" type="checkbox" id="%1$s-hide" value="%1$s" %2$s />%3$s</label>',
					esc_attr( $box['id'] ),
					checked( $is_hidden, false, false ),
					$widget_title
				);
			}
		}
	}
}

Changelog

Version Description
2.7.0 Introduced.