Group settings together that have been created with the add_settings_field() function

[ad_1]

I’ve created a few fields using the `add_settings_field()` function but I can’t seem to figure out how to put some of them in a group.

I want to be able to group the controls together by a common theme rather than having one big stream of controls with little context between them all. “Hero controls”, “Meta” etc.

Has anyone else done this and if so how? I was initially outputting the group as html but it was being outputted on the top of the page rather than around the controls I was wanting to show.

​

if ( $field[‘type’] === ‘group’ ) {

?>
<div class=”options-group” id=”<?php echo esc_attr( $field[‘id’] ); ?>”>
<h3><?php echo esc_attr( $field[‘label’] ); ?></h3>
<?php
if ( esc_attr( $field[‘description’] ) != ” && esc_attr( $field[‘description’] ) != null ) {
?>
<p class=”description”>
<?php esc_html_e( $field[‘description’], ‘site-settings’ ); ?>
</p>
<?php
}
?>
<?php
foreach ( $field[‘sub’] as $sub_field ) {

// Register a new field in the main section.
add_settings_field(
$sub_field[‘id’], /* ID for the field. Only used internally. To set the HTML ID attribute, use $args[‘label_for’]. */
__( $sub_field[‘label’], ‘site-settings’ ), /* Label for the field. */
[$this, ‘render_field’], /* The name of the callback function. */
‘site-settings’, /* The menu page on which to display this field. */
‘site-settings-section’, /* The section of the settings page in which to show the box. */
[
‘label_for’ => $sub_field[‘id’], /* The ID of the field. */
‘class’ => ‘wporg_row’, /* The class of the field. */
‘field’ => $sub_field, /* Custom data for the field. */
] );
}
?>
</div>
<?php
} else {
// Register a new field in the main section.
add_settings_field(
$field[‘id’], /* ID for the field. Only used internally. To set the HTML ID attribute, use $args[‘label_for’]. */
__( $field[‘label’], ‘site-settings’ ), /* Label for the field. */
[$this, ‘render_field’], /* The name of the callback function. */
‘site-settings’, /* The menu page on which to display this field. */
‘site-settings-section’, /* The section of the settings page in which to show the box. */
[
‘label_for’ => $field[‘id’], /* The ID of the field. */
‘class’ => ‘wporg_row’, /* The class of the field. */
‘field’ => $field, /* Custom data for the field. */
] );
}

&#x200B;

[ad_2]

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer