Last active
August 29, 2015 14:02
-
-
Save michaeluno/fb4088b922b71710c7fb to your computer and use it in GitHub Desktop.
Demonstrates how to set a custom value to the field created by Admin Page Framework.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Plugin Name: Admin Page Framework Demo - Setting Custom Field Values | |
* Description: Demonstrates how to set a custom value to the field created by Admin Page Framework. | |
* Author: Michael Uno | |
* Author URI: http://michaeluno.jp | |
* Version: 1.0.1 | |
* Requirement: Admin Page Framework v3.1.0 or higher. | |
*/ | |
if ( ! class_exists( 'AdminPageFramework' ) ) { | |
// include( dirname( __FILE__ ) . '/class/admin-page-framework.min.php' ); | |
include( dirname( dirname( __FILE__ ) ) . '/admin-page-framework/development/admin-page-framework.php' ); | |
} | |
class AFFTest_CustomFieldValues extends AdminPageFramework { | |
public function setUp() { | |
$this->setRootMenuPage( 'Settings' ); // where to belong | |
$this->addSubMenuItem( | |
array( | |
'title' => __( 'Custom Field Values', 'demo-custom-field-values' ), // page and menu title | |
'page_slug' => 'apf_demo_custom_field_values' // page slug | |
) | |
); | |
$this->addSettingSections( | |
array( | |
'section_id' => 'custom_values', | |
'page_slug' => 'apf_demo_custom_field_values', | |
) | |
); | |
$this->addSettingFields( | |
'custom_values', // the target section ID | |
array( | |
'field_id' => 'my_text_field', | |
'title' => __( 'Text Input', 'demo-custom-field-values' ), | |
'type' => 'text', | |
'value' => $this->_getValueFromYourCustomTable( 'my_text_field' ), | |
), | |
array( | |
'field_id' => 'my_select_field', | |
'title' => __( 'Text Input', 'demo-custom-field-values' ), | |
'type' => 'select', | |
'label' => array( | |
'a' => __( 'Apple', 'demo-custom-field-values' ), | |
'b' => __( 'Banana', 'demo-custom-field-values' ), | |
), | |
'value' => $this->_getValueFromYourCustomTable( 'my_select_field' ), | |
) | |
); | |
} | |
/** | |
* Retrieves the data from your custom table. | |
*/ | |
private function _getValueFromYourCustomTable( $sFieldID ) { | |
// Retrieve the value from your custom table. | |
switch( $sFieldID ) { | |
case 'my_text_field': | |
return 'Assume this is the retrieved value'; | |
case 'my_select_field': | |
return 'a'; // the key of the label array. | |
} | |
} | |
/** | |
* Do something in the page. | |
* | |
* @remark This is a pre-defined framework method. | |
*/ | |
public function do_apf_demo_custom_field_values() { // do_{page slug} | |
submit_button(); // Add a submit button | |
} | |
/** | |
* The pre-defined validation callback method. | |
* | |
* @remark This is a pre-defined framework method. | |
*/ | |
public function validation_apf_demo_custom_field_values( $aInput, $aOldInput ) { // validation_{page slug} | |
// See the log file in the wp-content directory. | |
AdminPageFramework_Debug::logArray( $aInput ); | |
// Now do something with the submitted data here. | |
// saveDataInYourCustomTable( $aInput ); | |
return $aInput; | |
} | |
} | |
new AFFTest_CustomFieldValues; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment