Skip to content

Instantly share code, notes, and snippets.

@michaeluno
Created February 28, 2016 06:53
Show Gist options
  • Select an option

  • Save michaeluno/d2e32c98f5bf710078ad to your computer and use it in GitHub Desktop.

Select an option

Save michaeluno/d2e32c98f5bf710078ad to your computer and use it in GitHub Desktop.
A sample WordPress plugin that demonstrates a custom text field type for Admin Page Framework.
<?php
/**
* Plugin Name: Admin Page Framework - Custom Text Field TYpe
*/
function loadAPFCustomTextFieldType() {
if ( ! class_exists( 'AdminPageFramework' ) ) {
return;
}
include( dirname( __FILE__ ) . '/CustomTextFieldType.php' );
include( dirname( __FILE__ ) . '/APF_CustomTextFieldType.php' );
// Register the field type
new CustomTextFieldType( 'APF_CustomTextFieldType' );
// Create an admin page
new APF_CustomTextFieldType;
}
add_action( 'plugins_loaded', 'loadAPFCustomTextFieldType' );
<?php
class APF_CustomTextFieldType extends AdminPageFramework {
public function setUp() {
$this->setRootMenuPage( 'Sample' ); // where to belong
$this->addSubMenuItem(
array(
'title' => __( 'Custom Text Field Type', 'admin-page-framework-demo-custom-field-type' ), // page and menu title
'page_slug' => 'apf_custom_text_field_type' // page slug
)
);
$this->addSettingFields(
// 'sbutmit_hooks', // the target section ID
array(
'field_id' => 'my_text_field',
'title' => __( 'Example', 'admin-page-framework-demo-custom-field-type' ),
'type' => 'custom_text',
)
);
}
/**
* Do something in the page.
*
* @callback filter do_{page slug}
*/
public function do_apf_custom_text_field_type() {
submit_button();
}
}
<?php
class CustomTextFieldType extends AdminPageFramework_FieldType_text {
/**
* Defines the field type slugs used for this field type.
*/
public $aFieldTypeSlugs = array( 'custom_text', );
protected function getScripts() {
return
"jQuery( document ).ready( function(){
jQuery( 'input[type=text][data-type=custom-text]' ).each( function() {
jQuery( this ).get( 0 ).scrollLeft = jQuery( this ).get( 0 ).scrollWidth;
});
});";
}
/**
* Returns the output of the field type.
*
* @return string
*/
protected function getField( $aField ) {
$aField[ 'attributes' ][ 'type' ] = 'text';
$aField[ 'attributes' ][ 'data-type' ] = 'custom-text';
$aField[ 'attributes' ][ 'autofocus' ] = '';
return parent::getField( $aField );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment