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 need to add [attach thumbnail] to this form

functions.php Code:

if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "new_post") {

    // Do some minor form validation to make sure there is content
    $title =  $_POST['title'];
    $ordertype = $_POST['ordertype'];
    $price = $_POST['price'];
    $ordertime = $_POST['ordertime'];

    // Add the content of the form to $post as an array
    $new_post = array(
        'post_title'    => $title,
        'post_content'  => $description,
        'post_category' => array($_POST['category']),
        'post_status'   => 'publish',
        'post_type' => 'post'
    );
    //save the new post
    $pid = wp_insert_post($new_post); 
    add_post_meta($pid, 'ordertype', $ordertype, true);
    add_post_meta($pid, 'price', $price, true);
    add_post_meta($pid, 'ordertime', $ordertime, true);
    wp_redirect('/'); exit;
    //insert taxonomies
}

source: Form to Add Posts to Custom Post Type

share|improve this question
add comment

2 Answers

Its pretty simple actually. Here is the link for the set_thumbnail function reference codex. And here is the answer for the file upload.

First off all you have to attach the file to the post :

function attach_uploads($uploads,$post_id = 0){
$files = rearrange($uploads);
if($files[0]['name']==''){
    return false;   
}
foreach($files as $file){
    $upload_file = wp_handle_upload( $file, array('test_form' => false) );
    $attachment = array(
    'post_mime_type' => $upload_file['type'],
    'post_title' => preg_replace('/\.[^.]+$/', '', basename($upload_file['file'])),
    'post_content' => '',
    'post_status' => 'inherit'
);
    $attach_id = wp_insert_attachment( $attachment, $upload_file['file'], $post_id );
    $attach_array[] = $attach_id;
    require_once(ABSPATH . 'wp-admin/includes/image.php');
    $attach_data = wp_generate_attachment_metadata( $attach_id, $upload_file['file'] );
    wp_update_attachment_metadata( $attach_id, $attach_data );
}
return $attach_array;
}

More detail about the php file upload rearrangement : php.net.

And this is the function to set the uploaded file as post thumbnail. Put it in the corresponding wp_ajax function.

// put this one early, before any process
$files = $_FILES['profile-picture'];

// insert attachment, after you have the new post id of course
$attached_files = attach_uploads($files,$pid);

// set the first file as post thumbnail
// $attached_files[0] is for the first file

if($attached_files){
    set_post_thumbnail( $pid, $attached_files[0] ); 
}

Hope this help

ADDED :

function rearrange( $arr ){
    foreach( $arr as $key => $all ){
        foreach( $all as $i => $val ){
            $new[$i][$key] = $val;    
        }    
    }
    return $new;
}

Just put it in the functions.php

share|improve this answer
    
i add first code in functions.php, but another codes i don`t know how can i use it..! –  Mohannad Qamara Mar 6 '12 at 3:22
    
i add my source code in functions.php before your code.. how can i add your code to mine? –  Mohannad Qamara Mar 6 '12 at 4:08
    
Both of them should be in the functions.php. Put this $files = $_FILES['profile-picture']; after $ordertime = $_POST['ordertime']; and put the rest add_post_meta($pid, 'ordertime', $ordertime, true); –  ifdion Mar 6 '12 at 5:16
    
can you check my new answer? –  Mohannad Qamara Mar 6 '12 at 6:33
add comment

thanks @ifdion .. That is my finally functions.php code

if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "new_post") {

    // Do some minor form validation to make sure there is content
    $title =  $_POST['title'];
    $ordertype = $_POST['ordertype'];
    $price = $_POST['price'];
    $ordertime = $_POST['ordertime'];
    $files = $_FILES['profile-picture'];

    // Add the content of the form to $post as an array
    $new_post = array(
        'post_title'    => $title,
        'post_content'  => $description,
        'post_category' => array($_POST['category']),  // Usable for custom taxonomies too
        'post_status'   => 'publish',           // Choose: publish, preview, future, draft, etc.
        'post_type' => 'post'  //'post',page' or use a custom post type if you want to
    );
    //save the new post
    $pid = wp_insert_post($new_post); 
    add_post_meta($pid, 'ordertype', $ordertype, true);
    add_post_meta($pid, 'price', $price, true);
    add_post_meta($pid, 'ordertime', $ordertime, true);
    $attached_files = attach_uploads($files,$pid);
    if($attached_files){
    set_post_thumbnail( $pid, $attached_files[0] ); 
    }
    wp_redirect(get_permalink($pid)); exit;
    //insert taxonomies
}
function attach_uploads($uploads,$post_id = 0){
    $files = rearrange($uploads);
    if($files[0]['name']==''){
        return false;   
    }
    foreach($files as $file){
        $upload_file = wp_handle_upload( $file, array('test_form' => false) );
        $attachment = array(
        'post_mime_type' => $upload_file['type'],
        'post_title' => preg_replace('/\.[^.]+$/', '', basename($upload_file['file'])),
        'post_content' => '',
        'post_status' => 'inherit'
    );
        $attach_id = wp_insert_attachment( $attachment, $upload_file['file'], $post_id );
        $attach_array[] = $attach_id;
        require_once(ABSPATH . 'wp-admin/includes/image.php');
        $attach_data = wp_generate_attachment_metadata( $attach_id, $upload_file['file'] );
        wp_update_attachment_metadata( $attach_id, $attach_data );
    }
    return $attach_array;
}

and this upload [input] in theme..

<input type="file" id="profile-picture" name="profile-picture[]" size="40" multiple />

but if i do submit for form, responding with this error...

Fatal error: Call to undefined function rearrange()

sorry for your time

share|improve this answer
    
here is the link for the rearrange function php.net. do give it a try –  ifdion Mar 6 '12 at 7:21
    
MR @ifdion, I try it, really i can`t do it :( –  Mohannad Qamara Mar 9 '12 at 0:14
    
can you give more detail? is there any error message? have you put the rearrange() function? –  ifdion Mar 9 '12 at 2:34
    
yeah, same error message [Fatal error: Call to undefined function rearrange()] –  Mohannad Qamara Mar 9 '12 at 6:24
    
have you put the rearrange() function? the one on my added answer? –  ifdion Mar 9 '12 at 8:42
show 2 more comments

Your Answer

 
discard

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

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