描述
add_meta_box() 函數(shù)是在 WordPress 2.5 添加的,用來給插件開發(fā)者添加 Meta模塊 到管理界面。
用法
<?php
add_meta_box( $id, $title, $callback, $post_type, $context,$priority, $callback_args );
?>
參數(shù)
$id
(字符串)(必需)Meta模塊的 HTML“ID”屬性
$title
(字符串)(必需)Meta模塊的標題,對用戶可見
$callback
(回調(diào))(必需)為Meta模塊輸出 HTML代碼的函數(shù)
$post_type
(字符串)(必需)顯示Meta模塊的文章類型,可以是文章(post)、頁面(page)、鏈接(link)、附件(attachment) 或 自定義文章類型(自定義文章類型的別名)
$context
(字符串)(可選)Meta模塊的顯示位置(’normal’,’advanced’, 或 ‘side’)
默認值:’advanced’
$priority
(字符串)(可選)Meta模塊顯示的優(yōu)先級別(’high’, ‘core’, ‘default’or ‘low’)
默認值: ‘default’
$callback_args
(數(shù)組)(可選)傳遞到 callback 函數(shù)的參數(shù)。callback 函數(shù)將接收 $post 對象和其他由這個變量傳遞的任何參數(shù)。
示例
下面是一個例子,在文章和頁面編輯界面上添加自定義欄目:
<?php
/* 定義自定義Meta模塊 */
add_action( 'add_meta_boxes', 'myplugin_add_custom_box' );
// 向后兼容(WP3.0前)
// add_action( 'admin_init', 'myplugin_add_custom_box', 1 );
/* 寫入數(shù)據(jù)*/
add_action( 'save_post', 'myplugin_save_postdata' );
/*在文章和頁面編輯界面的主欄中添加一個模塊 */
function myplugin_add_custom_box() {
$screens = array( 'post', 'page' );
foreach ($screens as $screen) {
add_meta_box(
'myplugin_sectionid',
__( 'My Post Section Title', 'myplugin_textdomain' ),
'myplugin_inner_custom_box',
$screen
);
}
}
/* 輸出模塊內(nèi)容 */
function myplugin_inner_custom_box( $post ) {
// 使用隨機數(shù)進行核查
wp_nonce_field( plugin_basename( __FILE__ ), 'myplugin_noncename' );
// 用于數(shù)據(jù)輸入的實際字段
// 使用 get_post_meta 從數(shù)據(jù)庫中檢索現(xiàn)有的值,并應(yīng)用到表單中
$value = get_post_meta( $post->ID, '_my_meta_value_key', true );
echo '<label for="myplugin_new_field">';
_e("Description for this field", 'myplugin_textdomain' );
echo '</label> ';
echo '<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="'.esc_attr($value).'" size="25" />';
}
/* 文章保存時,保存我們的自定義數(shù)據(jù)*/
function myplugin_save_postdata( $post_id ) {
// 首先,我們需要檢查當前用戶是否被授權(quán)做這個動作。
if ( 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) )
return;
} else {
if ( ! current_user_can( 'edit_post', $post_id ) )
return;
}
// 其次,我們需要檢查,是否用戶想改變這個值。
if ( ! isset( $_POST['myplugin_noncename'] ) || ! wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename( __FILE__ ) ) )
return;
// 第三,我們可以保存值到數(shù)據(jù)庫中
//如果保存在自定義的表,獲取文章ID
$post_ID = $_POST['post_ID'];
//過濾用戶輸入
$mydata = sanitize_text_field( $_POST['myplugin_new_field'] );
// 使用$mydata做些什么
// 或者使用
add_post_meta($post_ID, '_my_meta_value_key', $mydata, true) or
update_post_meta($post_ID, '_my_meta_value_key', $mydata);
// 或自定義表(見下面的進一步閱讀的部分)
}
?>
這是一個例子,如何從一個類內(nèi)部添加Meta模塊
/**
* 在文章編輯界面調(diào)用這個類
*/
function call_someClass()
{
return new someClass();
}
if ( is_admin() )
add_action( 'load-post.php', 'call_someClass' );
/**
* 這個類
*/
class someClass
{
const LANG = 'some_textdomain';
public function __construct()
{
add_action( 'add_meta_boxes', array( &$this, 'add_some_meta_box' ) );
}
/**
* 添加Meta模塊
*/
public function add_some_meta_box()
{
add_meta_box(
'some_meta_box_name'
,__( 'Some Meta Box Headline', self::LANG )
,array( &$this, 'render_meta_box_content' )
,'post'
,'advanced'
,'high'
);
}
/**
* 呈送Meta模塊內(nèi)容
*/
public function render_meta_box_content()
{
echo '<h1>TEST OUTPUT - this gets rendered inside the meta box.</h1>';
}
}
回調(diào)數(shù)組
$callback_args 數(shù)組將被傳遞給回調(diào)函數(shù)的第二個參數(shù)。第一個參數(shù)是這篇文章的 $post 對象。
// 這個函數(shù)添加一個帶有回調(diào)函數(shù) my_metabox_callback() 的Meta模塊
function add_my_meta_box() {
$var1 = 'this';
$var2 = 'that';
add_meta_box(
'metabox_id',
'Metabox Title',
'my_metabox_callback',
'page',
'normal',
'low',
array( 'foo' => $var1, 'bar' => $var2)
);
}
// $post 是一個包含當前文章的對象 (作為一個 $post 對象)
// $metabox 是一個數(shù)組,包含模塊 id, title, callback, and args elements.
// args element 是一個包含傳遞到 $callback_args 變量的數(shù)組
function my_metabox_callback ( $post, $metabox ) {
echo 'Last Modified: '.$post->post_modified; // 輸出文章最后編輯的時間
echo $metabox['args']['foo']; // 輸出 'this'
echo $metabox['args']['bar']; // 輸出 'that'
echo get_post_meta($post->ID,'my_custom_field',true); // 輸出自定義字段的值
}
源文件
add_meta_box() 位于 wp-admin/includes/template.php





這個和自定義字段有啥區(qū)別?
樓主,我想問一下,是不是把上面的代碼復(fù)制到主題下面的functions.php文件里面?如果是的話,我這里沒有顯示預(yù)期效果啊
add_action( ‘save_post’, ‘myplugin_save_postdata’ ); 不只是保存文章時會調(diào)用這個函數(shù), 在保存其他數(shù)據(jù)的時候也會調(diào)用, 比如保存菜單的時候
這個有價值