Question

J'ai trouvé ce script WonderDul que @bainternet ici sur WordPress Answers créé, qui permet d'attacher des images dans des articles d'un formulaire HTML.

J'ai essayé de se combiner dans mon script "HTML Form to Publier" .. Je crée un post comme brouillon dans le backend .. mais l'image n'apparaît pas, et elle ne s'affiche pas dans "Media" ..

C'est le script / réponse @bainternet,Soumettre un message et télécharger l'image depuis frontal

C'est à moi,

<?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>

Pas de solution correcte

Licencié sous: CC-BY-SA avec attribution
Non affilié à wordpress.stackexchange
scroll top