Make WordPress Core


Ignore:
Timestamp:
09/20/2022 09:57:43 AM (3 years ago)
Author:
audrasjb
Message:

Administration: Allow to wrap Settings sections with custom HTML content.

This changeset improves the add_settings_section() function to allow developers to pass extra HTML mark-up to be rendered before and after the settings section. Extra argument $args can now be passed to the function, and is an array that can contain the following items:

  • before_section: HTML content to prepend to the section's HTML output. Receives the section's class name provided with the section_class argument via an optional %s placeholder. Default empty.
  • after_section: HTML content to append to the section's HTML output. Default empty.
  • section_class: The class name to use for the section. Used by before_section if a %s placeholder is present. Default empty.

The HTML passed using these extra arguments is escaped using wp_kses_post() just before rendering. This changeset also provides a set of unit tests for this new feature.

Props griffinjt, nacin, scribu, ross_ritchey, ryan, chriscct7, palmiak, rehanali, costdev, martinkrcho, chaion07, audrasjb, hellofromtonya.
Fixes #17851.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/template.php

    r54071 r54247  
    15621562 *
    15631563 * @since 2.7.0
     1564 * @since 6.1.0 Added an `$args` parameter for the section's HTML wrapper and class name.
    15641565 *
    15651566 * @global array $wp_settings_sections Storage array of all settings sections added to admin pages.
     
    15711572 *                           'general', 'reading', 'writing', 'discussion', 'media', etc. Create your own using
    15721573 *                           add_options_page();
    1573  */
    1574 function add_settings_section( $id, $title, $callback, $page ) {
     1574 * @param array    $args     {
     1575 *     Arguments used to create the settings section.
     1576 *
     1577 *     @type string $before_section HTML content to prepend to the section's HTML output.
     1578 *                                  Receives the section's class name as `%s`. Default empty.
     1579 *     @type string $after_section  HTML content to append to the section's HTML output. Default empty.
     1580 *     @type string $section_class  The class name to use for the section. Default empty.
     1581 * }
     1582 */
     1583function add_settings_section( $id, $title, $callback, $page, $args = array() ) {
    15751584    global $wp_settings_sections;
     1585
     1586    $defaults = array(
     1587        'id'             => $id,
     1588        'title'          => $title,
     1589        'callback'       => $callback,
     1590        'before_section' => '',
     1591        'after_section'  => '',
     1592        'section_class'  => '',
     1593    );
     1594
     1595    $section = wp_parse_args( $args, $defaults );
    15761596
    15771597    if ( 'misc' === $page ) {
     
    16011621    }
    16021622
    1603     $wp_settings_sections[ $page ][ $id ] = array(
    1604         'id'       => $id,
    1605         'title'    => $title,
    1606         'callback' => $callback,
    1607     );
     1623    $wp_settings_sections[ $page ][ $id ] = $section;
    16081624}
    16091625
     
    17011717
    17021718    foreach ( (array) $wp_settings_sections[ $page ] as $section ) {
     1719        if ( '' !== $section['before_section'] ) {
     1720            if ( '' !== $section['section_class'] ) {
     1721                echo wp_kses_post( sprintf( $section['before_section'], esc_attr( $section['section_class'] ) ) );
     1722            } else {
     1723                echo wp_kses_post( $section['before_section'] );
     1724            }
     1725        }
     1726
    17031727        if ( $section['title'] ) {
    17041728            echo "<h2>{$section['title']}</h2>\n";
     
    17151739        do_settings_fields( $page, $section['id'] );
    17161740        echo '</table>';
     1741
     1742        if ( '' !== $section['after_section'] ) {
     1743            echo wp_kses_post( $section['after_section'] );
     1744        }
    17171745    }
    17181746}
Note: See TracChangeset for help on using the changeset viewer.