Pregunta

Encontré este script Wonderdul que @Bainternet aquí en WordPress Respuestas creadas, que hace posible adjuntar imágenes en publicaciones de un formulario HTML.

Traté de combinarme en mi script "Form de HTML para publicar". Creo una publicación como borrador en el backend ... pero la imagen no aparece, y no se muestra en "Media" ..

Este es @Bainternet Script/Respuesta,Envíe la imagen y la imagen de carga desde el front-end

Esto es mío,

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

No hay solución correcta

Licenciado bajo: CC-BY-SA con atribución
scroll top