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 have a client who bought a wordpress theme that is built for posting job listings, a user can create an account and then post job listings but my client wants it converted to post Wheel listings (car wheels).

I have converted it pretty well so far but I need to allow the user to upload an image for a listing. A listing is simply a post but it is posted from a user facing form instead of in the admin panel.

I would like to utilize the the_post_thumbnail() is possible, is it possible for me to allow a user to upload through this without going into the admin panel?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

You might want to check out this resource which provides a relatively simple and easy to understand snippet to achieve just that!

Link to the original source from @Matthew Price.

I have pasted the code below for the purpose longevity but please note this is not my code nor am I taking credit for its authorship.

<?php
// if you have this in a file you will need to load "wp-load.php" to get access to WP functions.  If you post to "self" with this code then WordPress is by default loaded
require $_SERVER['DOCUMENT_ROOT'] . "/wp-load.php";
// require two files that are included in the wp-admin but not on the front end.  These give you access to some special functions below.
require $_SERVER['DOCUMENT_ROOT'] . "/wp-admin/includes/file.php";
require $_SERVER['DOCUMENT_ROOT'] . "/wp-admin/includes/image.php";

// required for wp_handle_upload() to upload the file
$upload_overrides = array( 'test_form' => FALSE );

global $current_user;
get_currentuserinfo();
$logged_in_user = $current_user->ID;

// count how many files were uploaded
$count_files = count( $_FILES['files'] );

// load up a variable with the upload direcotry
$uploads = wp_upload_dir();

// foreach file uploaded do the upload
foreach ( range( 0, $count_files ) as $i ) {

    // create an array of the $_FILES for each file
    $file_array = array(
        'name'      => $_FILES['files']['name'][$i],
        'type'      => $_FILES['files']['type'][$i],
        'tmp_name'  => $_FILES['files']['tmp_name'][$i],
        'error'     => $_FILES['files']['error'][$i],
        'size'      => $_FILES['files']['size'][$i],
    );

    // check to see if the file name is not empty
    if ( !empty( $file_array['name'] ) ) {

        // upload the file to the server
        $uploaded_file = wp_handle_upload( $file_array, $upload_overrides );

        // checks the file type and stores in in a variable
        $wp_filetype = wp_check_filetype( basename( $uploaded_file['file'] ), null );  

        // set up the array of arguments for "wp_insert_post();"
        $attachment = array(
            'post_mime_type' => $wp_filetype['type'],
            'post_title' => preg_replace('/\.[^.]+$/', '', basename( $uploaded_file['file'] ) ),
            'post_content' => '',
            'post_author' => $logged_in_user,
            'post_status' => 'inherit',
            'post_type' => 'attachment',
            'post_parent' => $_POST['post_id'],
            'guid' => $uploads['baseurl'] . '/' . $file_array['name']
        );

        // insert the attachment post type and get the ID
        $attachment_id = wp_insert_post( $attachment );

        // generate the attachment metadata
        $attach_data = wp_generate_attachment_metadata( $attachment_id, $uploaded_file['file'] );

        // update the attachment metadata
        wp_update_attachment_metadata( $attachment_id,  $attach_data );

                // this is optional and only if you want to.  it is here for reference only.
                // you could set up a separate form to give a specific user the ability to change the post thumbnail
                // set_post_thumbnail( $_POST['post_id', $attachment_id );

    }
}
// setup redirect.  i used the referer so that i can say "file uploaded" in a div if the files query string variable is set.
header("Location: " . $_SERVER['HTTP_REFERER'] . "/?files=uploaded");
?>

An example of the fron end form would be;

<!-- you can place the upload script into your theme folder or in a plugin -->
<form action="/path/to/upload/script/upload.php" method="POST" enctype="multipart/form-data">
    <input type="file" name="files[]" multiple>
    <!-- The following hidden field contains the post id for which you will be uploading images to -->
    <input type="hidden" name="post_id" value="100">
    <input type="submit" class="btn" value="Start Upload »">
</form>

You can adapt it to meet your needs.

share|improve this answer
    
Instead of $_SERVER['DOCUMENT_ROOT'], you should use ABSPATH. Btw: Did you try this? I'm pretty shure that the index $i in the foreach-loop will come before the name/type/tmp_name/etc. –  kaiser Apr 12 '12 at 4:25
    
@kaiser No I haven't tested this snippet, however a colleague has, thus I keep it on file. I use similar methodology, though this is one of the more easier snippets to understand and quite possibly adapt to a given scenario. Granted though I do agree with you about using ABSPATH as a matter of preference. Lastly, are you sure the index $i will come before the keys? Looks right to me... –  userabuser Apr 12 '12 at 10:34
1  
If your wp-dir is in a subdir, then $_SERVER['DOCUMENT_ROOT'] might fail, while ABSPATH was set to the root inside wp-config.php. Ad $i) I'd wonder if there weren't on array for each file, but instead on array of names of all files, another one of types for all files, etc. Makes things just harder to get around. –  kaiser Apr 12 '12 at 13:55
    
Thanks for the info and snippet. I haven't played with it yet but 1 thing I am not sure of, is currently I have an existing for that the user uses to make a post right now and I need to add this into that same form. I am not sure how to make sure this get's attached to my other post that will be created when the form is submitted –  jasondavis Apr 12 '12 at 21:36
    
@userabuser any ideas on this related question? wordpress.stackexchange.com/questions/119073/… ? –  codecowboy Oct 16 '13 at 13:14

You'll need to give users the ability to upload files, I've used User Role Editer to allow image uploads on a directory site. You select the user role of your general user and check the upload_files box to give them upload privileges.

share|improve this answer

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.