Last active
December 11, 2015 13:59
-
-
Save karlazz/4611360 to your computer and use it in GitHub Desktop.
Use the add_settings and settings_field tags to create an options page. This is an example of a settings page from Otto on WordPress. I strung the code together from his post. I have not tried it yet. Some notes: seems like a lot of indirection to hide the fact that you are building a form : falls in the rewriting html category, since you have t…
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 // add the admin options page | |
add_action('admin_menu', 'plugin_admin_add_page'); | |
function plugin_admin_add_page() { | |
add_options_page('Custom Plugin Page', 'Custom Plugin Menu', 'manage_options', 'plugin', 'plugin_options_page'); | |
} | |
?> | |
<?php // display the admin options page | |
function plugin_options_page() { | |
?> | |
<div> | |
<h2>My custom plugin</h2> | |
Options relating to the Custom Plugin. | |
<form action="options.php" method="post"> | |
<?php settings_fields('plugin_options'); ?> | |
<?php do_settings_sections('plugin'); ?> | |
<input name="Submit" type="submit" value="<?php esc_attr_e('Save Changes'); ?>" /> | |
</form></div> | |
<?php | |
}?> | |
<?php // add the admin settings and such | |
add_action('admin_init', 'plugin_admin_init'); | |
function plugin_admin_init(){ | |
register_setting( 'plugin_options', 'plugin_options', 'plugin_options_validate' ); | |
add_settings_section('plugin_main', 'Main Settings', 'plugin_section_text', 'plugin'); | |
add_settings_field('plugin_text_string', 'Plugin Text Input', 'plugin_setting_string', 'plugin', 'plugin_main'); | |
}?> | |
<?php function plugin_setting_string() { | |
$options = get_option('plugin_options'); | |
echo "<input id='plugin_text_string' name='plugin_options[text_string]' size='40' type='text' value='{$options['text_string']}' />"; | |
} ?> | |
<?php // validate our options | |
function plugin_options_validate($input) { | |
$options = get_option('plugin_options'); | |
$options['text_string'] = trim($input['text_string']); | |
if(!preg_match('/^[a-z0-9]{32}$/i', $options['text_string'])) { | |
$options['text_string'] = ''; | |
} | |
return $options; | |
} | |
?> | |
<?php /* http://ottopress.com/2009/wordpress-settings-api-tutorial/ */ ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment