Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaeluno/80839cac5f82ee51c46437920d4205d5 to your computer and use it in GitHub Desktop.
Save michaeluno/80839cac5f82ee51c46437920d4205d5 to your computer and use it in GitHub Desktop.
Demonstrates the use of custom taxonomy fields with Admin Page Framework, a WordPress libaray.
<?php
/**
* Plugin Name: Admin Page Framework Tutorial 14 - Taxonomy Fields
* Plugin URI: http://en.michaeluno.jp/admin-page-framework
* Description: Demonstrates the use of custom taxonomy fields with Admin Page Framework.
* Author: Michael Uno
* Author URI: http://michaeluno.jp
* Version: 1.0.0
*/
// Include the library file. Set your correct file path here.
include( dirname( dirname( __FILE__ ) ) . '/admin-page-framework/library/apf/admin-page-framework.php' );
class APF_Tutorial_TaxonomyField extends AdminPageFramework_TaxonomyField {
/*
* Defines custom taxonomy fields.
*/
public function setUp() {
$this->addSettingFields(
array(
'field_id' => 'my_image',
'type' => 'image',
'title' => __( 'Image', 'admin-page-framework-tutorial' ),
'attributes' => array(
'preview' => array(
'style' => 'max-width: 200px;',
),
),
)
);
}
/**
* Modifies the columns of the term listing table.
* @callback filter column_{instantiated class name}
*/
public function columns_APF_Tutorial_TaxonomyField( $aColumns ) {
unset( $aColumns[ 'description' ] );
return array(
'cb' => $aColumns[ 'cb' ],
'thumbnail' => __( 'Thumbnail', 'admin-page-framework-tutorial' ),
)
+ $aColumns;
}
/**
* Modifies the cell of the `thumbnail` column.
* @callback filter cell_{instantiated class name}_{cell slug}
* @return string
*/
public function cell_APF_Tutorial_TaxonomyField_thumbnail( $sCellHTML, $iTermID ) {
$_aOptions = get_option(
'APF_Tutorial_TaxonomyField', // option key - by default the class name is the option key.
array() // - default value in case a value is not set.
);
$_sImageURL = $this->oUtil->getElement(
$_aOptions,
array( $iTermID, 'my_image' ),
'' // default value
);
return $_sImageURL
? "<img src='{$_sImageURL}' style='max-height: 72px; max-width: 120px;'/>"
: $sCellHTML;
}
/**
* Inserts custom text.
*
* @callback action do_{instantiated class name}
*/
public function do_APF_Tutorial_TaxonomyField() {
?>
<p><?php _e( 'This text is inserted with the <code>do_{instantiated class name}</code> hook.', 'admin-page-framework-tutorial' ) ?></p>
<?php
}
/**
* Validates submitted option values.
* @return array
* @callback filter validation_{class name}
*/
public function validation_APF_Tutorial_TaxonomyField( $aInputs, $aOldInputs ) {
return $aInputs;
}
}
new APF_Tutorial_TaxonomyField(
'category' // taxonomy slug
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment