Skip to content

Instantly share code, notes, and snippets.

@grappler
Forked from kovshenin/plugin.php
Last active December 19, 2015 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grappler/5892119 to your computer and use it in GitHub Desktop.
Save grappler/5892119 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: My Plugin
* Plugin Description: Settings API Demo
* Text Domain: my-plugin
*/
add_action( 'admin_menu', 'my_admin_menu' );
function my_admin_menu() {
add_options_page(
__( 'My Plugin', 'my-plugin' ), // $page_title
__( 'My Plugin', 'my-plugin' ), // $menu_title
'manage_options', // $capability
'my-plugin', // $menu_slug
'my_options_page' // $function
);
}
function my_options_page() {
?>
<div class="wrap">
<?php screen_icon(); ?>
<h2><?php _e( 'My Plugin Options', 'my-plugin' ) ?></h2>
<form action="options.php" method="POST">
<?php settings_fields( 'my-settings-group' ); /* $option_group */ ?>
<?php do_settings_sections( 'my-plugin' ); /* $page */ ?>
<?php submit_button(); ?>
</form>
</div>
<?php
}
add_action( 'admin_init', 'my_admin_init' );
function my_admin_init() {
// Sections
add_settings_section(
'section-one', // $section_id
__( 'Section One', 'my-plugin' ), // $title
'section_one_callback', // $callback
'my-plugin' // $page
);
// Fields
add_settings_field(
'field-one', // $field_id
__( 'Field One', 'my-plugin' ), // $title
'field_one_callback', // $callback
'my-plugin', // $page
'section-one', // $section_id
array()
);
// Settings
register_setting(
'my-settings-group', // $option_group
'my-setting', // $option_name
'esc_attr' // $sanitize_callback
);
}
function section_one_callback() {
_e( 'Some help text goes here.', 'my-plugin' );
}
function field_one_callback() {
$setting_value = get_option( 'my-setting' ); // $option = $option_name
echo '<input class="regular-text" type="text" name="my-setting" value="' . esc_attr( $setting_value ) . '" />';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment