就我个人而言,我更喜欢按照以下模式向post类型添加自定义元处理程序。使用下面的命令,您可以通过调用add\\u post\\u type\\u support(“my\\u post\\u type”,“subtitle”)将支持键(“subtitle”)添加到post类型的支持数组中,从而将元支持添加到post类型中;
class Subtitle_Meta_Handler {
public function initialize() {
add_action(\'add_meta_boxes\', array($this, \'add_metabox\'), 10, 2);
add_action(\'save_post\', array($this, \'update\'));
}
public function add_metabox($post_type, $post)
{
if(post_type_supports($post_type, \'subtitle\'))
{
add_meta_box(\'subtitle\', \'Subtitle\', array($this, \'metabox\'), $post_type);
}
}
public function metabox($post)
{
$subtitle = get_post_meta($post->ID, \'subtitle\', true);
if(!$subtitle)
{
$subtitle = \'\';
}
?>
<input type="text" style="width: 70%;" value="<?php echo esc_attr($subtitle);?>" name="subtitle" id="subtitle">
<?php
wp_nonce_field(\'update_subtitle\', \'subtitle_nonce\');
}
public function update($post_id)
{
if(wp_is_post_autosave($post_id) || wp_is_post_revision($post_id)) {
return $post_id;
}
if(isset($_REQUEST[\'subtitle_nonce\']) && wp_verify_nonce($_REQUEST[\'subtitle_nonce\'], \'update_subtitle\')) {
$subtitle = trim(strip_tags($_REQUEST[\'subtitle\'], \'<b><strong><span><a>\'));
if(empty($subtitle)) {
delete_post_meta($post_id, \'subtitle\');
} else {
update_post_meta($post_id, \'subtitle\', $subtitle);
}
}
}
}
add_action(\'init\', array(new Subtitle_Meta_Handler(), \'initialize\'));
希望这样的东西很快就会添加到core中。