Make WordPress Core

Changeset 54247

Timestamp:
09/20/2022 09:57:43 AM (23 months 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.

Location:
trunk
Files:
2 edited

Legend:

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

    r54071 r54247  
    15621562 *
    15631563 * @since 2.7.0
     1564
    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
     1587
     1588
     1589
     1590
     1591
     1592
     1593
     1594
     1595
    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
     1720
     1721
     1722
     1723
     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
     1743
     1744
    17171745    }
    17181746}
  • trunk/tests/phpunit/tests/template.php

    r51568 r54247  
    457457    }
    458458
     459
     460
     461
     462
     463
     464
     465
     466
     467
     468
     469
     470
     471
     472
     473
     474
     475
     476
     477
     478
     479
     480
     481
     482
     483
     484
     485
     486
     487
     488
     489
     490
     491
     492
     493
     494
     495
     496
     497
     498
     499
     500
     501
     502
     503
     504
     505
     506
     507
     508
     509
     510
     511
     512
     513
     514
     515
     516
     517
     518
     519
     520
     521
     522
     523
     524
     525
     526
     527
     528
     529
     530
     531
     532
     533
     534
     535
     536
     537
     538
     539
     540
     541
     542
     543
     544
     545
     546
     547
     548
     549
     550
     551
     552
     553
     554
     555
     556
     557
     558
     559
     560
     561
     562
     563
     564
     565
     566
     567
     568
     569
     570
     571
     572
     573
     574
     575
     576
     577
     578
     579
     580
     581
     582
     583
     584
     585
     586
     587
     588
     589
     590
     591
     592
     593
     594
     595
     596
     597
     598
     599
     600
     601
     602
     603
     604
     605
     606
     607
     608
     609
     610
     611
     612
     613
     614
     615
     616
     617
     618
     619
     620
     621
     622
     623
     624
     625
     626
     627
     628
     629
     630
    459631
    460632    public function assertTemplateHierarchy( $url, array $expected, $message = '' ) {
Note: See TracChangeset for help on using the changeset viewer.