Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Gravity Forms photo upload WordPress

  • SOLVED

I came across this snippet, which does almost what I am looking for. Problem is I cannot get it to work with gravity forms, hoping someone can spot my error and help with the missing part.

Here is the [[LINK href="http://madebyraygun.com/blog/2012/upload-and-attach-multiple-images-from-the-wordpress-front-end/"]]snippet[[/LINK]]

Here is my page [[LINK href="http://pastie.org/4474578"]]template[[/LINK]]

Here is a screenshot of the [[LINK href="http://imgur.com/x4voP"]]form code[[/LINK]]

The missing part is that I want to map this to a set post_id rather than the current post of the form like it does, so if anyone could figure that out too I'd appreciate it.

Answers (2)

2012-08-14

Arnav Joy answers:

you have to change the following line of code

foreach ($_FILES as $file => $array) {
$newupload = insert_attachment($file,$post->ID);
}

see this $post->ID

you have to change this to your desired post id say 50

so here is the code


$mypostID = 50;

foreach ($_FILES as $file => $array) {
$newupload = insert_attachment($file,$mypostID);
}


Arnav Joy comments:

here is the full code

<?php
/*
Template Name: Add Photos
*/

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;
}


global $post;
$mypostID = 50; // change it to your desired post id

if ( $_FILES ) {
$files = $_FILES['input_1'];
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);

$_FILES = array("input_1" => $file);

foreach ($_FILES as $file => $array) {
//$newupload = insert_attachment($file,$post->ID); // this was your code

$newupload = insert_attachment($file,$mypostID); // this is my code

}
}
}
}

genesis();


see the line

$mypostID = 50; // change it to your desired post id


Kyle comments:

Okay, that may work for the second part. I can't tell yet

Still need to solve the Gravity Forms compatibility problem before I can tell if that's working

Thanks for the reply though


Arnav Joy comments:

i did not get you , can you be more clear about what you want?
sorry for miss confusion


Kyle comments:

The way I worded the question up top may not have been best because it is two parts.

(On the first line up top) The main problem is that I am using that code snippet you can see with that first link and the code snippet doesn't work (it wasn't made for Gravity Forms). I also attached the current page template and the code from the gravity form to help. Hoping someone sees where I messed up.


Arnav Joy comments:

try this

<?php
/*
Template Name: Add Photos
*/

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;
}


global $post;
$mypostID = 50; // change it to your desired post id

if ( $_FILES ) {
$files = $_FILES['input_1'];
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);

// $_FILES = array("input_1" => $file);

// foreach ($_FILES as $file => $array) {
//$newupload = insert_attachment($file,$post->ID); // this was your code

$newupload = insert_attachment($_FILES,$mypostID); // this is my code


}
}
}

genesis();


Kyle comments:

No visible change to process. I am still having the file submitted with nothing being added to a gallery/media library


Arnav Joy comments:

ok , can you show me the result of

<?php print_r( $_FILES);?>

and can you check if the image is uploading to the folder if not try setting the permission of the folder to 777


Kyle comments:

it just prints Array()

The files are uploading the the gravity forms file: /files/gravity_forms/11-27379e615c3b26b6d2e0b70167319e03/2012/08/bentrod14.jpg


Arnav Joy comments:

try this in functions.phpof your theme

<?php

add_action("gform_pre_submission_11", "gform_registration");

function gform_registration( $form_meta ){


if($_FILES['input_1']) {

$mypostID = 50; // change it to your desired post id


require_once(ABSPATH . 'wp-admin/includes/file.php');

require_once(ABSPATH . 'wp-admin/includes/image.php');

require_once(ABSPATH . 'wp-admin/includes/media.php');


$attach_id = media_handle_upload($_FILES, $mypostID);

update_post_meta($mypostID,'_thumbnail_id',$attach_id);



}

}

?>


Kyle comments:

Unfortunately still no change whatsoever


Arnav Joy comments:

ok try this



<?php


add_action("gform_post_submission_11", "post_submission", 10, 2);

function gform_registration($entry, $form){


if($_FILES['input_1']) {

$mypostID = 50; // change it to your desired post id


require_once(ABSPATH . 'wp-admin/includes/file.php');

require_once(ABSPATH . 'wp-admin/includes/image.php');

require_once(ABSPATH . 'wp-admin/includes/media.php');


$attach_id = media_handle_upload($_FILES, $mypostID);

update_post_meta($mypostID,'_thumbnail_id',$attach_id);



}

}

?>


Arnav Joy comments:

this one

<?php


add_action("gform_post_submission_11", "post_submission", 10, 2);

function post_submission($entry, $form){


if($_FILES['input_1']) {

$mypostID = 50; // change it to your desired post id


require_once(ABSPATH . 'wp-admin/includes/file.php');

require_once(ABSPATH . 'wp-admin/includes/image.php');

require_once(ABSPATH . 'wp-admin/includes/media.php');


$attach_id = media_handle_upload($_FILES, $mypostID);

update_post_meta($mypostID,'_thumbnail_id',$attach_id);



}

}

?>


Arnav Joy comments:

here is the updated code

add_action("gform_pre_submission", "post_submission");
function post_submission(){
if($_FILES['input_1']) {
$mypostID = 2; // change it to your desired post id
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/image.php');
require_once(ABSPATH . 'wp-admin/includes/media.php');
$attach_id = media_handle_upload('input_1', $mypostID);
update_post_meta($mypostID,'_thumbnail_id',$attach_id);
}
}


Kyle comments:

Works perfectly!!

2012-08-14

Martin Pham answers:

I think, was having some problem with your php code.
Current

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;
}

Wrong
// check to make sure its a successful upload
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) <strong>__return_false();</strong>

---
Fixed

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;
}


Kyle comments:

Thanks for the reply, no visible change in function