我对wordpres的开发相对来说比较陌生,我一直都在关注这个问题。我已经创建了一个自定义的帖子类型,它显示在管理菜单中。我需要一个带有文件上传字段的元数据库。metabox显示良好,但未保存文件。
我正在使用以下代码。任何帮助都将不胜感激。
//////////////////Best Practices///////////////
add_action( \'init\', \'wpb_register_cpt_best_practices\' );
function wpb_register_cpt_best_practices() {
$labels = array(
\'name\' => _x( \'Best Practices\', \'best practices\' ),
\'singular_name\' => _x( \'Best Practice\', \'best practice\' ),
\'add_new\' => _x( \'Add New\', \'best practice\' ),
\'add_new_item\' => _x( \'Add New best practice\', \'best practice\' ),
\'edit_item\' => _x( \'Edit best practice\', \'best practice\' ),
\'new_item\' => _x( \'New best practice\', \'best practice\' ),
\'view_item\' => _x( \'View best practice\', \'best practice\' ),
\'search_items\' => _x( \'Search best practices\', \'best practice\' ),
\'not_found\' => _x( \'No best practices found\', \'best practice\' ),
\'not_found_in_trash\' => _x( \'No best practices found in Trash\', \'best practice\' ),
\'parent_item_colon\' => _x( \'Parent best practice:\', \'best practice\' ),
\'menu_name\' => _x( \'Best practices\', \'best practice\' ),
);
$args = array(
\'labels\' => $labels,
\'hierarchical\' => false,
\'supports\' => array( \'title\', \'editor\', \'revisions\' ),
\'public\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'show_in_nav_menus\' => true,
\'publicly_queryable\' => true,
\'exclude_from_search\' => false,
\'has_archive\' => true,
\'query_var\' => true,
\'can_export\' => true,
\'rewrite\' => true,
\'capability_type\' => \'post\'
);
register_post_type( \'best practice\', $args );
}
$key_bp = "best practice";
$meta_boxes_bp = array(
"document" => array(
"name" => "document",
"title" => "Best Practices Document",
"description" => "Attach the best practices document.",
"type" => "file")
);
function wpb_create_meta_box3() {
global $key_bp;
if( function_exists( \'add_meta_box\' ) ) {
add_meta_box( \'new-meta-boxes\', ucfirst( $key_bp ) . \' Document\', \'display_meta_box3\', \'best practice\', \'normal\', \'high\' );
}
}
function display_meta_box3() {
global $post, $meta_boxes_bp, $key_bp;
?>
<div class="form-wrap">
<?php
wp_nonce_field( plugin_basename( __FILE__ ), $key_bp . \'_wpnonce\', false, true );
foreach($meta_boxes_bp as $meta_box) {
$data = get_post_meta($post->ID, $key_bp, true);
?>
<div class="form-field form-required">
<label for="<?php echo $meta_box[ \'name\' ]; ?>"><?php echo $meta_box[ \'title\' ]; ?></label>
<input type="file" name="<?php echo $meta_box[ \'name\' ]; ?>" value="<?php echo (isset($data[ $meta_box[ \'name\' ] ]) ? $data[ $meta_box[ \'name\' ] ] : \'\'); ?>" />
<p><?php echo $meta_box[ \'description\' ]; ?>
<?php //print_r($data);
?></p>
</div>
<?php } ?>
</div>
<?php
}
function wpb_save_meta_box3( $post_id ) {
global $post, $meta_boxes_bp, $key_bp;
foreach( $meta_boxes_bp as $meta_box ) {
if (isset($_POST[ $meta_box[ \'name\' ] ])) {
$data[ $meta_box[ \'name\' ] ] = $_POST[ $meta_box[ \'name\' ] ];
}
}
if (!isset($_POST[ $key_bp . \'_wpnonce\' ]))
return $post_id;
if ( !wp_verify_nonce( $_POST[ $key_bp . \'_wpnonce\' ], plugin_basename(__FILE__) ) )
return $post_id;
if ( !current_user_can( \'edit_post\', $post_id ))
return $post_id;
update_post_meta( $post_id, $key_bp, $data );
}
add_action( \'admin_menu\', \'wpb_create_meta_box3\' );
add_action( \'save_post\', \'wpb_save_meta_box3\' );