add_settings_field()
最后更新于:2021-11-25 19:40:43
add_settings_field( string$id, string$title, callable$callback, string$page, string$section=’default’, array$args=array())Add a new field to a section of a settings page.
参数
- $id
-
(string) (Required) Slug-name to identify the field. Used in the ‘id’ attribute of tags.
- $title
-
(string) (Required) Formatted title of the field. Shown as the label for the field during output.
- $callback
-
(callable) (Required) Function that fills the field with the desired form inputs. The function should echo its output.
- $page
-
(string) (Required) The slug-name of the settings page on which to show the section (general, reading, writing, …).
- $section
-
(string) (Optional) The slug-name of the section of the settings page in which to show the box.
Default value: ‘default’
- $args
-
(array) (Optional) Extra arguments used when outputting the field.
-
‘label_for’
(string) When supplied, the setting title will be wrapped in a<label>
element, itsfor
attribute populated with this value. -
‘class’
(string) CSS Class to be added to the<tr>
element when the field is output.
Default value: array()
-
‘label_for’
源文件
文件: gc-admin/includes/template.php
function add_settings_field( $id, $title, $callback, $page, $section = 'default', $args = array() ) {
global $gc_settings_fields;
if ( 'misc' === $page ) {
_deprecated_argument(
__FUNCTION__,
'3.0.0',
sprintf(
/* translators: %s: misc */
__( 'The "%s" options group has been removed. Use another settings group.' ),
'misc'
)
);
$page = 'general';
}
if ( 'privacy' === $page ) {
_deprecated_argument(
__FUNCTION__,
'3.5.0',
sprintf(
/* translators: %s: privacy */
__( 'The "%s" options group has been removed. Use another settings group.' ),
'privacy'
)
);
$page = 'reading';
}
$gc_settings_fields[ $page ][ $section ][ $id ] = array(
'id' => $id,
'title' => $title,
'callback' => $callback,
'args' => $args,
);
}
/* **************** CHECKBOXES **************** */ // settings checkbox add_settings_field( 'gcdevref_removestyles_field', esc_attr__('Remove Plugin Styles', 'gcdevref'), 'gcdevref_removestyles_field_cb', 'gcdevref_options', 'gcdevref_options_section', array( 'type' => 'checkbox', 'option_group' => 'gcdevref_options', 'name' => 'gcdevref_removestyles_field', 'label_for' => 'gcdevref_removestyles_field', 'value' => (empty(get_option('gcdevref_options')['gcdevref_removestyles_field'])) ? 0 : get_option('unitizr_options')['gcdevref_removestyles_field'], 'description' => __( 'Check to remove preset plugin overrides.', 'gcdevref' ), 'checked' => (!isset(get_option('gcdevref_options')['gcdevref_removestyles_field'])) ? 0 : get_option('gcdevref_options')['gcdevref_removestyles_field'], // Used 0 in this case but will still return Boolean not[see notes below] 'tip' => esc_attr__( 'Use if plugin fields drastically changed when installing this plugin.', 'gcdevref' ) ) );
/** * switch for 'remove styles' field * @since 2.0.1 * @input type checkbox */ function gcdevref_removestyles_field_cb($args) { $checked = ''; $options = get_option($args['option_group']); $value = ( !isset( $options[$args['name']] ) ) ? null : $options[$args['name']]; if($value) { $checked = ' checked="checked" '; } // Could use ob_start. $html = ''; $html .= '<input id="' . esc_attr( $args['name'] ) . '" name="' . esc_attr( $args['option_group'] . '['.$args['name'].']') .'" type="checkbox" ' . $checked . '/>'; $html .= '<span class="wndspan">' . esc_html( $args['description'] ) .'</span>'; $html .= '<b class="wntip" data-title="'. esc_attr( $args['tip'] ) .'"> ? </b>'; echo $html; }
add_action('admin_init', 'your_function'); function your_function(){ add_settings_field( 'myprefix_setting-id', 'This is the setting title', 'myprefix_setting_callback_function', 'general', 'default', array( 'label_for' => 'myprefix_setting-id' ) ); } function myprefix_setting_callback_function($args){ echo 'Content here'; }
register_setting( 'mygroup', 'mynewcheckboxID' ); add_settings_field( 'mynewcheckboxID', 'My New Checkbox', 'callback_input_myid', 'myAdminPage', 'myAdminSection', [ 'label_for' => 'mynewcheckboxID' ] ); function callback_input_myid() { echo "<input type='checkbox' id='mynewcheckboxID' value='1'" if ( get_option('mynewcheckboxID') == '1' ) { echo ' checked'; } echo '/>'; }
class ClassName { public function __construct() { add_action( 'admin_init', array( $this, 'your_function' ) ); } function your_function() { add_settings_field( 'myprefix_setting-id', 'This is the setting title', array( $this, 'myprefix_setting_callback_function' ), 'general', 'default', array( 'label_for' => 'myprefix_setting-id' ), ); } function myprefix_setting_callback_function( $args ) { echo 'Content here'; } } $ClassName = new ClassName();