Created
June 5, 2016 07:13
-
-
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.
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 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