Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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