首页    >    中文手册    >   常用函数-add_meta_box()

常用函数-add_meta_box()

常用函数-add_meta_box()

说明

add_meta_box在WordPress 2.5版本中被引入。插件开发者可通过该函数在Write Post,Write Page和Write Link编辑页面内添加版块。

用法

<?php add_meta_box('id', 'title', 'callback', 'page', 'context', 'priority'); ?>

示例

这是一个在文章和页面编辑界面上添加自定义版块的例子,可以在WordPress 2.5或更早版本中运行(未出现add_meta_box函数时)。

<?php
/* Use the admin_menu action to define the custom boxes */
add_action('admin_menu', 'myplugin_add_custom_box');

/* Use the save_post action to do something with the data entered */
add_action('save_post', 'myplugin_save_postdata');

/* Adds a custom section to the "advanced" Post and Page edit screens */
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 '<input type="hidden" name="myplugin_noncename" id="myplugin_noncename" value="' . 
    wp_create_nonce( plugin_basename(__FILE__) ) . '" />';

  // The actual fields for data entry

  echo '<label for="myplugin_new_field">' . __("Description for this field", 'myplugin_textdomain' ) . '</label> ';
  echo '<input type="text" name="myplugin_new_field" value="whatever" size="25" />';
}

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

  echo '<div class="dbx-b-ox-wrapper">' . "
";
  echo '<fieldset id="myplugin_fieldsetid" class="dbx-box">' . "
";
  echo '<div class="dbx-h-andle-wrapper"><h3 class="dbx-handle">' . 
        __( 'My Post Section Title', 'myplugin_textdomain' ) . "</h3></div>";   
   
  echo '<div class="dbx-c-ontent-wrapper"><div class="dbx-content">';

  // output editing form

  myplugin_inner_custom_box();

  // end wrapper

  echo "</div></div></fieldset></div>
";
}

/* When the post is saved, saves our custom data */
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(__FILE__) )) {
    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 
  // probably using add_post_meta(), update_post_meta(), or 
  // a custom table (see Further Reading section below)

   return $mydata;
}
?>

参数

$id

(字符)编辑界面的HTML'id'属性

title

(字符)编辑界面的标题,用户可见

callback

(字符)为编辑界面输出HTML代码的函数

page

(字符)显示编辑界面的Write界面类型(‘文章’,‘页面’或‘链接’)

context

(字符)显示编辑界面的页面部分(‘常规’,‘高级’或‘侧边’(自2.7版本))

priority

(字符)meta box所在内容中的优先级(‘高’或‘低’)

延伸阅读

外部资源

分类:中文手册

* 版权声明:作者WordPress啦! 转载请注明出处。