数字化技能提升教程,数字化时代生存宝典

数字化百科 / 产品文档 / WordPress:Function Reference/add meta box

WordPress:Function Reference/add meta box

查看全部产品文档命名空间:WordPress

描述

add_meta_box()函数是WordPress2.5版本引进的,能够使得插件开发人员,给编写文章,编写网页,和编写链接,之列的编辑网页,添加一些部分。

用法

%%% %%%

例子

下面有一个例子,给文章和网页编辑界面添加了一个自定义的部分,在WordPress2.5版本以及其它更早发行的WordPress版本中能够运行(add_meta_box函数不存在的时候)。


/*使用 admin_menu action 定义自定义框*/
add_action('admin_menu', 'myplugin_add_custom_box');

/* 使用 save_post action 处理输入的数据*/
add_action('save_post', 'myplugin_save_postdata');

/* 向 "advanced"文章和网页编辑界面添加一个自定义的部分*/
function myplugin_add_custom_box() {

if( function_exists( 'add_meta_box' )) {
add_meta_box( 'myplugin_sectionid', __( 'My Post Section Title', 'myplugin_textdomain' ),
'myplugin_inner_custom_box', 'post', 'advanced' );
add_meta_box( 'myplugin_sectionid', __( 'My Post Section Title', 'myplugin_textdomain' ),
'myplugin_inner_custom_box', 'page', 'advanced' );
} else {
add_action('dbx_post_advanced', 'myplugin_old_custom_box' );
add_action('dbx_page_advanced', 'myplugin_old_custom_box' );
}
}

/* Prints the inner fields for the custom post/page section */
function myplugin_inner_custom_box() {

// Use nonce for verification

echo '';

// The actual fields for data entry

echo ' ';
echo '';
}

/* Prints the edit form for pre-WordPress 2.5 post/page */
function myplugin_old_custom_box() {

echo '

' . "\n";
echo '
' . "\n";
echo '

' .
__( 'My Post Section Title', 'myplugin_textdomain' ) . "

";

echo '
';

// output editing form

myplugin_inner_custom_box();

// end wrapper

echo "

\n";
}

/* 当文章被保存时,保存我们定制的数据 */
function myplugin_save_postdata( $post_id ) {

// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times

if ( !wp_verify_nonce( $_POST[myplugin_noncename], plugin_basename() )) {
return $post_id;
}

if ( 'page' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ))
return $post_id;
} else {
if ( !current_user_can( 'edit_post', $post_id ))
return $post_id;
}

// OK, we're authenticated: we need to find and save the data

$mydata = $_POST['myplugin_new_field'];

// TODO: Do something with $mydata

return $mydata;
}

if( function_exists( 'add_meta_box' )) {
add_meta_box( 'myplugin_sectionid', __( 'My Post Section Title', 'myplugin_textdomain' ),
'myplugin_inner_custom_box', 'post', 'advanced' );
add_meta_box( 'myplugin_sectionid', __( 'My Post Section Title', 'myplugin_textdomain' ),
'myplugin_inner_custom_box', 'page', 'advanced' );
} else {
add_action('dbx_post_advanced', 'myplugin_old_custom_box' );
add_action('dbx_page_advanced', 'myplugin_old_custom_box' );
}
}

/*为自定义文章/网页部分输出inner fields */
function myplugin_inner_custom_box() {

// 使用现时确认

echo '';

// 数据登记项的真正的fields
echo ' ';
echo '';
}

/*为WordPress 2.5 版本之前的文章/网页输出编辑形式*/
function myplugin_old_custom_box() {

echo '

' . "\n";
echo '
' . "\n";
echo '

' .
__( 'My Post Section Title', 'myplugin_textdomain' ) . "

";

echo '
';

// 输出编辑形式

myplugin_inner_custom_box();

// end wrapper

echo "

\n";
}

/* 文章保存后,保存我们的自定义数据、 */
function myplugin_save_postdata( $post_id ) {

// 使用适当的权限确认界面内容
// 因为 save_post 可能在其它时候触发

if ( !wp_verify_nonce( $_POST[myplugin_noncename], plugin_basename() )) {
return $post_id;
}

if ( 'page' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ))
return $post_id;
} else {
if ( !current_user_can( 'edit_post', $post_id ))
return $post_id;
}

// 我们已经得到了鉴别;我们需要找到并且保存数据
$mydata = $_POST['myplugin_new_field'];

// TODO:用 $mydata执行一些步骤

return $mydata;
}

参数

;$id : (string)编辑界面部分的HTML 'id'属性

;title : (string)编辑界面部分的标题,用户可以看到

;callback : (string)为编辑界面部分输出HTML的函数

;page : (string)编写界面的类型,现时了编辑界面部分('post', 'page', or 'link')

;context : (string)显示编辑界面的网页的那个部分('normal' 或者'advanced')

深入阅读