此解决方案将在您的Edit Post 屏幕并显示四个其他表单字段-
然后,它会将数据保存到相应的DB表中,每个字段按您的指定在其自己的列中。因为您没有提供我猜到的表名wp_additional_post_info
, 但如果你愿意,你可以改变。
我没有向您提供任何内容,但是如果您想让metabox看起来更好一些,如果您愿意,可以添加一些CSS。
DB表这是DB表wp_additional_post_info
. 它的设置是为了在删除帖子时,此表中的相关信息也将从DB中删除。
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
CREATE TABLE IF NOT EXISTS `wp_additional_post_info` (
`post_id` bigint(20) unsigned NOT NULL DEFAULT \'0\',
`name` text NOT NULL,
`position` text NOT NULL,
`publications` longtext NOT NULL,
`freetext` longtext NOT NULL,
PRIMARY KEY (`post_id`),
UNIQUE KEY `post_id` (`post_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `wp_additional_post_info`
ADD CONSTRAINT `Costrain additional post info` FOREIGN KEY (`post_id`) REFERENCES `fgw_2_posts` (`ID`) ON DELETE CASCADE ON UPDATE NO ACTION;
将此放置在
functions.php (如果愿意,也可以包含一个外部文件)。
/**
* Add a metaboxs
*/
add_action( \'add_meta_boxes\', \'my_add_addition_post_info_meta_box\' );
function my_add_addition_post_info_meta_box(){
add_meta_box(
\'my_addition_post_info\', // ID
__(\'Additional Information\', \'myplugin_textdomain\'), // Title
\'my_fill_addition_post_info_meta_box\', // Callback
\'post\' // Post type
);
}
/**
* Print metabox content
*
* @param required WP_Post $post The object for the current post/page
*/
function my_fill_addition_post_info_meta_box($post){
/** Add a nonce field for security */
wp_nonce_field( \'my_addition_post_info\', \'my_addition_post_info_nonce\' );
/** Grab the additional info for this post */
global $wpdb;
$query = $wpdb->prepare(\'
SELECT *
FROM %1$s AS a
WHERE a.post_id = %2$s\',
$wpdb->prefix . \'additional_post_info\',
$post->ID
);
$info = $wpdb->get_row($query);
/**
* Output your form fields
*/
echo \'<div class="additional-field">\';
echo \'<label for="additional-name">Name</label>\';
printf(
\'<input type="text" id="additional-name" name="additional-name" value="%1$s" />\',
$info->name
);
echo \'</div>\';
echo \'<div class="additional-field">\';
echo \'<label for="additional-position">Position</label>\';
printf(
\'<input type="text" id="additional-position" name="additional-position" value="%1$s" />\',
$info->position
);
echo \'</div>\';
echo \'<div class="additional-field">\';
echo \'<label for="additional-publications">Publications</label>\';
echo \'<textarea type="text" id="additional-publications" name="additional-publications">\';
echo $info->publications;
echo \'</textarea>\';
echo \'</div>\';
echo \'<div class="additional-field">\';
echo \'<label for="additional-freetext">Freetext</label>\';
echo \'<textarea type="text" id="additional-freetext" name="additional-freetext">\';
echo $info->freetext;
echo \'</textarea>\';
echo \'</div>\';
}
/**
* Save metabox content
*
* @param required integer $post_id The ID of the post being saved
*/
add_action( \'save_post\', \'my_save_addition_post_info_meta_box\' );
function my_save_addition_post_info_meta_box( $post_id ) {
/** Check if the required nonce is set */
if(!isset($_POST[\'my_addition_post_info_nonce\']))
return;
/** Verify that the required nonce is valid */
if (!wp_verify_nonce($_POST[\'my_addition_post_info_nonce\'], \'my_addition_post_info\'))
return;
/** Ensure that this is not an autosave */
if(defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE)
return;
/** Ensure that the user has permission to be here */
if(!current_user_can(\'edit_post\', $post_id))
return;
/** Finally replace (or create) an entry for this post */
global $wpdb;
$table = $wpdb->prefix . \'additional_post_info\';
$data = array(
\'post_id\' => $post_id,
\'name\' => $_POST[\'additional-name\'],
\'position\' => $_POST[\'additional-position\'],
\'publications\' => $_POST[\'additional-publications\'],
\'freetext\' => $_POST[\'additional-freetext\']
);
$format = array(\'%d\', \'%s\', \'%s\', \'%s\', \'%s\');
$wpdb->replace($table, $data, $format);
}