0

i am trying to create an meta image uploader for a a custom post type with media_handle_upload() but it only returns an error teling me that it could not move the image to the folder "wp-content/year/month" - regular uploads do work without problems, there is should not be a directory permission problem...

I was using wp_insert_attachment before, but then my uploads had no thumbnails inside the media library for some reason, also the WP Codex stated that I should use media_handle_upload to save additional function calls. Can anybody help? This is my code:

/// SAVE MITARBEITER META DATA
function mitarbeiter_save_post_meta($post_id,$post){
    /// GET THE POST TYPE OBJECT
    $post_type = get_post_type_object( $post->post_type );

    /// CHECK IF THE CURRENT USER HAS THE NEEDED PERMISSION
    if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
        return $post_id;

    /// PREVENT CLEARING THE CUSTOMFIELDS BY AUTOSAVE, QUICK EDIT OR BULK EDIT
    if ((defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) || (defined('DOING_AJAX') && DOING_AJAX) || isset($_REQUEST['bulk_edit']))
        return;

    /// UPDATE DATA
    $mitarbeiter_leitung = (isset($_POST['mitarbeiter_leitung'])) ? $_POST['mitarbeiter_leitung'] : "";
    $mitarbeiter_email = (isset($_POST['mitarbeiter_email'])) ? $_POST['mitarbeiter_email'] : "";
    $mitarbeiter_position = (isset($_POST['mitarbeiter_position'])) ? $_POST['mitarbeiter_position'] : "";
    $mitarbeiter_position_extra = (isset($_POST['mitarbeiter_position_extra'])) ? $_POST['mitarbeiter_position_extra'] : "";
    $mitarbeiter_position_EN = (isset($_POST['mitarbeiter_position_EN'])) ? $_POST['mitarbeiter_position_EN'] : "";
    $mitarbeiter_position_extra_EN = (isset($_POST['mitarbeiter_position_extra_EN'])) ? $_POST['mitarbeiter_position_extra_EN'] : "";
    update_post_meta( $post_id, 'mitarbeiter_leitung', $mitarbeiter_leitung );
    update_post_meta( $post_id, 'mitarbeiter_email', $mitarbeiter_email );
    update_post_meta( $post_id, 'mitarbeiter_position', $mitarbeiter_position );
    update_post_meta( $post_id, 'mitarbeiter_position_extra', $mitarbeiter_position_extra );
    update_post_meta( $post_id, 'mitarbeiter_position_EN', $mitarbeiter_position_EN );
    update_post_meta( $post_id, 'mitarbeiter_position_extra_EN', $mitarbeiter_position_extra_EN );

    /// CUSTOM IMAGE UPLOAD
    if(!empty($_FILES['custom_upload']['name'])){ //New upload
        require_once( ABSPATH . 'wp-admin/includes/file.php' );
        $override['action'] = 'editpost';
        $uploaded_file = wp_handle_upload($_FILES['custom_upload'], $override);
        $post_id = $post->ID;
        $attachment = array(
            'post_title' => $_FILES['custom_upload']['name'],
            'post_content' => '',
            'post_type' => 'attachment',
            'post_parent' => $post->ID,
            'post_mime_type' => $_FILES['custom_upload']['type'],
            'guid' => $uploaded_file['url']
        );
        #$id = wp_insert_attachment( $attachment,$_FILES['custom_upload'][ 'file' ],$post->ID);
        #wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $_FILES['custom_upload']['file'] ) );
        $id = media_handle_upload('custom_upload',$post->ID);
        update_post_meta($post->ID, "custom_upload",$id);
    }
}
add_action( 'save_post', 'mitarbeiter_save_post_meta', 10, 2 );

/// MAKE FILEUPLOAD POSSIBLE
function fileupload_metabox_header(){
    ?>
    <script>
    jQuery(document).ready(function(){
        // Das ist ein Test
        jQuery("form#post").attr("enctype","multipart/form-data");
        jQuery("form#post").attr("encoding","multipart/form-data");
    });
    </script>
    <?php
}
add_action('admin_head', 'fileupload_metabox_header');

1 Answer 1

0

You're doing it twice - media_handle_upload is a wrapper for wp_handle_upload.

Just use the former like so:

require_once ABSPATH . 'wp-admin/includes/file.php';
$id = media_handle_upload(
    'custom_upload',
    $post->ID,
    array(), // Attachment post data overrides, WordPress will do all the grunt work by default
    array(
        'test_form' => false,
        'action' => 'editpost',
    )
);

if ( ! is_wp_error( $id ) )
    update_post_meta( $post->ID, 'custom_upload', $id );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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