Skip to content

Instantly share code, notes, and snippets.

@hansschuijff
Forked from kovshenin/plugin.php
Created December 4, 2023 21:29
Show Gist options
  • Save hansschuijff/40db3d6300e10514b3ae4f9ffe1ef573 to your computer and use it in GitHub Desktop.
Save hansschuijff/40db3d6300e10514b3ae4f9ffe1ef573 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