Function Reference/add meta box
本主题由 神墓 创建于 2009-10-29 0:34:39Description
The add_meta_box() function was introduced in WordPress 2.5. It allows plugin developers to add sections to the Write Post, Write Page, and Write Link editing pages.
Usage
<?php add_meta_box( $id, $title, $callback, $page, $context, $priority ); ?>
Parameters
$id
(string) HTML 'id' attribute of the edit screen section
$title
(string) Title of the edit screen section, visible to user
$callback
(string) Function that prints out the HTML for the edit screen section.
$page
(string) The type of Write screen on which to show the edit screen section ('post', 'page', or 'link')
$context
(string) The part of the page where the edit screen section should be shown ('normal', 'advanced' or (since 2.7) 'side')
$priority
(string) The priority within the context where the boxes should show ('high' or 'low')
Example
Here is an example that adds a custom section to the post and page editing screens. It also will work with WordPress 2.5 and also with earlier versions of WordPress (when the add_meta_box function did not exist).
<?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">' . "\n";
echo '<fieldset id="myplugin_fieldsetid" class="dbx-box">' . "\n";
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>\n";
}
/* 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;
}
// verify if this is an auto save routine. If it is our form has not been submitted, so we dont want
// to do anything
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
// Check permissions
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'];
// Do something with $mydata
// probably using add_post_meta(), update_post_meta(), or
// a custom table (see Further Reading section below)
return $mydata;
}
?>
http://codex.wordpress.org/Function_Reference/add_meta_box
放心注册,GeeKaa不会虚耗你半点时间,你只会发现更多乐趣。 | 立即注册 |