I am developing a plugin where user is able to submit a post with a feature image. However, the feature image set to post but does not upload to directory.
HTML Input Field
<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 );
We’re dealing with a file here and not an (text) input.
Therefore, you need to change
$_POST['my_image_upload']
to$_FILES['my_image_upload']
as per your file form element:
Reference:
Plus, make sure the folder you are uploading to, has the right permissions to write to it.
Add error reporting to the top of your file(s) which will help find errors.
Sidenote: Displaying errors should only be done in staging, and never production.
Edit: Added by the OP in an edit, where I made a slight edit for the line above ^
I can not see your form so I only can make a guess.
Maybe you are missing the enctype attribute (enctype=”multipart/form-data”) on your form. This is required if you want upload images.
If do you have the enctype attribute in your form, please ignore this answer but post also your HTML here so we can see what is going on!