Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a subsection field #102

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 100 additions & 2 deletions src/class.settings-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ function show_navigation() {
}

/**
* Show the section settings forms
* Show the section settings forms using a custom do_settings_sections()
*
* This function displays every sections in a different form
*/
Expand All @@ -534,7 +534,7 @@ function show_forms() {
<?php
do_action( 'wsa_form_top_' . $form['id'], $form );
settings_fields( $form['id'] );
do_settings_sections( $form['id'] );
$this->do_settings_sections( $form['id'] );
do_action( 'wsa_form_bottom_' . $form['id'], $form );
if ( isset( $this->settings_fields[ $form['id'] ] ) ):
?>
Expand Down Expand Up @@ -652,6 +652,104 @@ function _style_fix() {
<?php
endif;
}

/**
* Remove the h2 section title and call the custom do_settings_fields
*
* @see \do_settings_sections() from template.php
* @param $page
*/
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['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">';
$this->do_settings_fields( $page, $section['id'] );
echo '</table>';
}
}

/**
* If there is a type == subsection, just call the call_user_func() instead of creating the tr
*
* @see \do_settings_fields() from template.php
* @param $page
* @param $section
*/
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 ( $field['args']['type'] != 'subsection' ) {
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>';
} else {
call_user_func( $field['callback'], $field['args'] );
}
}
}

/**
* Displays the html for a subsection field
*
* @param array $args settings field args
* @return string
*/
public function callback_subsection( $args ) {
if ( ! isset( $args['name'] ) ) {
return;
}
$subection = $args['name'];
if ( empty( $subection ) ) {
return;
}
if ( isset( $args['desc'] ) ) {
$desc = $args['desc'];
}

?>
</table>
<h2><?php echo esc_html( $subection ) ?></h2>
<?php if ( ! empty( $desc ) ): ?>
<p><?php echo esc_html( $desc ); ?></p>
<?php endif; ?>
<table class="form-table">
<tbody>
<?php
}



}

Expand Down