WordPress.org

Make WordPress Core

Ticket #7682: 7682.diff

File 7682.diff, 4.0 KB (added by ryan, 6 years ago)

add_settings_section(), add_settings_field

  • wp-admin/includes/template.php

     
    16021602                } 
    16031603        } 
    16041604} 
     1605 
     1606function add_settings_section($id, $title, $callback, $page) { 
     1607        global $wp_settings_sections; 
     1608 
     1609        if  ( !isset($wp_settings_sections) ) 
     1610                $wp_settings_sections = array(); 
     1611        if ( !isset($wp_settings_sections[$page]) ) 
     1612                $wp_settings_sections[$page] = array(); 
     1613        if ( !isset($wp_settings_sections[$page][$id]) ) 
     1614                $wp_settings_sections[$page][$id] = array(); 
     1615 
     1616        $wp_settings_sections[$page][$id] = array('id' => $id, 'title' => $title, 'callback' => $callback); 
     1617} 
     1618 
     1619function add_settings_field($id, $title, $callback, $page, $section = 'default', $args = array()) { 
     1620        global $wp_settings_fields; 
     1621 
     1622        if  ( !isset($wp_settings_fields) ) 
     1623                $wp_settings_fields = array(); 
     1624        if ( !isset($wp_settings_fields[$page]) ) 
     1625                $wp_settings_fields[$page] = array(); 
     1626        if ( !isset($wp_settings_fields[$page][$section]) ) 
     1627                $wp_settings_fields[$page][$section] = array(); 
     1628 
     1629        $wp_settings_fields[$page][$section][$id] = array('id' => $id, 'title' => $title, 'callback' => $callback, 'args' => $args); 
     1630} 
     1631 
     1632function do_settings_sections($page) { 
     1633        global $wp_settings_sections, $wp_settings_fields; 
     1634 
     1635        if ( !isset($wp_settings_sections) || !isset($wp_settings_sections[$page]) ) 
     1636                return; 
     1637 
     1638        foreach ( (array) $wp_settings_sections[$page] as $section ) { 
     1639                echo "<h3>{$section['title']}</h3>\n"; 
     1640                call_user_func($section['callback'], $section); 
     1641                if ( !isset($wp_settings_fields) || !isset($wp_settings_fields[$page]) || !isset($wp_settings_fields[$page][$section['id']]) ) 
     1642                        continue; 
     1643                echo '<table class="form-table">'; 
     1644                do_settings_fields($page, $section['id']); 
     1645                echo '</table>'; 
     1646        } 
     1647} 
     1648 
     1649function do_settings_fields($page, $section) { 
     1650        global $wp_settings_fields; 
     1651 
     1652        if ( !isset($wp_settings_fields) || !isset($wp_settings_fields[$page]) || !isset($wp_settings_fields[$page][$section]) ) 
     1653                return; 
     1654 
     1655        foreach ( (array) $wp_settings_fields[$page][$section] as $field ) { 
     1656                echo '<tr valign="top">'; 
     1657                if ( !empty($field['args']['label_for']) ) 
     1658                        echo '<th scope="row"><label for="' . $field['args']['label_for'] . '">' . $field['title'] . '</label></th>'; 
     1659                else 
     1660                        echo '<th scope="row">' . $field['title'] . '</th>'; 
     1661                echo '<td>'; 
     1662                call_user_func($field['callback']); 
     1663                echo '</td>'; 
     1664                echo '</tr>'; 
     1665        }        
     1666} 
     1667 
    16051668?> 
  • wp-admin/options-writing.php

     
    6363?> 
    6464</select></td> 
    6565</tr> 
     66<?php do_settings_fields('writing', 'default'); ?> 
    6667</table> 
    6768 
    6869<h3><?php _e('Remote Publishing') ?></h3> 
     
    8384<input name="enable_xmlrpc" type="checkbox" id="enable_xmlrpc" value="1" <?php checked('1', get_option('enable_xmlrpc')); ?> /> 
    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> 
    8992<p><?php printf(__('To post to WordPress by e-mail you must set up a secret e-mail account with POP3 access. Any mail received at this address will be posted, so it&#8217;s a good idea to keep this address very secret. Here are three random strings you could use: <code>%s</code>, <code>%s</code>, <code>%s</code>.'), wp_generate_password(8, false), wp_generate_password(8, false), wp_generate_password(8, false)) ?></p> 
     
    120123?> 
    121124</select></td> 
    122125</tr> 
     126<?php do_settings_fields('writing', 'post_via_email'); ?> 
    123127</table> 
    124128 
    125129<h3><?php _e('Update Services') ?></h3> 
     
    136140 
    137141<?php endif; ?> 
    138142 
     143<?php do_settings_sections('writing'); ?> 
     144 
    139145<p class="submit"> 
    140146<input type="hidden" name="action" value="update" /> 
    141147<input type="submit" name="Submit" value="<?php _e('Save Changes') ?>" />