Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.
If the asker does not get an answer then they have 10 days to request a refund.
$20
Gravity Forms photo upload
Here is the snippet
Here is my page template
Here is a screenshot of the form code
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.
This question has been answered.
ktrusak | 08/14/12 at 12:42pm
Edit
(2) Possible Answers Submitted...
See a chronological view of answers?
Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.
-

Last edited:
08/14/12
12:51pmArnav Joy says: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);
}
- 08/14/12 12:53pm
Arnav Joy says: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
- 08/14/12 12:57pm
ktrusak says: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 - 08/14/12 12:58pm
Arnav Joy says:i did not get you , can you be more clear about what you want?
sorry for miss confusion - 08/14/12 1:07pm
ktrusak says: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. - 08/14/12 1:18pm
Arnav Joy says: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(); - 08/14/12 1:30pm
ktrusak says:No visible change to process. I am still having the file submitted with nothing being added to a gallery/media library
- 08/14/12 1:33pm
Arnav Joy says: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 - 08/14/12 1:39pm
ktrusak says:it just prints Array()
The files are uploading the the gravity forms file: /files/gravity_forms/11-27379e615c3b26b6d2e0b70167319e03/2012/08/bentrod14.jpg - 08/14/12 2:02pm
Arnav Joy says: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);
}
}
?> - 08/14/12 2:12pm
ktrusak says:Unfortunately still no change whatsoever
- 08/14/12 2:16pm
Arnav Joy says: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);
}
}
?>
- 08/14/12 2:17pm
Arnav Joy says: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);
}
}
?> - 08/14/12 3:10pm
Arnav Joy says: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);
}
} - 08/14/12 3:41pm
ktrusak says:Works perfectly!!
- 08/14/12 12:53pm
-

Last edited:
08/14/12
1:04pmMartin Pham says: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) __return_false();
---
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;
}
- 08/14/12 1:12pm
ktrusak says:Thanks for the reply, no visible change in function
- 08/14/12 1:12pm
This question has expired.
Gabriel Reguly, Hai Bui, Manoj Raj, ktrusak, Martin Pham voted on this question.
Current status of this question: Completed
Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.
If the asker does not get an answer then they have 10 days to request a refund.
