admin管理员组文章数量:1328020
I have an issue with submitting custom post from frontend with image. I have a form with post title, description and image, that should be featured image of the post. So when user submitting post, it actually creates with title and description, but without image, even in media library the image doesn't appear. Here is my form:
<form id="_themename-advert-create-form" method="post" enctype="multipart/form-data">
<?php wp_nonce_field('_themename_submit_advert_action','__themename_submit_advert_nonce'); ?>
<input type="hidden" name="_themename-advert-create-check" value="1" />
<label for="_themename-advert-create-title">Title</label>
<input id="_themename-advert-create-title" type="text" name="_themename-advert-create-title" />
<label for="_themename-advert-create-content">Sample Content</label>
<textarea id="_themename-advert-create-content" rows="8" name="_themename-advert-create-content"></textarea>
<label for="_themename-advert-create-image">Upload Post Image</label>
<input type="file" id="_themename-advert-create-image" name="_themename-advert-create-image" />
<input type="submit" id="_themename-advert-create-submit" value="SUBMIT" name="_themename-advert-create-submit" />
</form>
Here is my function that creates custom post:
add_action('init', '_themename_create_new_post');
function _themename_create_new_post(){
if('POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['_themename-advert-create-check']) && wp_verify_nonce( $_POST['__themename_submit_advert_nonce'], '_themename_submit_advert_action')) {
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
$new_post_title = sanitize_text_field($_POST['_themename-advert-create-title']);
$new_post_content = sanitize_textarea_field($_POST['_themename-advert-create-content']);
$new_post = array();
$new_post['post_author'] = $user_id;
$new_post['post_title'] = $new_post_title;
$new_post['post_content'] = $new_post_content;
$new_post['post_status'] = 'pending';
$new_post['post_name'] = 'pending';
$new_post['post_type'] = 'jana_usa';
$post_success = wp_insert_post($new_post);
if ( $_FILES ) {
$files = $_FILES["_themename-advert-create-image"];
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 ("_themename-advert-create-image" => $file);
foreach ($_FILES as $file => $array) {
// $newupload = frontend_handle_attachment( $file, $post_success );
if ($_FILES[$file]['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, $post_success );
// Set featured image
set_post_thumbnail($post_success, $attach_id);
}
}
}
}
}
}
After submittin I'm getting an error Warning: Invalid argument supplied for foreach() in ... on line 43
. This line:
foreach ($files['name'] as $key => $value) { ...
I can't find how to solve it. What am I doing wrong?
I have an issue with submitting custom post from frontend with image. I have a form with post title, description and image, that should be featured image of the post. So when user submitting post, it actually creates with title and description, but without image, even in media library the image doesn't appear. Here is my form:
<form id="_themename-advert-create-form" method="post" enctype="multipart/form-data">
<?php wp_nonce_field('_themename_submit_advert_action','__themename_submit_advert_nonce'); ?>
<input type="hidden" name="_themename-advert-create-check" value="1" />
<label for="_themename-advert-create-title">Title</label>
<input id="_themename-advert-create-title" type="text" name="_themename-advert-create-title" />
<label for="_themename-advert-create-content">Sample Content</label>
<textarea id="_themename-advert-create-content" rows="8" name="_themename-advert-create-content"></textarea>
<label for="_themename-advert-create-image">Upload Post Image</label>
<input type="file" id="_themename-advert-create-image" name="_themename-advert-create-image" />
<input type="submit" id="_themename-advert-create-submit" value="SUBMIT" name="_themename-advert-create-submit" />
</form>
Here is my function that creates custom post:
add_action('init', '_themename_create_new_post');
function _themename_create_new_post(){
if('POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['_themename-advert-create-check']) && wp_verify_nonce( $_POST['__themename_submit_advert_nonce'], '_themename_submit_advert_action')) {
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
$new_post_title = sanitize_text_field($_POST['_themename-advert-create-title']);
$new_post_content = sanitize_textarea_field($_POST['_themename-advert-create-content']);
$new_post = array();
$new_post['post_author'] = $user_id;
$new_post['post_title'] = $new_post_title;
$new_post['post_content'] = $new_post_content;
$new_post['post_status'] = 'pending';
$new_post['post_name'] = 'pending';
$new_post['post_type'] = 'jana_usa';
$post_success = wp_insert_post($new_post);
if ( $_FILES ) {
$files = $_FILES["_themename-advert-create-image"];
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 ("_themename-advert-create-image" => $file);
foreach ($_FILES as $file => $array) {
// $newupload = frontend_handle_attachment( $file, $post_success );
if ($_FILES[$file]['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, $post_success );
// Set featured image
set_post_thumbnail($post_success, $attach_id);
}
}
}
}
}
}
After submittin I'm getting an error Warning: Invalid argument supplied for foreach() in ... on line 43
. This line:
foreach ($files['name'] as $key => $value) { ...
I can't find how to solve it. What am I doing wrong?
Share Improve this question asked Jul 20, 2020 at 21:23 AkForDevAkForDev 1051 bronze badge 2 |1 Answer
Reset to default 0The problem was here:
<input type="file" id="_themename-advert-create-image" name="_themename-advert-create-image" />
It should be:
<input type="file" id="_themename-advert-create-image" name="_themename-advert-create-image[]" />
Still can't understand why I should add [] in my name, but now everything is working.
本文标签: plugin developmentCan39t upload image via submitting custom post from frontend
版权声明:本文标题:plugin development - Can't upload image via submitting custom post from frontend 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742245135a2439083.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
foreach ($files as ... )
– shanebp Commented Jul 20, 2020 at 21:46