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.

I've made a custom meta box where you can upload an image attached to the post. I've done this since the image isn't placed in the normal content area but separate.

/*** Custom post upload ***/
function add_custom_meta_boxes() {

    // Define the custom attachment for posts
    add_meta_box(
        'wp_custom_attachment',
        'Sidobild uppladdning',
        'wp_custom_attachment',
        'post',
        'side'
    );

    // Define the custom attachment for pages
    add_meta_box(
        'wp_custom_attachment',
        'Sidobild uppladdning',
        'wp_custom_attachment',
        'page',
        'side'
    );
}
add_action('add_meta_boxes', 'add_custom_meta_boxes');

function wp_custom_attachment() {       
    $html = '<p class="description">';
        $html .= 'Ladda upp din bild här!';
    $html .= '</p>';
    $customAttach = get_post_meta(get_the_ID(), 'wp_custom_attachment', true);
    $html .= '<img src="'. $customAttach['url'] .'" alt="" />';
        $html .= '<input type="file" id="wp_custom_attachment" name="wp_custom_attachment" value="" size="25" />';
        $html .= '<input type="submit" value="Spara" />';

    // This is just me having an ugly stab at removing it
        $html .= '<input type="hidden" id="wp_custom_attachment_empty" name="wp_custom_attachment" value="" size="25" />';
        $html .= '<input type="submit" value="Ta bort" />';

    echo $html;

} // end wp_custom_attachment   
function save_custom_meta_data($id) {

    /* --- security verification --- */       
    if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
      return $id;
    } // end if

    if('page' == $_POST['post_type']) {
      if(!current_user_can('edit_page', $id)) {
        return $id;
      } // end if
    } else {
        if(!current_user_can('edit_page', $id)) {
            return $id;
        } // end if
    } // end if
    /* - end security verification - */

    // Make sure the file array isn't empty
    if(!empty($_FILES['wp_custom_attachment']['name'])) {

        // Setup the array of supported file types. In this case, it's just PDF.
        $supported_types = array('image/jpg', 'image/jpeg', 'image/png', 'image/gif');

        // Get the file type of the upload
        $arr_file_type = wp_check_filetype(basename($_FILES['wp_custom_attachment']['name']));
        $uploaded_type = $arr_file_type['type'];

        // Check if the type is supported. If not, throw an error.
        if(in_array($uploaded_type, $supported_types)) {

            // Use the WordPress API to upload the file
            $upload = wp_upload_bits($_FILES['wp_custom_attachment']['name'], null, file_get_contents($_FILES['wp_custom_attachment']['tmp_name']));

            if(isset($upload['error']) && $upload['error'] != 0) {
                wp_die('There was an error uploading your file. The error is: ' . $upload['error']);
            } else {
                add_post_meta($id, 'wp_custom_attachment', $upload);
                update_post_meta($id, 'wp_custom_attachment', $upload);     
            } // end if/else

        } else {
            wp_die("The file type that you've uploaded is not an image.");
        } // end if/else

    } // end if

} // end save_custom_meta_data
add_action('save_post', 'save_custom_meta_data');

function update_edit_form() {
    echo ' enctype="multipart/form-data"';
}
add_action('post_edit_form_tag', 'update_edit_form');   
/*** END Custom post upload ***/

I've followed a tutorial, I'm quite good with php but not familiar with everything in wordpress. I've found the delete metadata function but I just can't figure out how to implement this with the interface.

I want the user to have a remove button so they can unset the upload if they wish!

share|improve this question
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.