Skip to content

Instantly share code, notes, and snippets.

@kovshenin
Created October 26, 2012 07:55
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save kovshenin/3957517 to your computer and use it in GitHub Desktop.
Save kovshenin/3957517 to your computer and use it in GitHub Desktop.
Settings API Demo
<?php
/**
* Plugin Name: My Plugin
* Plugin Description: Settings API Demo
*/
add_action( 'admin_menu', 'my_admin_menu' );
function my_admin_menu() {
add_options_page( 'My Plugin', 'My Plugin', 'manage_options', 'my-plugin', 'my_options_page' );
}
function my_options_page() {
?>
<div class="wrap">
<?php screen_icon(); ?>
<h2>My Plugin Options</h2>
<form action="options.php" method="POST">
<?php settings_fields( 'my-settings-group' ); ?>
<?php do_settings_sections( 'my-plugin' ); ?>
<?php submit_button(); ?>
</form>
</div>
<?php
}
add_action( 'admin_init', 'my_admin_init' );
function my_admin_init() {
register_setting( 'my-settings-group', 'my-setting' );
// Sections
add_settings_section( 'section-one', 'Section One', 'section_one_callback', 'my-plugin' );
// Fields
add_settings_field( 'field-one', 'Field One', 'field_one_callback', 'my-plugin', 'section-one' );
}
function section_one_callback() {
echo "Some help text goes here.";
}
function field_one_callback() {
$setting_value = esc_attr( get_option( 'my-setting' ) );
echo "<input class='regular-text' type='text' name='my-setting' value='$setting_value' />";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment