Want more features on Pastebin? Sign Up, it's FREE!
Guest

Wordpress Settings

By: gtichy on Oct 24th, 2012  |  syntax: PHP  |  size: 2.35 KB  |  views: 122  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //ADD Settings page for Announcements
  2. add_action('admin_menu', 'plugin_admin_add_page');
  3. function plugin_admin_add_page() {
  4. add_options_page('Announcements Settings', 'Announcements', 'manage_options', 'simple_announce', 'plugin_options_page');
  5. }
  6.  
  7. //Display the admin options page
  8. function plugin_options_page(){
  9. ?>
  10.         <div>
  11.                 <h2>Simple Announcements</h2>
  12.                 <p>Announcements Settings Description Goes Here</p>
  13.                 <form action="options.php" method="post">
  14.                 <?php settings_fields('simple_options'); ?>
  15.                 <?php do_settings_sections('simple_announce'); ?>
  16.                
  17.                 <input name="Submit" type="submit" value="<?php esc_attr_e('Save Changes');?>" />
  18.                 </form>
  19.         </div>
  20.                
  21. <?php  
  22. }
  23.  
  24. //Define the settings
  25. add_action('admin_init', 'plugin_admin_init');
  26. function plugin_admin_init(){
  27. add_settings_section('simple_main', 'Main Settings', 'simple_section_text', 'simple_announce');
  28. register_setting( 'simple_options', 'background_color', 'simple_validate_background' );
  29. register_setting( 'simple_options', 'text_color', 'simple_validate_text' );
  30. add_settings_field('simple_background_color', 'Background Color:', 'plugin_background_string', 'simple_announce', 'simple_main');
  31. add_settings_field('simple_text_color', 'Text Color:', 'plugin_text_string', 'simple_announce', 'simple_main');
  32.  
  33. }
  34.  
  35. //Settings Section text
  36. function simple_section_text() {
  37. echo '<p>Main description of this section here.</p>';
  38. }
  39.  
  40. //Settings
  41. function plugin_background_string() {
  42. $options = get_option('background_color');
  43. echo "<input id='simple_background_color' name='simple_options[background_color]' size='40' type='text' value='$options' />";
  44.  
  45. }
  46.  
  47. function plugin_text_string() {
  48. $options = get_option('text_color');
  49. echo "<input id='simple_text_color' name='simple_options[text_color]' size='40' type='text' value='$options' />";
  50.  
  51. }
  52.  
  53. //Validate Options
  54. function simple_validate_background($input) {
  55. $options = get_option('background_color');
  56. $options['background_color'] = trim($input['background_color']);
  57. if(!preg_match('/^[a-z0-9]{32}$/i', $options['background_color'])) {
  58. $options['background_color'] = '';
  59. }
  60. return $options;
  61. }
  62.  
  63. function simple_validate_text($input) {
  64. $options = get_option('text_color');
  65. $options['text_color'] = trim($input['text_color']);
  66. if(!preg_match('/^[a-z0-9]{32}$/i', $options['text_color'])) {
  67. $options['text_color'] = '';
  68. }
  69. return $options;
  70. }
clone this paste RAW Paste Data