Skip to content

Instantly share code, notes, and snippets.

@michaeluno
Last active September 25, 2020 18:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaeluno/fcfac27825aa8a35b90f to your computer and use it in GitHub Desktop.
Save michaeluno/fcfac27825aa8a35b90f to your computer and use it in GitHub Desktop.
Demonstrates how to suppress the options in Admin Page Framework. ( this requires v3.1.0 or above)
<?php
/*
* Plugin Name: Admin Page Framework Demo - Suppress Options
* Description: Demonstrates how to suppress the options in Admin Page Framework.
* Author: Michael Uno
* Author URI: http://michaeluno.jp
* Version: 1.1.0
* Requirement: Admin Page Framework v3.1.0 or higher.
*/
include( dirname( dirname( __FILE__ ) ) . '/admin-page-framework/library/apf/admin-page-framework.php' );
class AFFTest_SuppressOptions extends AdminPageFramework {
/**
* Sest up pages.
*/
public function setUp() {
$this->setRootMenuPage( 'Settings' ); // where to belong
$this->addSubMenuItem(
array(
'title' => __( 'Suppress Options', 'admin-page-framework-examples' ), // page and menu title
'page_slug' => 'apf_demo_suppress_options' // page slug
)
);
$this->addSettingSections(
array(
'section_id' => 'custom_repeatable_values',
'page_slug' => 'apf_demo_suppress_options',
)
);
$this->addSettingFields(
'custom_repeatable_values', // the target section ID
array(
'field_id' => 'my_text_field',
'title' => __( 'Repeatable Text Input', 'admin-page-framework-examples' ),
'type' => 'text',
'repeatable' => true,
)
);
}
/**
* Do something in the page.
*
* @callback action do_{page slug}
*/
public function do_apf_demo_suppress_options() {
submit_button(); // Add a submit button
}
/**
* The pre-defined validation callback method.
*
* @remark This is a pre-defined framework method.
*/
public function validate( $aInputs, $aOldInputs, $oAdminPage ) {
$this->setSettingNotice(
__( 'The form data is stored in a custom location.', 'admin-page-framework-examples' ),
'updated'
);
// Replace this with your function storesing data in your custom data base table.
set_transient( get_class( $this ), $aInputs, 3600 );
return $aInputs;
}
/**
* Returns the form data.
*
* @callback filter options_{instantiated class name}
* @return array The form data as a multi-dimensional array.
*/
public function options_AFFTest_SuppressOptions( $aOptions ) {
// Replace this with your function that retrieves form data.
$_aOptions = get_transient( get_class( $this ) );
$_aOptions = is_array( $_aOptions ) ? $_aOptions : array();
return $_aOptions + array(
'custom_repeatable_values' => array(
'my_text_field' => array(
0 => 'one',
1 => 'two',
2 => 'three',
),
)
);
}
}
new AFFTest_SuppressOptions(
'' // an empty string disables the built-in funcitonality that stores form data in the options table.
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment