Create post from form with image
-
30-10-2019 - |
Вопрос
I found this wonderdul script that @Bainternet here at Wordpress Answers created, that makes it possible to attach images into posts from a html form..
I tried to combine in into my "html form to post" script.. I does create a post as draft in the backend.. but the image does not show up, and it doesn't show in "media" ..
This is @Bainternet script/answer, Submit post and upload image from front-end
This is mine,
<?php
if(isset($_POST['new_post']) == '1') {
$post_title = $_POST['post_title'];
$post_category = 'fashion';
$post_content = $_POST['post_content'];
$new_post = array(
'ID' => '',
'post_author' => $user->ID,
'post_category' => array($post_category),
'post_content' => $post_content,
'post_title' => $post_title,
'post_status' => 'draft'
);
$post_id = wp_insert_post($new_post);
if (!function_exists('wp_generate_attachment_metadata')){
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
}
if ($_FILES) {
foreach ($_FILES as $file => $array) {
if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
return "upload error : " . $_FILES[$file]['error'];
}
$attach_id = media_handle_upload( $file, $post_id );
}
}
if ($attach_id > 0){
//and if you want to set that image as Post then use:
update_post_meta($post_id,'_thumbnail_id',$attach_id);
}
// This will redirect you to the newly created post
$post = get_post($post_id);
wp_redirect( 'http://domain.com' );
}
?>
<form method="post" action="">
<input type="text" name="post_title" size="45" id="input-title" value="heading"/><br>
<input type="text" name="post_url" size="45" id="input-url" value="Url"/><br>
<input type="file" name="thumbnail" id="thumbnail"><br>
<textarea rows="5" name="post_content" cols="66" id="text-desc"></textarea> <br>
<input type="hidden" name="new_post" value="1"/> <br>
<input class="subput round" type="submit" name="submit" value="Post"/><br>
</form>
Нет правильного решения
Не связан с wordpress.stackexchange