Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!

problem with register_settings()

By: brassblogs on Jul 12th, 2012  |  syntax: PHP  |  size: 6.90 KB  |  views: 46  |  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. $options = get_option('bei_options');
  2.  
  3. add_action('admin_menu', 'instructions_admin_add_options');                                                                     // start 'er up!
  4. function instructions_admin_add_options() {
  5.         add_options_page('Back End Instructions', 'Back End Instructions', 'manage_options', 'bei', 'bei_options_page');
  6. }
  7.  
  8.  
  9. function bei_options_page() {                                                                                                                           // the actual page contents ?>
  10. <div class="wrap">
  11.         <div id="icon-options-general" class="icon32"><br /></div><h2>Back End Instructions</h2>
  12.         <p>There aren't too many default settings for the Back End Instructions, but it makes life easier to have them here.</p>
  13.         <form action="options.php" method="post">
  14.                 <?php settings_fields('bei_options'); ?>
  15.                 <?php do_settings_sections('bei'); ?>
  16.                 <p><input name="submit" type="submit" id="submit" class="button-primary" value="<?php esc_attr_e('Save Changes', 'bei_languages'); ?>" /></p>
  17.         </form>
  18. </div>
  19. <?php }
  20.  
  21.  
  22. add_action('admin_init', 'instructions_admin_init');
  23. function instructions_admin_init(){                                                                                                                     // the options settings
  24.         register_setting( 'bei_options', 'bei_options', 'bei_options_validate' );
  25.         add_settings_section('bei_main', '', 'bei_section_text', 'bei');
  26.         add_settings_field('bei_admin', 'Default Admin Level', 'bei_setting_string', 'bei', 'bei_main');
  27.         add_settings_field('bei_public', 'Show in front?', 'bei_setting_string_public', 'bei', 'bei_main');
  28.         add_settings_field('bei_registered', 'Logged-in users only?', 'bei_setting_string_private', 'bei', 'bei_main');
  29.         add_settings_field('bei_view', 'Default viewing level', 'bei_setting_string_view', 'bei', 'bei_main');
  30. }
  31.  
  32. function bei_section_text() {                                                                                                                           //nuthin' really.  Might use later.
  33. }
  34.  
  35. function bei_setting_string() {
  36.         global $options;
  37.        
  38.         echo __('<span class="description" style="display:block;">Choose the lowest level logged-in user to create/edit/delete Instructions.</span>', 'bei_languages');
  39.        
  40.         if(is_multisite()) {                                                                                                                                    // test that this is a multi-site install      
  41.           echo '<input id="bei_admin" name="bei_options[admin]" size="40" type="radio" value="manage_network" ' . (isset($options["admin"]) && $options["admin"] == "manage_network" ? 'checked="checked" ' : '') . '/> Super Administrator (for multi-site only)<br />';
  42.         }
  43.        
  44.         echo '<input id="bei_admin" name="bei_options[admin]" size="40" type="radio" value="activate_plugins" ' . (isset($options["admin"]) && $options["admin"] == "activate_plugins" ? 'checked="checked" ' : '') . '/> Administrator';
  45.         echo '<br /><input id="bei_admin" name="bei_options[admin]" size="40" type="radio" value="edit_others_posts" ' . (isset($options["admin"]) && $options["admin"] == "edit_others_posts" ? 'checked="checked" ' : '') . '/> Editor';
  46.         echo '<br /><input id="bei_admin" name="bei_options[admin]" size="40" type="radio" value="delete_published_posts" ' . (isset($options["admin"]) && $options["admin"] == "delete_published_posts" ? 'checked="checked" ' : '') . '/> Author';
  47. }
  48.  
  49. function bei_setting_string_public() {
  50.         global $options;
  51.         $permalink = get_option("home") . '/wp-admin/options-permalink.php';
  52.        
  53.         echo sprintf(__('<span class="description" style="display:block;">Check "yes" if you\'d like to make your instructions viewable on the front end of the site. <br /><strong>PLEASE NOTE</strong>: The first time you change this option, you WILL have to <a href="%1$s">re-save your permalink settings</a> for this to take effect.  You may not ever have to do it again, but if you find you have issues after swapping back and forth, then try resetting them again to see if it helps.</span>', 'bei_languages'), $permalink);
  54.        
  55.         if(!isset($options['public'])) $options['public'] = 'no';
  56.         echo '<input id="bei_public" name="bei_options[public]" size="40" type="radio" value="yes" ' . (isset($options["public"]) && $options["public"] == "yes" ? 'checked="checked" ' : '') . '/> Yes';
  57.         echo ' &nbsp; &nbsp; <input id="bei_public" name="bei_options[public]" size="40" type="radio" value="no" ' . (isset($options["public"]) && $options["public"] == "no" ? 'checked="checked" ' : '') . '/> No';
  58. }
  59.  
  60. function bei_setting_string_private() {
  61.         global $options;
  62.        
  63.         echo __('<span class="description" style="display:block;">Check "yes" if you\'d like to make front-end instructions visible only to logged-in users.<br /><strong>PLEASE NOTE</strong>: if you check "yes" ANYONE can see ALL of these instructions.  See the next option to help with that a bit.</span>', 'bei_languages');
  64.        
  65.         echo '<input id="bei_registered" name="bei_options[registered]" size="40" type="radio" value="yes" ' . (isset($options["registered"]) && $options["registered"] == "yes" ? 'checked="checked" ' : '') . '/> Yes';
  66.         echo ' &nbsp; &nbsp; <input id="bei_registered" name="bei_options[registered]" size="40" type="radio" value="no" ' . (isset($options["registered"]) && $options["registered"] == "no" ? 'checked="checked" ' : '') . '/> No';
  67. }      
  68.  
  69. function bei_setting_string_view() {
  70.         global $options;
  71.        
  72.         echo __('<span class="description" style="display:block;">You only need to choose an option from this dropdown if you set "Show in front?" to "yes" AND "Logged-in users only?" to "no".  If this option were not here, then ANY visitor to the site could see ALL instructions just by visiting the page.  If the user is logged in, they would see only instructions that were available to their level, but if they aren\'t, they would see them for ALL levels.  This option will allow you to treat a non-logged-in user as if they have a user level.  The default is "Contributor."</span>', 'bei_languages');
  73.        
  74.         // setup array
  75.         $choices = array();
  76.        
  77.         if(is_multisite()) {                                                                                                                                    // test that this is a multi-site install      
  78.           $choices['Super Administrator'] = 'manage_networks';
  79.         }
  80.        
  81.         $choices['Administrator'] = 'activate_plugins';
  82.         $choices['Editor'] = 'edit_others_posts';
  83.         $choices['Author'] = 'delete_published_posts';
  84.         $choices['Contributor'] = 'delete_posts';
  85.         $choices['Subscriber'] = 'read';
  86.                                
  87.         echo '<p><select id="bei_view" name="name="bei_options[view]"></p>' . "\n";
  88.  
  89.         if (isset($options["view"])) {
  90.                 foreach($choices as $key => $value) {
  91.                         if($options["view"] == $value)
  92.                           echo '<option value="' . $value . '" selected>'. $key .'</option>' . "\n";
  93.                 }
  94.         } else {
  95.                           echo '<option value="delete_posts" selected>Contributor</option>' . "\n";
  96.         }
  97.                
  98.         echo '<option value="">-------------------</option>' . "\n";
  99.                
  100.         foreach($choices as $key => $value) {
  101.                 echo '<option value="' . $value . '">' . $key .'</option>' . "\n";
  102.         }      
  103.        
  104.         echo '</select>' . "\n";       
  105. }
  106.  
  107. function bei_options_validate($input) {
  108.         isset($input['admin']) ? $newinput['admin'] = trim($input['admin']) : $newinput['admin'] = '';
  109.         isset($input['public']) ? $newinput['public'] = trim($input['public']) : $newinput['public'] = '';
  110.         isset($input['registered']) ? $newinput['registered'] = trim($input['registered']) : $newinput['registered'] = '';
  111.         isset($input['view']) ? $newinput['view'] = trim($input['view']) : $newinput['view'] = '';
  112.         return $newinput;
  113. }
clone this paste RAW Paste Data