无法通过从 Wordpress 前端提交自定义 post 来上传图片
Can't upload image via submitting custom post from frontend in Wordpress
我在使用图像从前端提交自定义 post 时遇到问题。
我有一个带有 post 标题、描述和图像的表格,应该是 post 的特色图像。
所以当用户提交 post 时,它实际上创建了标题和描述,但没有图像,即使在媒体库中图像也不会出现。
这是我的表格:
<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>
这是我创建自定义的函数 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);
}
}
}
}
}
}
提交后出现错误 Warning: Invalid argument supplied for foreach() in ... on line 43
。这一行:
foreach ($files['name'] as $key => $value) { ...
我找不到解决方法。我做错了什么?
问题出在这里:
<input type="file" id="_themename-advert-create-image" name="_themename-advert-create-image" />
应该是:
<input type="file" id="_themename-advert-create-image" name="_themename-advert-create-image[]" />
仍然不明白为什么我要在我的名字中添加[],但现在一切正常。
我在使用图像从前端提交自定义 post 时遇到问题。 我有一个带有 post 标题、描述和图像的表格,应该是 post 的特色图像。 所以当用户提交 post 时,它实际上创建了标题和描述,但没有图像,即使在媒体库中图像也不会出现。 这是我的表格:
<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>
这是我创建自定义的函数 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);
}
}
}
}
}
}
提交后出现错误 Warning: Invalid argument supplied for foreach() in ... on line 43
。这一行:
foreach ($files['name'] as $key => $value) { ...
我找不到解决方法。我做错了什么?
问题出在这里:
<input type="file" id="_themename-advert-create-image" name="_themename-advert-create-image" />
应该是:
<input type="file" id="_themename-advert-create-image" name="_themename-advert-create-image[]" />
仍然不明白为什么我要在我的名字中添加[],但现在一切正常。