为了处理插件选项,我假设您创建了某种选项页面?
下面是一个在选项页面中创建复选框设置的示例,包括验证和其他所有内容。只需创建一个新的“myplugin admin.php”文件并插入代码。只有一个设置:从wp区域移除垃圾。然后将管理文件包含在主插件php文件中(例如myplugin.php)->包含“myplugin admin.php”;
<?php
// SETUP THE MENU
function myplugin_setup_menu(){
add_options_page( \'Settings\', \'MyPlugin\', \'manage_options\', __FILE__, \'myplugin_build_options_page\' );
}
// BUILD OPTIONS PAGE
function myplugin_build_options_page(){ ?>
<div id="myplugin" class="wrap">
<div id="icon-options-general" class="icon32"></div>
<h2 class="options-title">MyPlugin <?php _e("Settings"); ?></h2>
<form method="post" action="options.php" class="options-form">
<div id="poststuff">
<div id="post-body" class="metabox-holder columns-2">
<!-- main content -->
<div id="post-body-content">
<div class="meta-box-sortables ui-sortable">
<div class="postbox">
<h3 class="hndle"><span><?php _e("General"); ?> <?php _e("Settings"); ?></span></h3>
<div class="inside">
<?php settings_fields(\'myplugin_options\'); ?>
<?php do_settings_sections(__FILE__); ?>
</div><!-- .inside -->
</div><!-- .postbox -->
</div><!-- .meta-box-sortables .ui-sortable -->
</div><!-- post-body-content -->
<!-- sidebar -->
<div id="postbox-container-1" class="postbox-container">
<div class="meta-box-sortables">
<div class="postbox">
<div class="inside">
<p><?php _e("Edit MyPLugin Settings"); ?></p>
</div><!-- .inside -->
</div><!-- .postbox -->
</div><!-- .meta-box-sortables -->
</div><!-- #postbox-container-1 .postbox-container -->
</div><!-- #post-body .metabox-holder .columns-2 -->
<br class="clear">
<?php myplugin_submit(); ?>
</div><!-- #poststuff -->
</form><!-- /.options-form -->
</div><!-- .wrap -->
<?php }
/* ================================================================ *\\
BUILD AND REGISTER OPTIONS
\\* ================================================================
register_setting( $option_group, $option_name, $sanitize_callback );
$option_group: a string representing the name of the settings group
$option_name: the name of the option
$sanitize_callback: a callback function that can handle any specific operations or sanitizing
add_settings_section( $id, $title, $callback, $page );
$id: a unique ID for the section
$title: a heading that will be displayed above the fields on the page
$callback: can handle the creation of the section; if it’s declared, you must create the function, or an error will be thrown
$page: defines the type of settings page that this section should be applied to; in our case, it should apply to our custom page, so we used __FILE__ as the name
add_settings_field( $id, $title, $callback, $page, $section, $args );
$id: a unique ID for the field
$title: the title before the field
$callback: handles the displaying of the field
$page: it should apply to our custom page, so we used __FILE__ as the name
$section: set the section the the field belongs to
$args: ka
*/
function myplugin_build_options () {
$options = get_option(\'lmyplugin_options\');
if(!isset($options)) {
//not present, so add
$options = array(
\'remove_junk\' => 1
);
add_option(\'myplugin_options\', $options);
}
// Register Settings
register_setting(\'myplugin_options\', \'myplugin_options\', \'myplugin_validate_settings\');
// Add Sections
add_settings_section(\'myplugin_section_functions\', __("Functions", "myplugin"), \'myplugin_section_functions_cb\', __FILE__);
/*
ADD FIELDS TO SECTION
*/
// Remove Junk
add_settings_field(\'remove_junk\', __(\'Remove Junk from Head\', \'myplugin\'), \'myplugin_setting_remove_junk\', __FILE__, \'myplugin_section_functions\');
}
// VALIDATE SETTINGS
function myplugin_validate_settings($options) {
return $options;
}
// SETTINGS SECTION CLLBACK
function myplugin_section_functions_cb() {}
/* ================================================================ *\\
BUILD SETTINGS FIELDS
\\* ================================================================ */
// Remove Junk
function myplugin_setting_remove_junk() {
$options = get_option(\'myplugin_options\'); ?>
<fieldset>
<legend class="screen-reader-text"><span><?php _e("Remove Junk from <head>", \'myplugin\'); ?></span></legend>
<label for="remove_junk">
<input name="myplugin_options[remove_junk]" type="checkbox" id="remove_junk" value="1" <?php checked($options[\'remove_junk\'], 1, true); ?> />
<span><?php _e( \'Remove junk information from wordpress <head> area.\', \'myplugin\' ); ?></span>
</label>
</fieldset>
<?php }
/* ================================================================ *\\
MISC
\\* ================================================================ */
// SUBMIT BUTTON
function myplugin_submit() { ?>
<p class="submit"><input name="Submit" type="submit" class="button button-primary" value="<?php _e(\'Save Changes\'); ?>" /><!-- /#.button-primary --></p><!-- /.submit -->
<?php }
// ADD STYLESHEET
function myplugin_register_head() {
$url = plugins_url(\'css/admin-style.css\', __FILE__ );
echo \'<link rel="stylesheet" href="\'.$url.\'" />\';
}
// ADD ACTIONS
add_action(\'admin_head\', \'myplugin_register_head\');
add_action(\'admin_init\', \'myplugin_build_options\');
add_action(\'admin_menu\', \'myplugin_setup_menu\');
?>
试试吧,我希望我能帮助你。