Last active
August 29, 2015 14:11
-
-
Save michaeluno/6c666aacb3d33ec9be48 to your computer and use it in GitHub Desktop.
An example WordPress plugin that shows how to create a custom field type with text input and textarea which can be repeated ans sorted.
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 Test - Text and Textarea Field Type */ | |
// Include the library file. Set your file path here. | |
$_sLibraryPath = defined( 'WP_DEBUG' ) && WP_DEBUG | |
? dirname( dirname( __FILE__ ) ) . '/admin-page-framework/development/admin-page-framework.php' | |
: dirname( dirname( __FILE__ ) ) . '/admin-page-framework/library/admin-page-framework.min.php'; | |
if ( ! class_exists( 'AdminPageFramework' ) && ! include( $_sLibraryPath ) ) { | |
return; | |
} | |
class APF_Test_CustomFieldType extends AdminPageFramework { | |
/** | |
* Defines pages. | |
*/ | |
public function setUp() { | |
$this->setRootMenuPage( __( 'Text and Textarea', 'admin-page-framework-test' ) ); | |
$this->addSubMenuItems( | |
array( | |
'title' => __( 'Text and Textarea', 'admin-page-framework-test' ), | |
'page_slug' => 'apf_test_text_and_textarea', | |
) | |
); | |
} | |
/** | |
* Defines form elements. | |
* | |
* This method gets triggered when the `apf_test_text_and_textarea` page loads. | |
*/ | |
public function load_apf_test_text_and_textarea( $oFactory ) { | |
if ( include( dirname( __FILE__ ) . '/TextAndTextAreaFieldType.php' ) ) { | |
new TextAndTextAreaFieldType( get_class( $this ) ); | |
} | |
$this->addSettingFields( | |
array( | |
'field_id' => 'text_and_textarea', | |
'type' => 'text_and_textarea', | |
'title' => __( 'Text and Textarea', 'admin-page-framework-test' ), | |
'label' => array( | |
'text' => __( 'Text', 'admin-page-framework-test' ), | |
'textarea' => __( 'Text Area', 'admin-page-framework-test' ), | |
), | |
'repeatable' => true, | |
'sortable' => true, | |
), | |
array( | |
'field_id' => 'submit', | |
'type' => 'submit', | |
) | |
); | |
} | |
} | |
new APF_Test_CustomFieldType( | |
null, // the option key - when null is passed the class name in this case 'APF_Test_CustomFieldType' will be used | |
__FILE__ // the caller script path. | |
); |
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 | |
if ( ! class_exists( 'TextAndTextAreaFieldType' ) ) : | |
class TextAndTextAreaFieldType extends AdminPageFramework_FieldType { | |
/** | |
* Defines the field type slugs used for this field type. | |
*/ | |
public $aFieldTypeSlugs = array( 'text_and_textarea', ); | |
/** | |
* Defines the default key-values of this field type. | |
* | |
* @remark $_aDefaultKeys holds shared default key-values defined in the base class. | |
*/ | |
protected $aDefaultKeys = array( | |
); | |
/** | |
* Loads the field type necessary components. | |
*/ | |
protected function setUp() { } | |
/** | |
* Returns an array holding the urls of enqueuing scripts. | |
*/ | |
protected function getEnqueuingScripts() { | |
return array( | |
// array( 'src' => dirname( __FILE__ ) . '/js/jquery.knob.js', 'dependencies' => array( 'jquery' ) ), | |
); | |
} | |
/** | |
* Returns an array holding the urls of enqueuing styles. | |
*/ | |
protected function getEnqueuingStyles() { | |
return array(); | |
} | |
/** | |
* Returns the field type specific JavaScript script. | |
*/ | |
protected function getScripts() { | |
return ""; | |
} | |
/** | |
* Returns IE specific CSS rules. | |
*/ | |
protected function getIEStyles() { return ''; } | |
/** | |
* Returns the field type specific CSS rules. | |
*/ | |
protected function getStyles() { | |
return " | |
.admin-page-framework-section .form-table td | |
.admin-page-framework-field-text_and_textarea .admin-page-framework-input-label-container label { | |
display: block; | |
clear: both; | |
width: 100%; | |
} | |
.admin-page-framework-field-text_and_textarea .admin-page-framework-input-label-container textarea { | |
vertical-align: middle; | |
} | |
"; | |
} | |
/** | |
* Returns the output of the field type. | |
*/ | |
protected function getField( $aField ) { | |
$_aAttributes_Text = array( | |
'type' => 'text', | |
'id' => $aField['attributes']['id'] . '_text', | |
'name' => $aField['attributes']['name'] . '[text]', | |
'value' => isset( $aField['value']['text'] ) ? $aField['value']['text'] : null, | |
) + $aField['attributes']; | |
$_aAttributes_Textarea = array( | |
'type' => null, | |
'id' => $aField['attributes']['id'] . '_textarea', | |
'name' => $aField['attributes']['name'] . '[textarea]', | |
) + $aField['attributes']; | |
unset( $_aAttributes_Textarea['value'] ); | |
$_sLabel_text = $this->getFieldElementByKey( $aField['label'], 'text' ); | |
$_sLabel_textarea = $this->getFieldElementByKey( $aField['label'], 'textarea' ); | |
return | |
$aField['before_label'] | |
. "<div class='admin-page-framework-input-label-container'>" | |
. "<label for='{$_aAttributes_Text['id']}'>" | |
. $this->getFieldElementByKey( $aField['before_input'], 'text' ) | |
. ( $_sLabel_text | |
? "<span class='admin-page-framework-input-label-string' style='min-width:" . $this->sanitizeLength( $aField['label_min_width'] ) . ";'>" . $_sLabel_text . "</span>" | |
: "" | |
) | |
. "<input " . $this->generateAttributes( $_aAttributes_Text ) . " />" | |
. $this->getFieldElementByKey( $aField['after_input'], 'text' ) | |
. "</label>" | |
. "<label for='{$_aAttributes_Textarea['id']}'>" | |
. $this->getFieldElementByKey( $aField['before_input'], 'textarea' ) | |
. ( $_sLabel_textarea | |
? "<span class='admin-page-framework-input-label-string' style='min-width:" . $this->sanitizeLength( $aField['label_min_width'] ) . ";'>" . $_sLabel_textarea . "</span>" | |
: "" | |
) | |
. "<textarea " . $this->generateAttributes( $_aAttributes_Textarea ) . " >" | |
. esc_textarea( isset( $aField['value']['textarea'] ) ? $aField['value']['textarea'] : null ) | |
. "</textarea>" | |
. $this->getFieldElementByKey( $aField['after_input'], 'textarea' ) | |
. "</label>" | |
. $aField['after_input'] | |
. "<div class='repeatable-field-buttons'></div>" // the repeatable field buttons will be replaced with this element. | |
. "</div>" | |
. $aField['after_label']; | |
} | |
} | |
endif; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment