Advertisement
Guest User

Untitled

a guest
Nov 5th, 2010
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.30 KB | None | 0 0
  1. <?php
  2. class optionObject{
  3.     var $option_group ="extra_options";//Group Slug for settings whitelist
  4.     var $option_prefix = "extra_";//name prefix set
  5.     var $page_title = "Extra Settings";//Page Title
  6.     var $menu_page_title = "Extra Settings";//Settings Page Name for sidemenu
  7.     var $user_level = 'manage_options';//set minimum user access level
  8.     var $menu_slug = "extra_settings";//ID for top level menu
  9.     var $options = array(
  10.                         array("title"=>"Extra 1","slug"=>"extra1","help"=>"A little extra something")
  11.                         );
  12.                         /*
  13.                         * Multi Dimensional Array, requires title, slug and help (all strings) for each
  14.                         * required option  
  15.                         */
  16.        
  17.    
  18.     function __construct($option_group = false, $option_prefix = false){
  19.        
  20.         if(!$option_group) $option_group = $this->option_group;
  21.         $this->option_group =$option_group;
  22.         if(!$option_prefix) $option_prefix = $this->option_prefix;
  23.         $this->option_prefix =$option_prefix;
  24.        
  25.        
  26.         register_setting( $this->option_group, $this->option_prefix, array(&$this, 'validate') );
  27.        
  28.     }
  29.    
  30.     function init(){
  31.  
  32.         register_setting( $this->option_group, $this->option_prefix, array(&$this, 'validate') );
  33.        
  34.     }
  35.  
  36.     function add_page() {
  37.        
  38.         add_options_page($this->menu_page_title, $this->page_title, $this->user_level, $this->menu_slug, array(&$this, 'do_page'));
  39.  
  40.     }
  41.  
  42.     function option_rows($options = false){
  43.         /* $options =
  44.         * Multi Dimensional Array, requires title, slug and help (all strings) for each
  45.         * required option  
  46.         */
  47.        
  48.         if(!$options) $options = $this->options;
  49.        
  50.        
  51.         foreach ($options as $option):?>
  52.             <tr valign="top"><th scope="row"><?php echo $option['title']; ?></th>
  53.  
  54.                 <td><input type="text" name=<?php echo $this->option_name.'['.$option['slug'].']'?> value="<?php echo $options['text']; ?>" /><p><?php echo $option[$option['slug']]; ?></p></td>
  55.  
  56.             </tr>  
  57.         <?php endforeach;
  58.     }
  59.  
  60.     function do_page() {
  61.  
  62.         ?>
  63.  
  64.         <div class="wrap">
  65.  
  66.             <h2><?php echo $this->page_title;?></h2>
  67.  
  68.             <form method="post" action="options.php">
  69.  
  70.                 <?php settings_fields($this->option_group); ?>
  71.  
  72.                 <?php $options = get_option($this->option_name); ?>
  73.  
  74.                 <table class="form-table">
  75.  
  76.                 <?php $this->option_rows(); ?>
  77.                    
  78.                 </table>
  79.  
  80.                 <p class="submit">
  81.  
  82.                 <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
  83.  
  84.                 </p>
  85.  
  86.             </form>
  87.  
  88.         </div>
  89.  
  90.         <?php    
  91.  
  92.     }
  93.  
  94.     function validate($input) {
  95.  
  96.  
  97.  
  98.         // Say our option must be safe text with no HTML tags
  99.     foreach ($input as $k => $v){
  100.             $input[$k] =  wp_filter_nohtml_kses($input[$k]);
  101.     }
  102.  
  103.     return $input;
  104.  
  105.     }
  106.    
  107. }
  108.  
  109.  
  110.  
  111.  
  112. function test_options(){
  113.     $options = new optionObject("test_options","test_");
  114.     //$options->user_level = "read";
  115.     $options->page_title = "Test Settings";
  116.     $options->menu_page_title = $options->page_title;
  117.     $options->menu_slug = "test_settings";
  118.     $options->options = array(
  119.                                 array("title"=>"Test 1","slug"=>"test1","help"=>"Help with Test 1"),
  120.                                 array("title"=>"Test 2","slug"=>"test2","help"=>"Help with Test 2")
  121.     );
  122.    
  123.     $options->init();
  124.    
  125.    
  126.     add_action('admin_init', array($options, 'add_page'));
  127. }
  128. add_action('admin_menu', 'test_options' );
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement