在WordPress世界中,它被称为“元框”,在您的情况下,它与帖子相同。您需要做的唯一不同的是保存功能,该功能应将数据保存在选项表中,下面是一个修改后的codex示例,该示例应作为您的起点:
<?php
/* Define the custom box */
add_action( \'add_meta_boxes\', \'myplugin_add_custom_box_WPA83147\' );
/* Adds a box to the main column on the Post and Page edit screens */
function myplugin_add_custom_box_WPA83147() {
add_meta_box(
\'myplugin_sectionid\',
__( \'My Post Section Title\', \'myplugin_textdomain\' ),
\'myplugin_inner_custom_box_WPA83147\',
\'post\'
);
}
/* Prints the box content */
function myplugin_inner_custom_box_WPA83147( $post ) {
// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), \'myplugin_noncename_WPA83147\' );
// The actual fields for data entry
// Use get_option to retrieve an existing value from the database and use the value for the form
$options = get_option(\'_WPA83147_options\', array());
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="\'.(isset($options[\'myplugin_new_field\']) ? $options[\'myplugin_new_field\'] : "").\'" size="25" />\';
}
/* Do something with the data entered */
add_action( \'save_post\', \'myplugin_save_postdata_WPA83147\' );
/* When the post is saved, saves our custom data */
function myplugin_save_postdata_WPA83147( $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;
// 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_WPA83147\'], plugin_basename( __FILE__ ) ) )
return;
// Check permissions
if ( \'page\' == $_POST[\'post_type\'] )
{
if ( !current_user_can( \'edit_page\', $post_id ) )
return;
}
else
{
if ( !current_user_can( \'edit_post\', $post_id ) )
return;
}
// OK, we\'re authenticated: we need to find and save the data
if (isset($_POST[\'myplugin_new_field\'])){
//sanitize user input
$mydata = sanitize_text_field( $_POST[\'myplugin_new_field\'] );
//get all saved options
$data = get_option(\'_WPA83147_options\', array());
//updated the field you need
$data[\'myplugin_new_field\'] = $mydata;
//store in the database
update_option(\'_WPA83147_options\', $$data);
}
}