函数文档

do_settings_sections()

💡 云策文档标注

概述

do_settings_sections() 是 WordPress Settings API 的一部分,用于在设置页面回调函数中输出所有已添加的设置区域和字段。它根据指定的页面 slug 渲染相关 HTML 结构。

关键要点

  • 该函数属于 Settings API,用于输出特定设置页面的所有设置区域。
  • 必须在设置页面回调函数中使用,配合 add_settings_section() 和 add_settings_field() 添加内容。
  • 输出时,区域标题包裹在 h3 标签中,设置字段包裹在表格中。
  • 函数接受一个必需参数 $page,表示要输出设置区域的页面 slug。
  • 内部处理包括检查全局变量、遍历区域、输出 before/after 部分、标题、回调函数和字段。

代码示例

function do_settings_sections( $page ) {
	global $wp_settings_sections, $wp_settings_fields;

	if ( ! isset( $wp_settings_sections[ $page ] ) ) {
		return;
	}

	foreach ( (array) $wp_settings_sections[ $page ] as $section ) {
		if ( '' !== $section['before_section'] ) {
			if ( '' !== $section['section_class'] ) {
				echo wp_kses_post( sprintf( $section['before_section'], esc_attr( $section['section_class'] ) ) );
			} else {
				echo wp_kses_post( $section['before_section'] );
			}
		}

		if ( $section['title'] ) {
			echo "{$section['title']}n";
		}

		if ( $section['callback'] ) {
			call_user_func( $section['callback'], $section );
		}

		if ( isset( $wp_settings_fields[ $page ][ $section['id'] ] ) ) {
			echo '';
			do_settings_fields( $page, $section['id'] );
			echo '';
		}

		if ( '' !== $section['after_section'] ) {
			echo wp_kses_post( $section['after_section'] );
		}
	}
}

注意事项

  • 使用前需确保已通过 add_settings_section() 和 add_settings_field() 添加了相关区域和字段。
  • 函数依赖于全局变量 $wp_settings_sections 和 $wp_settings_fields,需在正确上下文中调用。
  • 输出内容已通过 wp_kses_post() 和 esc_attr() 进行安全处理,确保 HTML 和属性转义。

📄 原文内容

Prints out all settings sections added to a particular settings page.

Description

Part of the Settings API. Use this in a settings page callback function to output all the sections and fields that were added to that $page with add_settings_section() and add_settings_field()

Parameters

$pagestringrequired
The slug name of the page whose settings sections you want to output.

More Information

This will output the section titles wrapped in h3 tags and the settings fields wrapped in tables.

Source

function do_settings_sections( $page ) {
	global $wp_settings_sections, $wp_settings_fields;

	if ( ! isset( $wp_settings_sections[ $page ] ) ) {
		return;
	}

	foreach ( (array) $wp_settings_sections[ $page ] as $section ) {
		if ( '' !== $section['before_section'] ) {
			if ( '' !== $section['section_class'] ) {
				echo wp_kses_post( sprintf( $section['before_section'], esc_attr( $section['section_class'] ) ) );
			} else {
				echo wp_kses_post( $section['before_section'] );
			}
		}

		if ( $section['title'] ) {
			echo "<h2>{$section['title']}</h2>n";
		}

		if ( $section['callback'] ) {
			call_user_func( $section['callback'], $section );
		}

		if ( isset( $wp_settings_fields[ $page ][ $section['id'] ] ) ) {
			echo '<table class="form-table" role="presentation">';
			do_settings_fields( $page, $section['id'] );
			echo '</table>';
		}

		if ( '' !== $section['after_section'] ) {
			echo wp_kses_post( $section['after_section'] );
		}
	}
}

Changelog

Version Description
2.7.0 Introduced.