do_settings_fields()WP 2.7.0

Displays on the screen the option fields related to the specified section.

The function works in conjunction with other functions of the Settings API.

Note: usually, instead of this function do_settings_sections() is used to output the option fields associated with the section.

The function is similar to do_settings_sections(); it also outputs fields for a specific page and section, but these fields are not formatted in a table layout and are output as-is.

No Hooks.

Returns

null. Outputs the HTML code to the screen: the form fields.

Usage

do_settings_fields( $page, $section );
$page(string) (required)
Identifier of the admin page (page slug) on which to output the form fields. Must match the $page parameter from
add_settings_section( $id, $title, $callback, $page ).
$section(string) (required)
Identifier of the section whose option fields should be output. Must match the $id parameter from
add_settings_section( $id, $title, $callback, $page ).

Examples

0

#1 Output the registered fields

Suppose we registered new options, added a section and fields to that section. Now, we need to display these fields on the plugin settings page 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( 'primer_group' ); ?>
		<?php do_settings_fields( 'primer_page' ); ?> 
		<?php submit_button(); ?>  
	</form>  

</div>

For more info see API settings.

Notes

  • Global. Array. $wp_settings_fields Storage array of settings fields and their pages/sections.

Changelog

Since 2.7.0 Introduced.

do_settings_fields() code WP 7.0

function do_settings_fields( $page, $section ) {
	global $wp_settings_fields;

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

	foreach ( (array) $wp_settings_fields[ $page ][ $section ] as $field ) {
		$class = '';

		if ( ! empty( $field['args']['class'] ) ) {
			$class = ' class="' . esc_attr( $field['args']['class'] ) . '"';
		}

		echo "<tr{$class}>";

		if ( ! empty( $field['args']['label_for'] ) ) {
			echo '<th scope="row"><label for="' . esc_attr( $field['args']['label_for'] ) . '">' . $field['title'] . '</label></th>';
		} else {
			echo '<th scope="row">' . $field['title'] . '</th>';
		}

		echo '<td>';
		call_user_func( $field['callback'], $field['args'] );
		echo '</td>';
		echo '</tr>';
	}
}