Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: WP Settings Form Demo
- Plugin URI: http://www.webarnes.ca/wpform
- Description: Simplifies making a settings page
- Version: 0.99
- Author: William Barnes
- Author URI: http://www.webarnes.ca
- License: GPL2
- */
- // WORDPRESS HOOKS
- require_once('class.wpform.php');
- $WPFD = new WP_Form_Demo();
- class WP_Form_Demo
- {
- function __construct()
- {
- add_action('admin_init',array($this, 'admin_init'));
- add_action('admin_menu',array($this, 'admin_menu'));
- $this->simple_settings = array(
- "option_name" => "wpdemo_simple",
- "validator" => array($this,"simple_validator"),
- "sections" => array(
- array(
- "title" => "Simple Settings Form",
- "text" => "<p>This is a simple form.</p>",
- "fields" => array(
- array(
- "name" => "title",
- "label" => "Title",
- "type" => "text",
- "size" => 60
- ),
- array(
- "name" => "tags",
- "label" => "Tags",
- "description" => "Comma-separated list of tags that will be applied to each post",
- "type" => "text", // Boolean, dropdown, text, categories, authors
- "validator" => array(
- "type" => "preg_replace",
- "pattern" =>"|[^a-zA-Z0-9, _-]|",
- "replacement" => ""
- )
- ),
- array(
- "name" => "categories",
- "label" => "Categories",
- "type" => "categories", // Boolean, select, categories, tags, maybe
- "validator" => array(
- "type" => "numeric"
- )
- ),
- array(
- "name" => "author",
- "label" => "Author",
- "type" => "authors", // Boolean, select, categories, tags, maybe
- "validator" => array(
- "type" => "numeric"
- )
- )
- )
- )
- )
- );
- $this->multi_settings = array(
- "option_name" => "wpdemo_multi",
- "multi" => array(
- "list_title" => "Rows",
- "new_title" => "Add a new row",
- /* "new_fields" replaces the "Add a new row" button with a form.
- * You can use the values in that form to initialize the main
- * form or add sections based on the choice.
- */
- /*"new_fields" => array(
- array(
- "name" => "type",
- "label" => "Row Type",
- "type" => "dropdown"
- "options" => array(
- "1" => "One",
- "2" => "Two",
- )
- )
- ),*/
- "table_fields" => array(
- array('name'=>'title','label'=>'Title','link'=>true),
- array('name'=>'timestamp','type'=>'timestamp','label'=>'Timestamp','width'=>'250px'),
- array('name'=>'enabled','label'=>'Enabled','width'=>'50px')
- ),
- // Example multi-row action buttons, provide a callback as below
- "table_buttons" => array(
- "pause" => array('label'=>'Disable','callback'=>array($this,'on_pause')),
- "unpause" => array('label'=>'Enable','callback'=>array($this,'on_unpause'))
- )
- ),
- "defaults" => array(
- "enabled" => 1,
- "timestamp" => time()
- ),
- "sections" => array(
- array(
- "title" => "First Section",
- "fields" => array(
- array(
- "name" => "title",
- "label" => "Title",
- "type" => "text",
- "description" => "Give the feed a unique title so you can identify it later.",
- "validator" => false
- )
- )
- ),
- array(
- "title" => "Second section",
- "fields" => array(
- array(
- "name" => "website",
- "label" => "Website",
- "type" => "text",
- "validator" => array(
- "type" => "url"
- )
- ),
- array(
- "name" => "three",
- "label" => "Third field",
- "type" => "text",
- "validator" => false
- ),
- array(
- "name" => "four",
- "label" => "Fourth",
- "type" => "text",
- "validator" => false
- ),
- array(
- "name" => "five",
- "label" => "Last field",
- "type" => "text",
- "validator" => false
- )
- )
- )
- )
- );
- }
- // Initialize the forms
- function admin_init()
- {
- $page = isset($_GET['page']) ? $_GET['page'] : '';
- switch ($page):
- case "wpformdemo":
- $this->simple_form = new WP_Settings_Form($this->simple_settings);
- $this->simple_form->register();
- break;
- case "wpformdemo/list":
- $this->multi_form = new WP_Settings_Form($this->multi_settings);
- $this->multi_form->register();
- break;
- case "wpformdemo/combined":
- $this->simple_form = new WP_Settings_Form($this->simple_settings);
- $this->simple_form->register();
- $this->multi_form = new WP_Settings_Form($this->multi_settings);
- $this->multi_form->register();
- break;
- endswitch;
- }
- // Create the menu
- function admin_menu()
- {
- add_menu_page('WP Form Demo','WP Form Demo','manage_options','wpformdemo',array($this, 'admin_page'));
- add_submenu_page('wpformdemo','Simple','Simple','manage_options','wpformdemo',array($this, 'admin_page'));
- add_submenu_page('wpformdemo','List','List','manage_options','wpformdemo/list',array($this, 'admin_page'));
- add_submenu_page('wpformdemo','Combined','Combined','manage_options','wpformdemo/combined',array($this, 'admin_page'));
- }
- // Echo the page content
- function admin_page()
- {
- switch ($_GET['page']):
- case "wpformdemo":
- echo '<div class="wrap"><h2>Simple Form</h2>';
- $this->simple_form->echo_settings_page();
- echo '</div>';
- break;
- case "wpformdemo/list":
- echo '<div class="wrap"><h2>List Form</h2>';
- $this->multi_form->echo_settings_page();
- echo '</div>';
- break;
- case "wpformdemo/combined":
- echo '<div class="wrap"><h2>Simple/List Combined Form</h2>';
- $this->multi_form->echo_settings_page();
- echo '<hr />';
- $this->simple_form->echo_settings_page();
- echo '</div>';
- break;
- endswitch;
- }
- // Callbacks
- public function simple_validator($test_value,$old_value,$field)
- {
- // A smarter approach would be to make this actually do some validation
- return $test_value;
- }
- public function on_pause($row)
- {
- $row['enabled'] = 0;
- return $row;
- }
- public function on_unpause($row)
- {
- $row['enabled'] = 1;
- return $row;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement