Advertisement
Guest User

WP Form Demo

a guest
Sep 12th, 2011
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.08 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: WP Settings Form Demo
  4. Plugin URI: http://www.webarnes.ca/wpform
  5. Description: Simplifies making a settings page
  6. Version: 0.99
  7. Author: William Barnes
  8. Author URI: http://www.webarnes.ca
  9. License: GPL2
  10. */
  11.  
  12. // WORDPRESS HOOKS
  13.  
  14. require_once('class.wpform.php');
  15. $WPFD = new WP_Form_Demo();
  16.  
  17. class WP_Form_Demo
  18. {
  19.     function __construct()
  20.     {
  21.         add_action('admin_init',array($this, 'admin_init'));
  22.         add_action('admin_menu',array($this, 'admin_menu'));
  23.        
  24.         $this->simple_settings = array(
  25.             "option_name" => "wpdemo_simple",
  26.             "validator" => array($this,"simple_validator"),
  27.             "sections" => array(
  28.                 array(
  29.                     "title" => "Simple Settings Form",
  30.                     "text" => "<p>This is a simple form.</p>",
  31.                     "fields" => array(
  32.                         array(
  33.                             "name" => "title",
  34.                             "label" => "Title",
  35.                             "type" => "text",
  36.                             "size" => 60
  37.                         ),
  38.                         array(
  39.                             "name" => "tags",
  40.                             "label" => "Tags",
  41.                             "description" => "Comma-separated list of tags that will be applied to each post",
  42.                             "type" => "text", // Boolean, dropdown, text, categories, authors
  43.                             "validator" => array(
  44.                                 "type" => "preg_replace",
  45.                                 "pattern" =>"|[^a-zA-Z0-9, _-]|",
  46.                                 "replacement" => ""
  47.                             )
  48.                         ),
  49.                         array(
  50.                             "name" => "categories",
  51.                             "label" => "Categories",
  52.                             "type" => "categories", // Boolean, select, categories, tags, maybe
  53.                             "validator" => array(
  54.                                 "type" => "numeric"
  55.                             )
  56.                         ),
  57.                         array(
  58.                             "name" => "author",
  59.                             "label" => "Author",
  60.                             "type" => "authors", // Boolean, select, categories, tags, maybe
  61.                             "validator" => array(
  62.                                 "type" => "numeric"
  63.                             )
  64.                         )
  65.                     )
  66.                 )
  67.             )
  68.         );
  69.        
  70.         $this->multi_settings = array(
  71.             "option_name" => "wpdemo_multi",
  72.             "multi" => array(
  73.                 "list_title" => "Rows",
  74.                 "new_title" => "Add a new row",
  75.                 /* "new_fields" replaces the "Add a new row" button with a form.
  76.                  * You can use the values in that form to initialize the main
  77.                  * form or add sections based on the choice.
  78.                  */
  79.                 /*"new_fields" => array(
  80.                     array(
  81.                         "name" => "type",
  82.                         "label" => "Row Type",
  83.                         "type" => "dropdown"
  84.                         "options" => array(
  85.                             "1" => "One",
  86.                             "2" => "Two",
  87.                         )
  88.                     )
  89.                 ),*/
  90.                 "table_fields" => array(
  91.                     array('name'=>'title','label'=>'Title','link'=>true),
  92.                     array('name'=>'timestamp','type'=>'timestamp','label'=>'Timestamp','width'=>'250px'),
  93.                     array('name'=>'enabled','label'=>'Enabled','width'=>'50px')
  94.                 ),
  95.                 // Example multi-row action buttons, provide a callback as below
  96.                 "table_buttons" => array(
  97.                     "pause" => array('label'=>'Disable','callback'=>array($this,'on_pause')),
  98.                     "unpause" => array('label'=>'Enable','callback'=>array($this,'on_unpause'))
  99.                 )
  100.             ),
  101.             "defaults" => array(
  102.                 "enabled" => 1,
  103.                 "timestamp" => time()
  104.             ),
  105.             "sections" => array(
  106.                 array(
  107.                     "title" => "First Section",
  108.                     "fields" => array(
  109.                         array(
  110.                             "name" => "title",
  111.                             "label" => "Title",
  112.                             "type" => "text",
  113.                             "description" => "Give the feed a unique title so you can identify it later.",
  114.                             "validator" => false
  115.                         )
  116.                     )
  117.                 ),
  118.                 array(
  119.                     "title" => "Second section",
  120.                     "fields" => array(
  121.                         array(
  122.                             "name" => "website",
  123.                             "label" => "Website",
  124.                             "type" => "text",
  125.                             "validator" => array(
  126.                                 "type" => "url"
  127.                             )
  128.                         ),
  129.                         array(
  130.                             "name" => "three",
  131.                             "label" => "Third field",
  132.                             "type" => "text",
  133.                             "validator" => false
  134.                         ),
  135.                         array(
  136.                             "name" => "four",
  137.                             "label" => "Fourth",
  138.                             "type" => "text",
  139.                             "validator" => false
  140.                         ),
  141.                         array(
  142.                             "name" => "five",
  143.                             "label" => "Last field",
  144.                             "type" => "text",
  145.                             "validator" => false
  146.                         )
  147.                     )
  148.                 )
  149.             )
  150.         );
  151.     }
  152.    
  153.     // Initialize the forms
  154.     function admin_init()
  155.     {
  156.         $page = isset($_GET['page']) ? $_GET['page'] : '';
  157.         switch ($page):
  158.             case "wpformdemo":
  159.                 $this->simple_form = new WP_Settings_Form($this->simple_settings);
  160.                 $this->simple_form->register();
  161.                 break;
  162.            
  163.             case "wpformdemo/list":
  164.                 $this->multi_form = new WP_Settings_Form($this->multi_settings);
  165.                 $this->multi_form->register();
  166.                 break;
  167.            
  168.             case "wpformdemo/combined":
  169.                 $this->simple_form = new WP_Settings_Form($this->simple_settings);
  170.                 $this->simple_form->register();
  171.                 $this->multi_form  = new WP_Settings_Form($this->multi_settings);
  172.                 $this->multi_form->register();
  173.                 break;
  174.         endswitch;
  175.     }
  176.    
  177.     // Create the menu
  178.     function admin_menu()
  179.     {
  180.         add_menu_page('WP Form Demo','WP Form Demo','manage_options','wpformdemo',array($this, 'admin_page'));
  181.         add_submenu_page('wpformdemo','Simple','Simple','manage_options','wpformdemo',array($this, 'admin_page'));
  182.         add_submenu_page('wpformdemo','List','List','manage_options','wpformdemo/list',array($this, 'admin_page'));
  183.         add_submenu_page('wpformdemo','Combined','Combined','manage_options','wpformdemo/combined',array($this, 'admin_page'));
  184.     }
  185.    
  186.     // Echo the page content
  187.     function admin_page()
  188.     {
  189.         switch ($_GET['page']):
  190.             case "wpformdemo":
  191.                 echo '<div class="wrap"><h2>Simple Form</h2>';
  192.                 $this->simple_form->echo_settings_page();
  193.                 echo '</div>';
  194.                 break;
  195.            
  196.             case "wpformdemo/list":
  197.                 echo '<div class="wrap"><h2>List Form</h2>';
  198.                 $this->multi_form->echo_settings_page();
  199.                 echo '</div>';
  200.                 break;
  201.            
  202.             case "wpformdemo/combined":
  203.                 echo '<div class="wrap"><h2>Simple/List Combined Form</h2>';
  204.                 $this->multi_form->echo_settings_page();
  205.                 echo '<hr />';
  206.                 $this->simple_form->echo_settings_page();
  207.                 echo '</div>';
  208.                 break;
  209.         endswitch;
  210.     }
  211.    
  212.     // Callbacks
  213.     public function simple_validator($test_value,$old_value,$field)
  214.     {
  215.         // A smarter approach would be to make this actually do some validation
  216.         return $test_value;
  217.     }
  218.    
  219.     public function on_pause($row)
  220.     {
  221.         $row['enabled'] = 0;
  222.         return $row;
  223.     }
  224.    
  225.     public function on_unpause($row)
  226.     {
  227.         $row['enabled'] = 1;
  228.         return $row;
  229.     }
  230. }
  231. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement