Admin Page Framework Documentation
  • Package
  • Class
  • Tree

Packages

  • AdminPageFramework
    • FieldType
    • MetaBox
    • Page
    • PostType
    • TaxonomyField

Classes

  • AdminPageFramework
  • AdminPageFramework_Menu
  • AdminPageFramework_Page
  • AdminPageFramework_Setting

Class AdminPageFramework_Setting

Provides public methods to add form elements with WordPress Settings API.

AdminPageFramework_Base
Extended by AdminPageFramework_Page_MetaBox
Extended by AdminPageFramework_Page
Extended by AdminPageFramework_Menu
Extended by AdminPageFramework_Setting_Base
Extended by AdminPageFramework_Setting

Indirect known subclasses

AdminPageFramework

Abstract
Package: AdminPageFramework\Page
Since: 2.0.0
Extends: AdminPageFramework_Setting_Base
Var: array $aFieldErrors stores the settings field errors.
Located at page/AdminPageFramework_Setting.php

Methods summary

public
# setSettingNotice( string $sMsg, string $sType = 'error', string $sID = null, integer $bOverride = true )

Sets the given message to be displayed in the next page load.

Sets the given message to be displayed in the next page load.

This is used to inform users about the submitted input data, such as "Updated successfully." or "Problem occurred." etc. and normally used in validation callback methods.

Example

if ( ! $bVerified ) {
        $this->setFieldErrors( $aErrors );
        $this->setSettingNotice( 'There was an error in your input.' );
        return $aOldPageOptions;
}

Parameters

$sMsg
string
the text message to be displayed.
$sType
string
( optional ) the type of the message, either "error" or "updated" is used.
$sID
string
( optional ) the ID of the message. This is used in the ID attribute of the message HTML element.
$bOverride
integer
( optional ) false: do not override when there is a message of the same id. true: override the previous one.

Since

2.0.0
2.1.2 Added a check to prevent duplicate items.
2.1.5 Added the $bOverride parameter.
3.0.0 Changed the scope to public from protected.

Overrides

AdminPageFramework_Base::setSettingNotice
public
# addSettingSections( array|string $aSection1, array $aSection2 = null, array $_and_more = null )

Adds the given form section items into the property.

Adds the given form section items into the property.

The passed section array must consist of the following keys.

Example

$this->addSettingSections(
        array(
                'section_id'    => 'text_fields',
                'page_slug'     => 'first_page',
                'tab_slug'      => 'textfields',
                'title' => 'Text Fields',
                'description'   => 'These are text type fields.',
                'order' => 10,
        ),
        array(
                'section_id'    => 'selectors',
                'page_slug'     => 'first_page',
                'tab_slug'      => 'selectors',
                'title' => 'Selectors and Checkboxes',
                'description'   => 'These are selector type options such as dropdown lists, radio buttons, and checkboxes',
        )

Parameters

$aSection1
array|string
the section array or the target page slug. If the target page slug is set, the next section array can omit the page slug key. Section Array
  • section_id - ( required, string ) the section ID. Avoid using non-alphabetic characters except underscore and numbers.
  • page_slug - ( optional, string ) the page slug that the section belongs to. If the target page slug is set, it can be omitted.
  • tab_slug - ( optional, string ) the tab slug that the section belongs to. The tab here refers to in-page tabs.
  • section_tab_slug - ( optional, string ) [3.0.0+] the section tab slug that the section are grouped into. The tab here refers to section tabs.
  • title - ( optional, string ) the title of the section.
  • capability - ( optional, string ) the access level of the section. If the page visitor does not have sufficient capability, the section will be invisible to them.
  • if - ( optional, boolean ) if the passed value is false, the section will not be registered.
  • order - ( optional, integer ) the order number of the section. The higher the number is, the lower the position it gets.
  • help - ( optional, string ) the help description added to the contextual help tab.
  • help_aside - ( optional, string ) the additional help description for the side bar of the contextual help tab.
  • repeatable - ( optional, boolean|array ) [3.0.0+] Indicates whether or not the section is repeatable. To set a minimum/maximum number of sections, pass an array with the key, min, and max. e.g. array( 'min' => 3, 'max' => 10 )
$aSection2
array
( optional ) another section array.
$_and_more
array
( optional ) add more section array to the next parameters as many as necessary.

Since

2.0.0
3.0.0 Changed the scope to public from protected.

Remark

Accepts variadic parameters; the number of accepted parameters are not limited to three.
The actual registration will be performed in the _replyToRegisterSettings() method with the admin_menu hook.
The target section tab slug and the target tab slug will be reset once the method returns.

Overrides

AdminPageFramework_Base::addSettingSections
public
# addSettingSection( array|string $asSection )

A singular form of the adSettingSections() method which takes only a single parameter.

A singular form of the adSettingSections() method which takes only a single parameter.

This is useful when adding section arrays in loops.

Parameters

$asSection
array|string
the section array. If a string is passed, it is considered as a target page slug that will be used as a page slug element from the next call so that the element can be ommited.

Since

2.1.2
3.0.0 Changed the scope to public from protected.

Remark

The actual registration will be performed in the _replyToRegisterSettings() method with the admin_menu hook.

Overrides

AdminPageFramework_Base::addSettingSection
public
# removeSettingSections( string $sSectionID1 = null, string $sSectionID2 = null, string $_and_more = null )

Removes the given section(s) by section ID.

Removes the given section(s) by section ID.

This accesses the property storing the added section arrays and removes the specified ones.

Example

$this->removeSettingSections( 'text_fields', 'selectors', 'another_section', 'yet_another_section' );

Parameters

$sSectionID1
string
the section ID to remove.
$sSectionID2
string
( optional ) another section ID to remove.
$_and_more
string
( optional ) add more section IDs to the next parameters as many as necessary.

Since

2.0.0
3.0.0 Changed the scope to public from protected.

Remark

Accepts variadic parameters; the number of accepted parameters are not limited to three.
The actual registration will be performed in the _replyToRegisterSettings() method with the admin_menu hook.

Overrides

AdminPageFramework_Base::removeSettingSections
public
# addSettingFields( array $aField1, array $aField2 = null, array $_and_more = null )

Adds the given field array items into the field array property by the given field definition array(s).

Adds the given field array items into the field array property by the given field definition array(s).

The field definition array requires specific keys. Refer to the parameter section of this method.

Example

$this->addSettingFields(
        array(
                'field_id'      =>      'text',
                'section_id'    =>      'text_fields',
                'title' =>      __( 'Text', 'admin-page-framework-demo' ),
                'description'   =>      __( 'Type something here.', 'admin-page-framework-demo' ),
                'type'  =>      'text',
                'order' =>      1,
                'default'       =>      123456,
                'size'  =>      40,
        ),
        array(
                'field_id'      =>      'text_multiple',
                'section_id'    =>      'text_fields',
                'title' =>      'Multiple Text Fields',
                'description'   =>      'These are multiple text fields.',
                'type'  =>      'text',
                'order' =>      2,
                'default'       =>      'Hello World',
                'label' =>      'First Item',
                'attributes'    =>      array(
                        'size'  =>      30
                ),
                array(
                        'label' =>      'Second Item',
                        'default'       =>      'Foo bar',
                        'attributes'    =>      array(
                                'size'  =>      60,
                        ),
                ),
                array(
                        'label' =>      'Third Item',
                        'default'       =>      'Yes, we can.',
                        'attributes'    =>      array(
                                'size'  =>      90,
                        ),
                ),
        )
);

Parameters

$aField1
array
the field definition array.

Built-in Field Types

  • text - a normal field to enter text input.
  • text - a masked text input field.
  • textarea - a text input field with multiple lines. It supports rich text editor.
  • radio - a set of radio buttons that lets the user pick an option.
  • checkbox - a check box that lets the user enable/disable an item.
  • select - a drop-down list that lest the user pick one or more item(s) from a list.
  • hidden - a hidden field that will be useful to insert invisible values.
  • file - a file uploader that lets the user upload files.
  • image - a custom text field with the image uploader script that lets the user set the image URL.
  • media - a custom text field with the media uploader script that lets the user set the file URL.
  • color - a custom text field with the color picker script.
  • submit - a submit button that lets the user send the form.
  • export - a custom submit field that lets the user export the stored data.
  • import - a custom combination field of the file and the submit fields that let the user import data.
  • posttype - a check-list of post types enabled on the site.
  • taxonomy - a set of check-lists of taxonomies enabled on the site in a tabbed box.
  • size - a combination field of the text and the select fields that let the user set sizes with a selectable unit.
  • section_title - [3.0.0+] a text field type that will be placed in the section title so that it lets the user set the section title. Note that only one field with this field type is allowed per a section.

Field Definition Array

  • field_id - ( required, string ) the field ID. Avoid using non-alphabetic characters except underscore and numbers.
  • type - ( required, string ) the type of the field. The supported types are listed below.
  • section_id - ( optional, string ) the section ID that the field belongs to. If not set, the internal _default section ID will be assigned.
  • title - ( optional, string ) the title of the section.
  • description - ( optional, string ) the description of the field which is inserted into the after the input field tag.
  • tip - ( optional, string ) the tip for the field which is displayed when the mouse is hovered over the field title.
  • capability - ( optional, string ) the http://codex.wordpress.org/Roles_and_Capabilities">access level of the section. If the page visitor does not have sufficient capability, the section will be invisible to them.
  • error_message - ( optional, string ) the error message to display above the input field.
  • before_field - ( optional, string ) the HTML string to insert before the input field output.
  • after_field - ( optional, string ) the HTML string to insert after the input field output.
  • if - ( optional, boolean ) if the passed value is false, the section will not be registered.
  • order - ( optional, integer ) the order number of the section. The higher the number is, the lower the position it gets.
  • label - ( optional, string ) the text label(s) associated with and displayed along with the input field. Some input types can ignore this key.
  • default - ( optional, string|array ) the default value(s) assigned to the input tag's value attribute.
  • value - ( optional, string|array ) the value(s) assigned to the input tag's value attribute to override the default and the stored value.
  • delimiter - ( optional, string ) the HTML string that delimits multiple elements. This is available if the label key is passed as array. It will be enclosed in inline-block elements so the passed HTML string should not contain block elements.
  • before_input - ( optional, string ) the HTML string inserted right before the input tag. It will be enclosed in the label tag so the passed HTML string should not contain block elements.
  • after_input - ( optional, string ) the HTML string inserted right after the input tag. It will be enclosed in the label tag so the passed HTML string should not contain block elements.
  • label_min_width - ( optional, string ) the inline style property of the min-width of the label tag for the field in pixel without the unit. Default: <span class="php-num">120</span>.
  • help - ( optional, string ) the help description added to the contextual help tab.
  • help_aside - ( optional, string ) the additional help description for the side bar of the contextual help tab.
  • repeatable - [3.0.0+] ( optional, array|boolean ) whether the fields should be repeatable. If it yields true, the plus and the minus buttons appear next to each field that lets the user add/remove the fields. Optionally an setting array can be passed.
    Repeatable Fields Setting Array
    • max - the allowed maximum number of fields to be repeated.
    • min</string> - the allowed minimum number of fields to be repeated.
  • sortable - [3.0.0+] ( optional, boolean ) whether the fields should be sortable. If it yields true, the fields will be enclosed in a draggable box.
  • attributes - [3.0.0+] ( optional, array ) holds key-value pairs representing the attribute and its property. Note that some field types have specific keys in the first dimensions. e.g.array( 'class' => 'my_custom_class_selector', 'style' => 'background-color:#777', 'size' => 20, )
  • show_title_column - [3.0.0+] ( optional, boolean ) If true, the field title column will be omitted from the output.
  • hidden - [3.0.0+] ( optional, boolean ) If true, the entire field row output will be invisible with the inline style attribute of style="display:none".

Field Type Specific Keys

Each field type uses specific array keys.

  • text - a text input field which allows the user to type text.
  • password - a password input field which allows the user to type text.
  • number, range - HTML5 input field types. Some browsers do not support these.
  • textarea - a textarea input field. The following array keys are supported.
    • rich - [2.1.2+]( optional, array ) to make it a rich text editor pass a non-empty value. It accept a setting array of the _WP_Editors class defined in the core. For more information, see the argument section of this page.
  • radio - a radio button input field.
  • checkbox - a check box input field.
  • select - a drop-down input field.
    • is_multiple - ( optional, boolean ) if this is set to true, the multiple attribute will be inserted into the field input tag, which enables the multiple selections for the user.
  • size - a size input field. This is a combination of number and select fields.
    • units - ( optional, array ) defines the units to show. e.g. array( 'px' => 'px', '%' => '%', 'em' => 'em' ) Default: array( 'px' => 'px', '%' => '%', 'em' => 'em', 'ex' => 'ex', 'in' => 'in', 'cm' => 'cm', 'mm' => 'mm', 'pt' => 'pt', 'pc' => 'pc' )
    • is_multiple - ( optional, boolean ) if this is set to true, the multiple attribute will be inserted into the field input tag, which enables the multiple selections for the user.
    • attributes - [3.0.0+] ( optional, array ) The attributes array of this field type has four initial keys: size, unit, optgroup, and option and they have a regular attribute array in each.
  • hidden - a hidden input field.
  • file - a file upload input field.
  • submit - a submit button input field.
    • href - ( optional, string ) the url(s) linked to the submit button.
    • redirect_url - ( optional, string ) the url(s) redirected to after submitting the input form.
    • reset - [2.1.2+] ( optional, boolean ) the option key to delete. Set 1 for the entire option.
  • import - an import input field. This is a custom file and submit field.
    • option_key - ( optional, string ) the option table key to save the importing data.
    • format - ( optional, string ) the import format. json, or array is supported. Default: array
    • is_merge - ( optional, boolean ) [2.0.5+] determines whether the imported data should be merged with the existing options.
  • export - an export input field. This is a custom submit field.
    • file_name - ( optional, string ) the file name to download.
    • format - ( optional, string ) the format type. array, json, or text is supported. Default: array.
    • data - ( optional, string|array|object ) the data to export.
  • image - an image input field. This is a custom text field with an attached JavaScript script.
    • show_preview - ( optional, boolean ) if this is set to false, the image preview will be disabled.
    • attributes_to_store - [2.1.3+] ( optional, array ) the array of the attribute names of the image to save. If this is set, the field will be an array with the specified attributes. The supported attributes are, 'title', 'alt', 'width', 'height', 'caption', 'id', 'align', and 'link'. Note that for external URLs, ID will not be captured. e.g. 'attributes_to_store' => array( 'id', 'caption', 'description' )
    • allow_external_source - [2.1.3+] ( optional, boolean ) whether external URL can be set via the uploader.
    • attributes - [3.0.0+] ( optional, array ) The attributes array of this field type has three keys: input, button, and preview and they have a regular attribute array in each.
  • media - [2.1.3+] a media input field. This is a custom text field with an attached JavaScript script.
    • attributes_to_store - [2.1.3+] ( optional, array ) the array of the attribute names of the image to save. If this is set, the field will be an array with the specified attributes. The supported attributes are, 'id', 'caption', and 'description'. Note that for external URLs, ID will not be captured. e.g. 'attributes_to_store' => array( 'id', 'caption', 'description' )
    • allow_external_source - [2.1.3+] ( optional, boolean ) whether external URL can be set via the uploader.
  • color - a color picker input field. This is a custom text field with a JavaScript script.
  • taxonomy - a taxonomy check list. This is a set of check boxes listing a specified taxonomy. This does not accept to create multiple fields by passing an array of labels.
    • taxonomy_slugs - ( optional, array ) the taxonomy slug to list.
    • max_width - ( optional, string ) the inline style property value of max-width of this element. Include the unit such as px, %. Default: 100%
    • height - ( optional, string ) the inline style property value of height of this element. Include the unit such as px, %. Default: 250px
  • posttype - a post-type check list. This is a set of check boxes listing post type slugs.
    • slugs_to_remove - ( optional, array ) the post type slugs not to be listed. e.g.array( 'revision', 'attachment', 'nav_menu_item' )
$aField2
array
( optional ) another field array.
$_and_more
array
( optional ) add more field arrays to the next parameters as many as necessary.

Since

2.0.0
3.0.0 Changed the scope to public from protected.

Remark

Accepts variadic parameters; the number of accepted parameters are not limited to three.
The actual registration will be performed in the _replyToRegisterSettings() method with the admin_menu hook.

Overrides

AdminPageFramework_Base::addSettingFields
public
# addSettingField( array|string $asField )

Adds the given field array items into the field array property.

Adds the given field array items into the field array property.

Identical to the addSettingFields() method except that this method does not accept enumerated parameters.

Parameters

$asField
array|string
the field array or the target section ID. If the target section ID is set, the section_id key can be omitted from the next passing field array.

Since

2.1.2
3.0.0 Changed the scope to public from protected.

Overrides

AdminPageFramework_Base::addSettingField
public
# removeSettingFields( string $sFieldID1, string $sFieldID2 = null, string $_and_more )

Removes the given field(s) by field ID.

Removes the given field(s) by field ID.

This accesses the property storing the added field arrays and removes the specified ones.

Example

$this->removeSettingFields( 'fieldID_A', 'fieldID_B', 'fieldID_C', 'fieldID_D' );

Parameters

$sFieldID1
string
the field ID to remove.
$sFieldID2
string
( optional ) another field ID to remove.
$_and_more
string
( optional ) add more field IDs to the next parameters as many as necessary.

Since

2.0.0
3.0.0 Changed the scope to public from protected.

Remark

Accepts variadic parameters; the number of accepted parameters are not limited to three.
The actual registration will be performed in the _replyToRegisterSettings() method with the admin_menu hook.

Overrides

AdminPageFramework_Base::removeSettingFields
public
# setFieldErrors( array $aErrors, string $sID = null, integer $nSavingDuration = 300 )

Sets the field error array.

Sets the field error array.

This is normally used in validation callback methods when the submitted user's input data have an issue. This method saves the given array in a temporary area( transient ) of the options database table.

Example

public function validation_APF_Demo_verify_text_field_submit( $aNewInput, $aOldOptions ) {

        // 1. Set a flag.
        $bVerified = true;

        // 2. Prepare an error array.
        $aErrors = array();

        // 3. Check if the submitted value meets your criteria.
        if ( ! is_numeric( $aNewInput['verify_text_field'] ) ) {
                $aErrors['verify_text_field'] = __( 'The value must be numeric:', 'admin-page-framework-demo' )
                        . $aNewInput['verify_text_field'];
                $bVerified = false;
        }

        // 4. An invalid value is found.
        if ( ! $bVerified ) {
                // 4-1. Set the error array for the input fields.
                $this->setFieldErrors( $aErrors );
                $this->setSettingNotice( 'There was an error in your input.' );
                return $aOldOptions;
        }

        return $aNewInput;

}

Parameters

$aErrors
array
the field error array. The structure should follow the one contained in the submitted $_POST array.
$sID
string
this should be the page slug of the page that has the dealing form field.
$nSavingDuration
integer
the transient's lifetime. 300 seconds means 5 minutes.

Since

2.0.0
3.0.0 Changed the scope to public from protected.

Remark

the user may use this method.
the transient name is a MD5 hash of the extended class name + _ + page slug ( the passed ID )

Overrides

AdminPageFramework_Base::setFieldErrors
public array|string|null
# getFieldValue( string $sFieldID, mixed $sSectionID = '' )

Retrieves the specified field value stored in the options by field ID.

Retrieves the specified field value stored in the options by field ID.

Parameters

$sFieldID
string
The field ID.
$sSectionID

Returns

array|string|null
If the field ID is not set in the saved option array, it will return null. Otherwise, the set value. If the user has not submitted the form, the framework will try to return the default value set in the field definition array.

Since

2.1.2
3.0.0 Changed the scope to public from protected. Dropped the sections. Made it return a default value even if it's not saved in the database.

Overrides

AdminPageFramework_Base::getFieldValue

Methods inherited from AdminPageFramework_Setting_Base

__construct(), _replyToCheckRedirects(), _replyToGetFieldOutput(), _replyToGetSectionHeaderOutput(), _replyToRegisterSettings()

Methods inherited from AdminPageFramework_Menu

addSubMenuItem(), addSubMenuItems(), setRootMenuPage(), setRootMenuPageBySlug()

Methods inherited from AdminPageFramework_Page

_replyToFinalizeInPageTabs(), addInPageTab(), addInPageTabs(), setInPageTabTag(), setInPageTabsVisibility(), setPageHeadingTabTag(), setPageHeadingTabsVisibility(), setPageTitleVisibility()

Methods inherited from AdminPageFramework_Page_MetaBox

_getNumberOfColumns(), _printMetaBox(), _replyToAddMetaboxScript(), _replyToEnableMetaBox(), _replyToReturnDefaultNumberOfScreenColumns(), _replyToSetNumberOfScreenLayoutColumns()

Methods inherited from AdminPageFramework_Base

__call(), _doPageLoadCall(), _isInThePage(), _sortByOrder(), addHelpTab(), addLinkToPluginDescription(), addLinkToPluginTitle(), addSubMenuLink(), addSubMenuPage(), addSubMenuPages(), enqueueScript(), enqueueScripts(), enqueueStyle(), enqueueStyles(), setAdminNotice(), setCapability(), setDisallowedQueryKeys(), setFooterInfoLeft(), setFooterInfoRight(), setUp()

Magic methods summary

Properties summary

Properties inherited from AdminPageFramework_Setting_Base

$_sFieldsType, $_sTargetPageSlug, $_sTargetSectionTabSlug, $_sTargetTabSlug, $aFieldErrors

Properties inherited from AdminPageFramework_Base

$_aHookPrefixes, $oDebug, $oHeadTag, $oHelpPane, $oLink, $oMsg, $oPageLoadInfo, $oProp, $oUtil

Admin Page Framework Documentation API documentation generated by ApiGen 2.8.0