我正在开发一个插件,用户可以使用功能图片提交帖子。但是,要素图像设置为发布但不上传到目录。
HTML输入字段
<form id="fep-new-post" name="new_post" method="post" action="" enctype="multipart/form-data">
<input placeholder="Add Post Title" type="text" id ="fep-post-title" name="post-title"/>
<p>
<?php
$settings = array(
'textarea_rows' => 12,
'teeny' => false,
'quicktags' => false,
'textarea_name' => 'post-content',
'media_buttons' => true,
'editor_class' => 'front-end-post',
'tinymce' => array(
'theme_advanced_buttons1' => 'formatselect,|,bold,italic,underline,|,' .
'bullist,blockquote,|,justifyleft,justifycenter' .
',justifyright,justifyfull,|,link,unlink,|' .
',spellchecker,wp_fullscreen,wp_adv'
)
);
wp_editor( '', 'content', $settings);
?>
</p>
<p>
<select id="user_submitted_categories" name="post-category">
<option value="<?php null ?>">Select category</option>
<?php
$categories = get_categories();
foreach ($categories as $category) {
$option = '<option value="'.$category->cat_ID.'">';
$option .= $category->cat_name;
$option .= '</option>';
echo $option;
}
?>
</select>
</p>
<input type="file" name="my_image_upload" id="my_image_upload" multiple="false" />
<p><input id="fep-tags" name="tags" type="text" tabindex="2" autocomplete="off" placeholder="Add Post tag"/></p>
<input class="button" onclick="requiredInput()" id="submit" type="submit" tabindex="3" value="<?php esc_attr_e( 'Publish', 'exclutips-fep' ); ?>" />
<input type="hidden" name="action" value="post" />
<input type="hidden" name="empty-description" id="empty-description" value="1"/>
<?php wp_nonce_field( 'new-post' ); ?>
</form>
PHP
$post_id = wp_insert_post( array(
'post_author' => $user_id,
'post_title' => $post_title,
'post_type' => 'post',
'post_content' => $post_content,
'post_category' => $post_category,
'tags_input' => $tags,
'post_status' => 'publish'
) );
$uploaddir = wp_upload_dir();
$post_image = $_POST['my_image_upload'];
$uploadfile = $uploaddir['path'] . '/' . basename( $post_image);
move_uploaded_file( $post_image, $uploadfile );
$filename = basename( $uploadfile );
$wp_filetype = wp_check_filetype(basename($filename), null );
$attachment = array(
'guid' => $uploaddir['url'] . '/' . basename( $filename ),
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $uploadfile, $post_id );
set_post_thumbnail( $post_id, $attach_id );
答案 0 :(得分:3)
我们在这里处理文件而不是(文本)输入。
因此,您需要将$_POST['my_image_upload']
更改为$_FILES['my_image_upload']
根据您的文件表单元素:
<input type="file" name="my_image_upload" id="my_image_upload" multiple="false" />
参考:
另外,请确保您要上传的文件夹具有写入该文件夹的正确权限。
将error reporting添加到文件的顶部,这有助于查找错误。
旁注:只应在暂存时进行显示错误,而不是生产。
编辑:在编辑中由OP添加,我对上面的行进行了轻微编辑^
$post_id = wp_insert_post( array(
'post_author' => $user_id,
'post_title' => $post_title,
'post_type' => 'post',
'post_content' => $post_content,
'post_category' => $post_category,
'tags_input' => $tags,
'post_status' => 'publish'
) );
//=============================================================
$uploaddir = wp_upload_dir();
$file = $_FILES['my_image_upload'];
$uploadfile = $uploaddir['path'] . '/' . basename( $file['name'] );
move_uploaded_file( $file['tmp_name'] , $uploadfile );
$filename = basename( $uploadfile );
$wp_filetype = wp_check_filetype(basename($filename), null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', $filename),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $uploadfile );
set_post_thumbnail( $post_id, $attach_id );
答案 1 :(得分:0)
我看不到你的表格,所以我只能猜一猜。
您的表单上可能缺少 enctype 属性(enctype =“multipart / form-data”)。如果您想要上传图片,则需要这样做。
如果您的表单中有 enctype 属性,请忽略此答案,但也请在此处发布您的HTML,以便我们了解正在发生的事情!