Last active
September 25, 2020 18:10
-
-
Save michaeluno/c30713fcfe0d9d45d89f to your computer and use it in GitHub Desktop.
Demonstrates how to set default options with the options_{instantiated class name} hook in 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 - Default Option Values | |
* Description: Demonstrates how to set default options with the options_{instantiated class name} hook in 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__ ) . '/library/admin-page-framework.min.php' ); | |
// include( dirname( dirname( __FILE__ ) ) . '/admin-page-framework/development/admin-page-framework.php' ); | |
} | |
class APF_Demo_DefaultOptions extends AdminPageFramework { | |
public function setUp() { | |
$this->setRootMenuPage( 'Settings' ); // where to belong | |
$this->addSubMenuItem( | |
array( | |
'title' => __( 'Default Options', 'admin-page-framework-demo-default-options' ), // page and menu title | |
'page_slug' => 'apf_demo_default_options' // page slug | |
) | |
); | |
$this->addSettingSections( | |
array( | |
'section_id' => 'default_options_demo', | |
'page_slug' => 'apf_demo_default_options', | |
) | |
); | |
$this->addSettingFields( | |
'default_options_demo', // the target section ID | |
array( | |
'field_id' => 'my_text_field', | |
'title' => __( 'Text Input', 'admin-page-framework-demo-default-options' ), | |
'type' => 'text', | |
), | |
array( | |
'field_id' => 'my_text_field_repeatable', | |
'title' => __( 'Repeatable Text Inputs', 'admin-page-framework-demo-default-options' ), | |
'type' => 'text', | |
'repeatable' => true, | |
), | |
array( | |
'field_id' => 'my_select', | |
'title' => __( 'Select', 'admin-page-framework-demo-default-options' ), | |
'type' => 'select', | |
'label' => array( | |
'a' => __( 'A', 'admin-page-framework-demo-default-options' ), | |
'b' => __( 'B', 'admin-page-framework-demo-default-options' ), | |
'c' => __( 'C', 'admin-page-framework-demo-default-options' ), | |
) | |
), | |
array( | |
'field_id' => 'my_check_box', | |
'title' => __( 'Check Box', 'admin-page-framework-demo-default-options' ), | |
'type' => 'checkbox', | |
'label' => __( 'Check me!', 'admin-page-framework-demo-default-options' ), | |
), | |
array( | |
'field_id' => 'my_color', | |
'title' => __( 'Color', 'admin-page-framework-demo-default-options' ), | |
'type' => 'color', | |
), | |
array( | |
'field_id' => 'my_submit_reset', | |
'type' => 'submit', | |
'reset' => true, | |
'label' => __( 'Reset', 'admin-page-framework-demo-default-options' ), | |
'attributes' => array( | |
'class' => 'button button-secondary' | |
), | |
) | |
); | |
} | |
/** | |
* Do something in the page. | |
* | |
* @remark This is a pre-defined framework method. | |
*/ | |
public function do_apf_demo_default_options() { // do_{page slug} | |
submit_button(); // Add a submit button | |
} | |
/** | |
* Returns the options. | |
*/ | |
public function options_APF_Demo_DefaultOptions( $aOptions ) { // options_{instantiated class name} | |
/* | |
* Structure | |
* array( | |
* 'section id' => array( | |
* 'field id' => 'value' | |
* ) | |
* ) | |
*/ | |
return $aOptions + array( | |
'default_options_demo' => array( | |
'my_text_field' => 'Hello World!', | |
'my_text_field_repeatable' => array( | |
'This is the first value', | |
'This is the second value', | |
'This is the third value', | |
), | |
'my_select' => 'b', | |
'my_check_box' => true, | |
'my_color' => '#DDD' | |
) | |
); | |
} | |
} | |
new APF_Demo_DefaultOptions; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment