do_settings_sections()WP 2.7.0

Prints out all settings sections added to a particular settings page

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()

No Hooks.

Return

null. Nothing (null).

Usage

do_settings_sections( $page );
$page(string) (required)
The slug name of the page whose settings sections you want to output.

Examples

0

#1 Displaying settings sections on the settings page

Suppose we registered new options, added a block and fields to this block. Now, we need to display these fields on the settings page of the plugin in the admin panel, then use this function:

<div class="wrap">  
	<?php screen_icon(); ?>  
	<h2>Primer plugin settings</h2>  

	<form action="options.php" method="POST">  
		<?php settings_fields( 'option_id' ); ?>
		<?php do_settings_sections( 'primer_page' ); ?> 
		<?php submit_button(); ?>  
	</form>  
</div>

See the Settings API for a full example of how to use it.

Notes

  • Global. Array. $wp_settings_sections Storage array of all settings sections added to admin pages.
  • Global. Array. $wp_settings_fields Storage array of settings fields and info about their pages/sections.

Changelog

Since 2.7.0 Introduced.

do_settings_sections() code WP 6.5.3

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 ) || ! isset( $wp_settings_fields[ $page ] ) || ! isset( $wp_settings_fields[ $page ][ $section['id'] ] ) ) {
			continue;
		}
		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'] );
		}
	}
}