Skip to content

Instantly share code, notes, and snippets.

@michaeluno
Last active December 25, 2015 04:08
Show Gist options
  • Select an option

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

Select an option

Save michaeluno/67a38a9be9408bb6e903 to your computer and use it in GitHub Desktop.
Demonstrates how to add a meta box for the built-in 'post' post type with Admin Page Framework. This is a sample plugin introduced in the tutorial Add a Meta Box for Posts (http://en.michaeluno.jp/admin-page-framework/tutorials-v3/11-add-a-meta-box-for-posts/).
<?php
/*
* Plugin Name: Admin Page Framework Tutorial 11 - Add a Meta Box for Posts
* Plugin URI: http://en.michaeluno.jp/admin-page-framework
* Description: Adds a meta box for posts.
* Author: Michael Uno
* Author URI: http://michaeluno.jp
* Version: 1.0.1
* 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_SideMetaBox extends AdminPageFramework_MetaBox {
public function start() {
add_action( 'the_content', array( $this, 'replyToAddContent' ) );
}
/**
* Called when a post content is displayed.
*
* @callback filter the_content
* @return string
*/
public function replyToAddContent( $sContent ) {
if ( ! is_singular() ) {
return $sContent;
}
if ( ! is_main_query() ) {
return $sContent;
}
global $post;
if ( 'post' !== $post->post_type ) {
return $sContent;
}
$_sMetaData = get_post_meta( $post->ID, 'tutorial_normal_metabox_text', true );
return $sContent
. '<p>Admin Page Framework Tutorial Metabox Field: ' . $_sMetaData . '</p>';
}
/*
* 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' => 'tutorial_normal_metabox_text',
'type' => 'text',
'title' => __( 'Text', 'admin-page-framework-tutorial' ),
)
);
}
}
new APF_Tutorial_SideMetaBox(
null, // meta box ID - can be null.
__( 'Tutorial - Add a Meta Box for Posts', 'admin-page-framework-demo' ), // title
array( 'post' ), // post type slugs: post, page, etc.
'side', // context
'low' // priority
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment