• Hi,

    I’m looking for a plug-in (or some functions.php code) to create an upload button on the front-end.

    I’ve got certain users who can’t access the back-end but need to upload files (into the media-library).
    Those users need an upload field + submit button, and their uploads need to get linked to the page/post they add files to.

    Does anybody know how to do this without using BuddyPress?

    Thanks in advance!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Yeah, I’m intersted in that too …

    I am looking for the same thing. Anyone?

    Ok, I found solution that worked for me (can’t remeber where exactly…)
    In functions.php add this:

    <?php
    function insert_attachment($file_handler,$post_id,$setthumb='false') {
    
      // check to make sure its a successful upload
      if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();
    
      require_once(ABSPATH . "wp-admin" . '/includes/image.php');
      require_once(ABSPATH . "wp-admin" . '/includes/file.php');
      require_once(ABSPATH . "wp-admin" . '/includes/media.php');
    
      $attach_id = media_handle_upload( $file_handler, $post_id );
    
      if ($setthumb) update_post_meta($post_id,'_thumbnail_id',$attach_id);
      return $attach_id;
    }
    ?>

    and in theme, in file where the file upload form field is add this :

    <?php
    if ($_FILES) {
    
    	foreach ($_FILES as $file => $array) {
    		$newupload = insert_attachment($file,$pid);
    	}
    };
    ?>

    This worked for me … There is a lot of coding arround this, I hope I extracted this well … ??

    I’m about to release a plugin to do something very similar. I’m just having one issue. I want to be able to save the “Title” of the attachment but I can’t seem to get it to store it. It’s probably simple, but message me and I can let you check it out soon.

    (assuming you still need it)

    Have you any idea if we could set the destination folder of the file that the user upload???

    Johnny B

    (@johnnybmarcallabscouk)

    Areimann, did you ever get your uploader to work. I am interested in a front end uploaded to give to users who have logged in to my site.

    Regards,

    Johnny B

    I am working in that too, BUT…
    When I add in functions.php this code

    <?php
    
    function insert_attachment($file, $post_id, $setthumb = 'false') {
    
        // check to make sure its a successful upload
        if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK)
            __return_false();
    
        $post_id = $_POST['post_id'];
    
        require_once(ABSPATH . "wp-admin" . '/includes/image.php');
        require_once(ABSPATH . "wp-admin" . '/includes/file.php');
        require_once(ABSPATH . "wp-admin" . '/includes/media.php');
    
        $attach_id = media_handle_upload($file, $post_id);
    
        if ($setthumb)
            update_post_meta($post_id, '_thumbnail_id', $attach_id);
        return $attach_id;
    }
    ?>

    And in the home.php after wp_insert_post part I add

    $wp_filetype = wp_check_filetype(basename($file), null);
        $attachment = array(
            'post_mime_type' => $wp_filetype['type'],
            'post_title' => preg_replace('/\.[^.]+$/', '', basename($file)),
            'post_content' => '',
            'post_status' => 'inherit'
        );
    
        $attach_id = wp_insert_attachment($attachment, $file, $post_id);
    // you must first include the image.php file
    // for the function wp_generate_attachment_metadata() to work
        require_once(ABSPATH . "wp-admin" . '/includes/image.php');
        $attach_data = wp_generate_attachment_metadata($attach_id, $file);
        wp_update_attachment_metadata($attach_id, $attach_data);

    Now I get a file uploaded in the media admin page and attached to it’s custom-post.
    But the file is not an image and in the front page Comes ” Missing Attachment

    I get tired and mad after ten days work in that, Can any body help please?

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Upload files from front-end’ is closed to new replies.