-
-
Save hansschuijff/40db3d6300e10514b3ae4f9ffe1ef573 to your computer and use it in GitHub Desktop.
Settings API Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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