Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

This question already has an answer here:

I'm relatively new to wordpres development and am stuck at this issue. I have created a custom post type which shows up in the admin menu. I need a metabox in it with a file upload field. The metabox shows up fine but the file is not being saved.

I am using the following code for it. Any help would be greatly appreciated.

//////////////////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' );
share|improve this question

marked as duplicate by ialocin, Pieter Goosen, toscho Jul 2 at 14:50

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Please use the search to determine if your question has been answered before. –  ialocin Jul 2 at 10:13
    
I don't see you dealing with the file at all, like with $_FILES and so on. Take a look at this answer for that. Might actually be the better duplicate candidate. –  ialocin Jul 2 at 10:24

1 Answer 1

up vote -1 down vote accepted

For Wordpress metabox file upload in custom post. please refer Below link

http://code.tutsplus.com/articles/attaching-files-to-your-posts-using-wordpress-custom-meta-boxes-part-1--wp-22291

or you can use following plugins

https://wordpress.org/plugins/simple-fields/
http://www.advancedcustomfields.com/

both plugin best for custom field . you can also create reapeatable fields

share|improve this answer
1  
Please try to make sure that the answer itself can solve the problem. Link-only/-heavy answers are almost never able to do so. It is best to add the relevant code and explanation to the answer. –  ialocin Jul 2 at 10:29
    
These type of answers is bad for the site. Please improve your answers –  Pieter Goosen Jul 2 at 10:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.