Skip to content

Instantly share code, notes, and snippets.

@michaeluno
Last active December 24, 2015 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaeluno/9272572 to your computer and use it in GitHub Desktop.
Save michaeluno/9272572 to your computer and use it in GitHub Desktop.
Organize a form with sections with Admin Page Framework v3. This is an example plugin introduced in the tutrial, Organize a Form with Sections (http://en.michaeluno.jp/admin-page-framework/tutorials-v3/05-organize-a-form-with-sections/).
<?php
/*
Plugin Name: Admin Page Framework Tutorial 05 - Organize a Form with Sections
Plugin URI: http://en.michaeluno.jp/admin-page-framework
Description: Organize a form with sections with Admin Page Framework v3
Author: Michael Uno
Author URI: http://michaeluno.jp
Version: 1.0.1
Requirements: PHP 5.2.4 or above, WordPress 3.4 or above. Admin Page Framework 3.0.0 or above
*/
include( dirname( __FILE__ ) . '/library/apf/admin-page-framework.php' );
// Extend the class
class APF_CreateForm extends AdminPageFramework {
// Define the setup() method to set how many pages, page titles and icons etc.
public function setUp() {
// Set the root menu
$this->setRootMenuPage( 'Settings' ); // specifies to which parent menu to add.
// Add the sub menus and the pages
$this->addSubMenuItems(
array(
'title' => '5. Adding Forms with Sections', // the page and menu title
'page_slug' => 'my_second_form' // the page slug
)
);
$this->addSettingSections(
'my_second_form',
array(
'section_id' => 'my_first_section',
'title' => 'My First Form Section',
'description' => 'This section is for text fields.',
),
array(
'section_id' => 'my_second_section',
'title' => 'My Second Form Section',
'description' => 'This section is for selectors.',
)
);
// Add form fields
$this->addSettingFields(
'my_first_section',
array(
'field_id' => 'my_text_field',
'type' => 'text',
'title' => 'Text',
'description' => 'To create multiple fields of the same type, add arrays with numeric keys.',
array(), // sub-field
array(), // sub-field
),
array(
'field_id' => 'my_repeatable_text_field',
'type' => 'text',
'title' => 'Text',
'repeatable' => true,
)
);
$this->addSettingFields(
'my_second_section',
array(
'field_id' => 'my_checkbox',
'type' => 'checkbox',
'title' => __( 'Checkbox', 'admin-page-framework-demo' ),
'label' => __( 'Check me.', 'admin-page-framework-demo' ),
'default' => false,
),
array(
'field_id' => 'my_dropdown_list',
'title' => __( 'Drop-down List', 'admin-page-framework-demo' ),
'type' => 'select',
'default' => 1, // the index key of the label array below which yields 'Two'.
'label' => array(
0 => 'One',
1 => 'Two',
2 => 'Three',
),
),
array(
'field_id' => 'radio',
'title' => __( 'Radio Button', 'admin-page-framework-demo' ),
'type' => 'radio',
'label' => array(
'a' => 'Apple',
'b' => 'Banana',
'c' => 'Cherry'
),
'default' => 'c', // yields Cherry; its key is specified.
'after_label' => '<br />',
),
array( // Submit button
'field_id' => 'submit_button_b',
'type' => 'submit',
'show_title_column' => false,
)
);
}
/**
* @callback action do_ + page slug
*/
public function do_my_second_form() {
// Show the saved option value.
// The extended class name is used as the option key. This can be changed by passing a custom string to the constructor.
echo '<h3>Saved Values</h3>';
echo '<h3>Show as an Array</h4>';
echo $this->oDebug->getArray( get_option( 'APF_CreateForm' ) );
echo '<h3>Retrieve individual field values</h4>';
echo '<pre>APF_CreateForm[my_first_section][my_text_field][0]: ' . AdminPageFramework::getOption( 'APF_CreateForm', array( 'my_first_section', 'my_text_field', 0 ), 'default' ) . '</pre>';
echo '<pre>APF_CreateForm[my_second_section][my_dropdown_list]: ' . AdminPageFramework::getOption( 'APF_CreateForm', array( 'my_second_section', 'my_dropdown_list' ), 'default' ) . '</pre>';
}
}
// Instantiate the class object.
new APF_CreateForm;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment