Been stuck on this for a while now -- This function stopped working on a site I maintain recently, and I'm not sure exactly why. It is failing at the first media_handle_upload function and gives me this error:
Array([error] => Specified file failed upload test.
Tried using this code instead of media_handle_upload, https://gist.github.com/hissy/7352933 , but in with the code from the gist I got this error:
017/05/17 05:45:59 [error] 2066#2066: *10499 FastCGI sent in stderr: "PHP message: PHP Warning: file_get_contents(/tmp/php8mjcrC): failed to open stream: No such file or directory in /srv/www/test.dev/current/web/app/themes/test/functions.php on line 1083
Here is my function. It is hooking into a Gravity Forms form that accepts user submitted photos. I want these to be auto-populated in the media library. It used to work, I'm not sure what broke it, potentially something in WordPress v4.7
add_action("gform_pre_submission_16", "post_submission");
function post_submission(){
$body = 'From: '.$_POST['input_2'];
$body .= '<br/>Email: '.$_POST['input_3'];
$body .= '<br/>Description: '.$_POST['input_5'];
$mypostID = 0; // change it to your desired post id
$photo_description = $_POST['input_5'];
$photo_credit = $_POST['input_2'];
if( !empty($_FILES['input_13']['name'])) {
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_13', $mypostID);
$strain_cat_id = $_POST['input_20'];
$cat_id = $_POST['input_21'];
wp_set_object_terms( $attach_id, $strain_cat_id, 'media_category', true );
wp_set_object_terms( $attach_id, $cat_id, 'media_category', true );
wp_set_object_terms( $attach_id, 170, 'media_category', true );
$my_post = array(
'ID' => $attach_id,
'post_content' => $photo_description
);
wp_update_post( $my_post );
update_post_meta( $attach_id, 'photo_credit', $photo_credit );
$body .= '<br/><a href="http://darkheartnursery.com/wp-admin/upload.php?item='.$attach_id.'">View Photo for '.$strain_cat_id.'</a>';
}
if( !empty($_FILES['input_14']['name']) ) {
$attach_id = media_handle_upload('input_14', $mypostID);
$strain_cat_id = $_POST['input_22'];
$cat_id = $_POST['input_26'];
wp_set_object_terms( $attach_id, $strain_cat_id, 'media_category', true );
wp_set_object_terms( $attach_id, $cat_id, 'media_category', true );
wp_set_object_terms( $attach_id, 170, 'media_category', true );
$my_post = array(
'ID' => $attach_id,
'post_content' => $photo_description
);
wp_update_post( $my_post );
update_post_meta( $attach_id, 'photo_credit', $photo_credit );
$body .= '<br/><a href="http://darkheartnursery.com/wp-admin/upload.php?item='.$attach_id.'">View Photo for '.$strain_cat_id.'</a>';
}
if( !empty($_FILES['input_15']['name']) ) {
$attach_id = media_handle_upload('input_15', $mypostID);
$strain_cat_id = $_POST['input_23'];
$cat_id = $_POST['input_27'];
wp_set_object_terms( $attach_id, $strain_cat_id, 'media_category', true );
wp_set_object_terms( $attach_id, $cat_id, 'media_category', true );
wp_set_object_terms( $attach_id, 170, 'media_category', true );
$my_post = array(
'ID' => $attach_id,
'post_content' => $photo_description
);
wp_update_post( $my_post );
update_post_meta( $attach_id, 'photo_credit', $photo_credit );
$body .= '<br/><a href="http://darkheartnursery.com/wp-admin/upload.php?item='.$attach_id.'">View Photo for '.$strain_cat_id.'</a>';
}
if( !empty($_FILES['input_16']['name']) ) {
$attach_id = media_handle_upload('input_16', $mypostID);
$strain_cat_id = $_POST['input_24'];
$cat_id = $_POST['input_28'];
wp_set_object_terms( $attach_id, $strain_cat_id, 'media_category', true );
wp_set_object_terms( $attach_id, $cat_id, 'media_category', true );
wp_set_object_terms( $attach_id, 170, 'media_category', true );
$my_post = array(
'ID' => $attach_id,
'post_content' => $photo_description
);
wp_update_post( $my_post );
update_post_meta( $attach_id, 'photo_credit', $photo_credit );
$body .= '<br/><a href="http://darkheartnursery.com/wp-admin/upload.php?item='.$attach_id.'">View Photo for '.$strain_cat_id.'</a>';
}
if( !empty($_FILES['input_17']['name']) ) {
$attach_id = media_handle_upload('input_17', $mypostID);
$strain_cat_id = $_POST['input_25'];
$cat_id = $_POST['input_29'];
wp_set_object_terms( $attach_id, $strain_cat_id, 'media_category', true );
wp_set_object_terms( $attach_id, $cat_id, 'media_category', true );
wp_set_object_terms( $attach_id, 170, 'media_category', true );
$my_post = array(
'ID' => $attach_id,
'post_content' => $photo_description
);
wp_update_post( $my_post );
update_post_meta( $attach_id, 'photo_credit', $photo_credit );
}
}
Really appreciate any help or guidance on this. I can provide more clarity or code if needed as well. Thanks!