WordPress.org

Make WordPress Core

Changeset 8855


Ignore:
Timestamp:
09/09/2008 10:31:22 PM (10 years ago)
Author:
ryan
Message:

add_settings_section() and add_settings_field(). see #7682

Location:
trunk/wp-admin
Files:
8 edited

Legend:

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

    r8845 r8855  
    15081508}
    15091509
    1510 // crazyhorse - this can be made simpler
    15111510function do_meta_boxes($page, $context, $object) {
    15121511    global $wp_meta_boxes;
     
    16031602    }
    16041603}
     1604
     1605/**
     1606 * Add a new section to a settings page
     1607 *
     1608 * @since 2.7
     1609 *
     1610 * @param string $id String for use in the 'id' attribute of tags.
     1611 * @param string $title Title of the section
     1612 * @param string $callback Function that fills the section with the desired content.  The function should echo its output.
     1613 * @param string $page The type of settings page on which to show the section (general, reading, writing, ...)
     1614 */
     1615function add_settings_section($id, $title, $callback, $page) {
     1616    global $wp_settings_sections;
     1617
     1618    if  ( !isset($wp_settings_sections) )
     1619        $wp_settings_sections = array();
     1620    if ( !isset($wp_settings_sections[$page]) )
     1621        $wp_settings_sections[$page] = array();
     1622    if ( !isset($wp_settings_sections[$page][$id]) )
     1623        $wp_settings_sections[$page][$id] = array();
     1624
     1625    $wp_settings_sections[$page][$id] = array('id' => $id, 'title' => $title, 'callback' => $callback);
     1626}
     1627
     1628/**
     1629 * Add a new field to a settings page
     1630 *
     1631 * @since 2.7
     1632 *
     1633 * @param string $id String for use in the 'id' attribute of tags.
     1634 * @param string $title Title of the field
     1635 * @param string $callback Function that fills the field with the desired content.  The function should echo its output.
     1636 * @param string $page The type of settings page on which to show the field (general, reading, writing, ...)
     1637 * @param string $section The section of the settingss page in which to show the box (default, ...)
     1638 * @param array $args Additional arguments
     1639 */
     1640function add_settings_field($id, $title, $callback, $page, $section = 'default', $args = array()) {
     1641    global $wp_settings_fields;
     1642
     1643    if  ( !isset($wp_settings_fields) )
     1644        $wp_settings_fields = array();
     1645    if ( !isset($wp_settings_fields[$page]) )
     1646        $wp_settings_fields[$page] = array();
     1647    if ( !isset($wp_settings_fields[$page][$section]) )
     1648        $wp_settings_fields[$page][$section] = array();
     1649
     1650    $wp_settings_fields[$page][$section][$id] = array('id' => $id, 'title' => $title, 'callback' => $callback, 'args' => $args);
     1651}
     1652
     1653function do_settings_sections($page) {
     1654    global $wp_settings_sections, $wp_settings_fields;
     1655
     1656    if ( !isset($wp_settings_sections) || !isset($wp_settings_sections[$page]) )
     1657        return;
     1658
     1659    foreach ( (array) $wp_settings_sections[$page] as $section ) {
     1660        echo "<h3>{$section['title']}</h3>\n";
     1661        call_user_func($section['callback'], $section);
     1662        if ( !isset($wp_settings_fields) || !isset($wp_settings_fields[$page]) || !isset($wp_settings_fields[$page][$section['id']]) )
     1663            continue;
     1664        echo '<table class="form-table">';
     1665        do_settings_fields($page, $section['id']);
     1666        echo '</table>';
     1667    }
     1668}
     1669
     1670function do_settings_fields($page, $section) {
     1671    global $wp_settings_fields;
     1672
     1673    if ( !isset($wp_settings_fields) || !isset($wp_settings_fields[$page]) || !isset($wp_settings_fields[$page][$section]) )
     1674        return;
     1675
     1676    foreach ( (array) $wp_settings_fields[$page][$section] as $field ) {
     1677        echo '<tr valign="top">';
     1678        if ( !empty($field['args']['label_for']) )
     1679            echo '<th scope="row"><label for="' . $field['args']['label_for'] . '">' . $field['title'] . '</label></th>';
     1680        else
     1681            echo '<th scope="row">' . $field['title'] . '</th>';
     1682        echo '<td>';
     1683        call_user_func($field['callback']);
     1684        echo '</td>';
     1685        echo '</tr>';
     1686    }   
     1687}
     1688
    16051689?>
  • trunk/wp-admin/options-discussion.php

    r8802 r8855  
    8484</fieldset></td>
    8585</tr>
     86<?php do_settings_fields('discussion', 'default'); ?>
    8687</table>
    8788
     
    155156</fieldset></td>
    156157</tr>
    157 
     158<?php do_settings_fields('discussion', 'avatars'); ?>
    158159</table>
    159160
     161<?php do_settings_sections('discussion'); ?>
    160162
    161163<p class="submit">
  • trunk/wp-admin/options-general.php

    r8802 r8855  
    118118</select></td>
    119119</tr>
     120<?php do_settings_fields('general', 'default'); ?>
    120121</table>
     122
     123<?php do_settings_sections('general'); ?>
    121124
    122125<p class="submit"><input type="submit" name="Submit" value="<?php _e('Save Changes') ?>" />
  • trunk/wp-admin/options-misc.php

    r8831 r8855  
    4646</th>
    4747</tr>
     48<?php do_settings_fields('misc', 'default'); ?>
    4849</table>
    4950
     
    131132</fieldset></td>
    132133</tr>
    133 
     134<?php do_settings_fields('misc', 'images'); ?>
    134135</table>
    135136
     
    158159</table>
    159160
     161<?php do_settings_sections('misc'); ?>
     162
    160163<p class="submit">
    161164<input type="hidden" name="action" value="update" />
  • trunk/wp-admin/options-permalink.php

    r8705 r8855  
    192192        <td><input name="tag_base" id="tag_base" type="text" class="code"  value="<?php echo attribute_escape($tag_base); ?>" size="30" /></td>
    193193    </tr>
     194    <?php do_settings_fields('permalink', 'optional'); ?>
    194195</table>
     196
     197<?php do_settings_sections('permalink'); ?>
     198
    195199<p class="submit"><input type="submit" name="submit" class="button" value="<?php _e('Save Changes') ?>" /></p>
    196200  </form>
  • trunk/wp-admin/options-privacy.php

    r8802 r8855  
    3232</fieldset></td>
    3333</tr>
     34<?php do_settings_fields('privacy', 'default'); ?>
    3435</table>
     36
     37<?php do_settings_sections('privacy'); ?>
    3538
    3639<p class="submit"><input type="submit" name="Submit" value="<?php _e('Save Changes') ?>" />
  • trunk/wp-admin/options-reading.php

    r8802 r8855  
    7373<?php _e('The character encoding you write your blog in (UTF-8 is <a href="http://developer.apple.com/documentation/macos8/TextIntlSvcs/TextEncodingConversionManager/TEC1.5/TEC.b0.html">recommended</a>)') ?></td>
    7474</tr>
     75<?php do_settings_fields('reading', 'default'); ?>
    7576</table>
    7677<p class="submit">
  • trunk/wp-admin/options-writing.php

    r8802 r8855  
    6464</select></td>
    6565</tr>
     66<?php do_settings_fields('writing', 'default'); ?>
    6667</table>
    6768
     
    8485<?php _e('Enable the WordPress, Movable Type, MetaWeblog and Blogger XML-RPC publishing protocols.') ?></label><br />
    8586</fieldset></td>
    86 </tr></table>
     87</tr>
     88<?php do_settings_fields('writing', 'remote_publishing'); ?>
     89</table>
    8790
    8891<h3><?php _e('Post via e-mail') ?></h3>
     
    121124</select></td>
    122125</tr>
     126<?php do_settings_fields('writing', 'post_via_email'); ?>
    123127</table>
    124128
     
    137141<?php endif; ?>
    138142
     143<?php do_settings_sections('writing'); ?>
     144
    139145<p class="submit">
    140146<input type="hidden" name="action" value="update" />
Note: See TracChangeset for help on using the changeset viewer.