Crea post dal modulo con l'immagine
-
30-10-2019 - |
Domanda
Ho trovato questo script WonderDul che @bainternet qui a WordPress Risposte creata, che consente di allegare immagini a post da un modulo HTML ..
Ho provato a combinarmi nel mio script "HTML Form to Post" .. Crea un post come bozza nel backend .. ma l'immagine non viene visualizzata e non viene visualizzata in "Media" ..
Questo è @bainternet script/risposta,Invia Post e Carica immagine da front-end
Questo è mio,
<?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>
Nessuna soluzione corretta
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a wordpress.stackexchange