Last active
December 30, 2015 11:09
-
-
Save ahmad24/7820621 to your computer and use it in GitHub Desktop.
wordpress : Settings API
This file contains hidden or 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: Settings API example | |
Plugin URI: http://example.com/ | |
Description: A complete and practical example of use of the Settings API | |
Author: WROX | |
Author URI: http://wrox.com | |
*/ | |
// Add a menu for our option page | |
add_action('admin_menu', 'boj_myplugin_add_page'); | |
function boj_myplugin_add_page() { | |
add_options_page( 'My Plugin', 'My Plugin', 'manage_options', | |
'boj_myplugin', 'boj_myplugin_option_page' | |
); | |
} | |
// Draw the option page | |
function boj_myplugin_option_page() { | |
?> | |
<div class="wrap"> | |
<?php screen_icon(); ?> | |
<h2>My plugin</h2> | |
<form action="options.php" method="post"> | |
<?php settings_fields('boj_myplugin_options'); ?> | |
<?php do_settings_sections('boj_myplugin'); ?> | |
<input name="Submit" type="submit" value="Save Changes"/> | |
</form> | |
</div> | |
<?php | |
} | |
// Register and define the settings | |
add_action('admin_init', 'boj_myplugin_admin_init'); | |
function boj_myplugin_admin_init(){ | |
register_setting( 'boj_myplugin_options', 'boj_myplugin_options', | |
'boj_myplugin_validate_options' ); | |
add_settings_section( 'boj_myplugin_main', 'My Plugin Settings', | |
'boj_myplugin_section_text', 'boj_myplugin' ); | |
add_settings_field( 'boj_myplugin_text_string', 'Enter text here', | |
'boj_myplugin_setting_input', 'boj_myplugin', 'boj_myplugin_main' ); | |
} | |
// Draw the section header | |
function boj_myplugin_section_text() { | |
echo '<p>Enter your settings here.</p>'; | |
} | |
// Display and fill the form field | |
function boj_myplugin_setting_input() { | |
// get option 'text_string' value from the database | |
$options = get_option( 'boj_myplugin_options' ); | |
$text_string = $options['text_string']; | |
// echo the field | |
echo "<input id='text_string' name='boj_myplugin_options[text_string]' type='text' value='$text_string'/>"; | |
} | |
// Validate user input (we want text only) | |
function boj_myplugin_validate_options( $input ) { | |
$valid = array(); | |
$valid['text_string'] = preg_replace( | |
'/[^a-zA-Z]/', | |
'', | |
$input['text_string'] ); | |
if( $valid['text_string'] != $input['text_string'] ) { | |
add_settings_error( | |
'boj_myplugin_text_string', | |
'boj_myplugin_texterror', | |
'Incorrect value entered!', | |
'error' | |
); | |
} | |
return $valid; | |
}?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment