Skip to content

Instantly share code, notes, and snippets.

@danielbrinneman
Forked from kovshenin/plugin.php
Created August 27, 2020 03:27
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 danielbrinneman/f8c2eb3df77a58353fd82aae9b128e1b to your computer and use it in GitHub Desktop.
Save danielbrinneman/f8c2eb3df77a58353fd82aae9b128e1b 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' />";
}
@danielbrinneman
Copy link
Author

Most helpful for setup of settings page in WordPress admin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment