Last active
December 25, 2015 05:55
-
-
Save michaeluno/2f2673e3b89c5ff3619b to your computer and use it in GitHub Desktop.
Demonstrates how to add a meta box to a custom post type created by the framework and display the set custom field value in the front end article page and a custom cloumn in the post listing table. This is a sample plugin introduced in the tutorial Add a Meta Box to a Custom Post Type (http://en.michaeluno.jp/admin-page-framework/tutorials-v3/13…
This file contains hidden or 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 13 - Add a Meta Box to a Custom Post Type | |
| * Plugin URI: http://en.michaeluno.jp/admin-page-framework | |
| * Description: Adds a meta box to custom post type created by the framework and display the set custom field value. | |
| * Author: Michael Uno | |
| * Author URI: http://michaeluno.jp | |
| * Version: 1.0.0 | |
| * Requirements: PHP 5.2.4 or above, WordPress 3.4 or above. Admin Page Framework 3.0.6 or above | |
| */ | |
| // Include the library file. Set your file path here. | |
| include( dirname( dirname( __FILE__ ) ) . '/library/apf/admin-page-framework.php' ); | |
| class APF_Tutorial_PostType extends AdminPageFramework_PostType { | |
| /** | |
| * Sets up necessary settings. | |
| */ | |
| public function setUp() { | |
| $this->setArguments( | |
| array( // argument - for the array structure, refer to http://codex.wordpress.org/Function_Reference/register_post_type#Arguments | |
| 'labels' => array( | |
| 'name' => __( 'Colors', 'admin-page-framework-turorial' ), | |
| 'add_new_item' => __( 'Add Color', 'admin-page-framework-tutorial' ), | |
| ), | |
| 'supports' => array( 'title' ), // e.g. array( 'title', 'editor', 'comments', 'thumbnail', 'excerpt' ), | |
| 'public' => true, | |
| 'menu_icon' => version_compare( $GLOBALS['wp_version'], '3.8', '>=' ) | |
| ? 'dashicons-welcome-learn-more' | |
| : 'https://lh4.googleusercontent.com/-z7ydw-aPWGc/UwpeB96m5eI/AAAAAAAABjs/Brz6edaUB58/s800/demo04_16x16.png', | |
| // (framework specific key) this sets the screen icon for the post type for WordPress v3.7.1 or below. | |
| 'screen_icon' => dirname( APFDEMO_FILE ) . '/asset/image/wp-logo_32x32.png', // a file path can be passed instead of a url, plugins_url( 'asset/image/wp-logo_32x32.png', APFDEMO_FILE ) | |
| ) | |
| ); | |
| } | |
| /* | |
| * Modifies the columns of post listing table. | |
| * | |
| * @remark columns_{post type slug} | |
| * @return array | |
| */ | |
| public function columns_apf_tutorial_posts( $aHeaderColumns ) { | |
| return array( | |
| 'cb' => '<input type="checkbox" />', // Checkbox for bulk actions. | |
| 'title' => __( 'Title', 'admin-page-framework' ), // Post title. Includes "edit", "quick edit", "trash" and "view" links. If $mode (set from $_REQUEST['mode']) is 'excerpt', a post excerpt is included between the title and links. | |
| 'color' => __( 'Color', 'admin-page-framework-tutorial' ), | |
| ) | |
| // + $aHeaderColumns // uncomment this to enable the default columns. | |
| ; | |
| } | |
| /** | |
| * Modifies the 'color' column cell contents of the post listing table | |
| * @return string | |
| */ | |
| public function cell_apf_tutorial_posts_color( $sCell, $iPostID ) { // cell_{post type}_{column key} | |
| $_sColor = get_post_meta( $iPostID, 'my_custom_color', true ); | |
| $_sColor = $_sColor ? $_sColor : 'transparent'; | |
| return $sCell | |
| . '<div class="color-sample-container">' | |
| . '<p style="background-color: ' . esc_attr( $_sColor ) . '">' | |
| . '</p>' | |
| . '</div>'; | |
| } | |
| /** | |
| * Modifies the CSS rules of the admin page of the post listing table of the post type of this class. | |
| * @callback filter style_{instantiated class name} | |
| * @return string | |
| */ | |
| public function style_APF_Tutorial_PostType( $sStyle ) { | |
| return $sStyle . " | |
| .color-sample-container { | |
| height: 3em; | |
| } | |
| .color-sample-container p { | |
| border: solid 1px #CCC; | |
| width: 3em; | |
| height: 100%; | |
| } | |
| "; | |
| } | |
| /** | |
| * Modifies the output of the post content. | |
| * | |
| * This method is called in the single page of this class post type. | |
| * | |
| * Alternatively, you may use the 'content_{instantiated class name}' method, | |
| */ | |
| public function content( $sContent ) { | |
| $_sSelectedColor = get_post_meta( $GLOBALS['post']->ID, 'my_custom_color', true ); | |
| $_sSelectedColor = $_sSelectedColor ? $_sSelectedColor : 'transparent'; | |
| return "<h3>" . __( 'Selected Color', 'admin-page-framework-tutorial' ) . "</h3>" | |
| . "<div style='width: 100%;'>" | |
| . "<div style='margin:auto; width: 3em; height: 3em; background-color:" . $_sSelectedColor . "'></div>" | |
| . "</div>" | |
| ; | |
| } | |
| } | |
| class APF_Tutorial_CustomPostTypeMetaBox extends AdminPageFramework_MetaBox { | |
| /* | |
| * Use the setUp() method to define settings of this meta box. | |
| */ | |
| public function setUp() { | |
| /** | |
| * Adds setting fields in the meta box. | |
| */ | |
| $this->addSettingFields( | |
| array( | |
| 'field_id' => 'my_custom_color', | |
| 'type' => 'color', | |
| 'title' => __( 'Color', 'admin-page-framework-tutorial' ), | |
| ) | |
| ); | |
| } | |
| } | |
| new APF_Tutorial_PostType( 'apf_tutorial_posts' ); // the post type slug | |
| new APF_Tutorial_CustomPostTypeMetaBox( | |
| null, // meta box ID - can be null. | |
| __( 'Tutorial - Add a Meta Box to Custom Post Type', 'admin-page-framework-demo' ), // title | |
| array( 'apf_tutorial_posts' ), // post type slugs: post, page, etc. | |
| 'normal', // context | |
| 'low' // priority | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment