Reputation: 917
I have a function working for wordpress front-end forms except for one thing. I can upload images from the front-end form but if I opt NOT to add a file, I get the following error:
Object of class WP_Error could not be converted to int in wp-includes/post.php on line 4365
Here is the code that handles the image upload part:
if ($_FILES) {
foreach ($_FILES as $file => $array) {
$newupload = insert_attachment($file, $pid);
}
}
function insert_attachment($file_handler,$post_id,$setthumb='false') {
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;
}
Note: This only happens when file is not uploaded. Seems like it's expecting an integer and throws an error if there is not file id or integer. How do I fix this? Thanks.
Upvotes: 1
Views: 403
Reputation: 917
I discovered that when I went from my local server to a staging server, the error disappeared. Guessing that it might be a wp-config or htaccess file difference that caused the error, not sure, but anyway it's no longer an issue.
Upvotes: 0