2017-05-11 3 views
0

Ich versuche, ein Skript zu machen, mit dem ich das vorgestellte Bild eines bestimmten Beitrags festlegen kann. Aus irgendeinem Grund funktioniert es nicht. Kann mir jemand sagen warum?Wordpress - Hochladen der vorgestellten Bild für einen Post programmatisch

$post_id = ... // We get the ID from the form 

if (isset($_FILES['thumbnail'])) { 
    $uploaded_file = $_FILES['userfile']; 
    $filename  = $uploaded_file['name']; 
    $tmp_file  = $uploaded_file['tmp_name']; 
    $upload_dir = wp_upload_dir(); 
    $end_file  = $upload_dir['path']."/$filename"; 

    move_uploaded_file($tmp_file, $end_file); 

    $wp_filetype = wp_check_filetype($filename, null); 
    $attachment = array(
     'post_mime_type' => $wp_filetype['type'], 
     'post_title'  => sanitize_file_name($filename), 
     'post_content' => '', 
     'post_status' => 'inherit' 
    ); 
    $attach_id = wp_insert_attachment($attachment, $end_file, $post_id); 
    set_post_thumbnail($post_id, $attach_id); 
} 
+0

warum Sie ui nicht verwenden für das Hinzufügen von Abschnitt vorgestellten Bild, wenn Sie einen neuen Beitrag erstellen? – Exprator

+0

Ich muss dafür eine Schnittstelle erstellen, ohne Zugriff auf das native WP-Backend. Natürlich würde ich es lieber so machen :) –

Antwort

0

Wie lautet der korrekte Parametername, mit dem die Datei hochgeladen wird?

Sie verwenden verschiedene param name in If-Anweisung und nur nächste Zeile:

if (isset($_FILES['thumbnail'])) { 
    $uploaded_file = $_FILES['userfile']; 

Also nur sicherstellen, dass es die Datei Informationen von der richtigen param s ziehen. Auch sind vorbei Sie eine falsche Dateipfad für folgenden Code:

$wp_filetype = wp_check_filetype($filename, null); 

Es sollte $end_file, statt $filename.

Also der richtige Code könnte so aussehen folgende:

$post_id = ... // We get the ID from the form 

if (isset($_FILES['thumbnail'])) { 
    $uploaded_file = $_FILES['userfile']; 
    $filename  = $uploaded_file['name']; 
    $tmp_file  = $uploaded_file['tmp_name']; 
    $upload_dir = wp_upload_dir(); 
    $end_file  = $upload_dir['path']."/$filename"; 

    move_uploaded_file($tmp_file, $end_file); 

    $wp_filetype = wp_check_filetype($end_file, null); 
    $attachment = array(
     'guid'   => $wp_upload_dir['url'] . '/' . basename($end_file), 
     'post_mime_type' => $wp_filetype['type'], 
     'post_title'  => sanitize_file_name($filename), 
     'post_content' => '', 
     'post_status' => 'inherit' 
    ); 
    $attach_id = wp_insert_attachment($attachment, $end_file, $post_id); 

    // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it. 
    require_once(ABSPATH . 'wp-admin/includes/image.php'); 

    // Generate the metadata for the attachment, and update the database record. 
    $attach_data = wp_generate_attachment_metadata($attach_id, $end_file); 
    wp_update_attachment_metadata($attach_id, $attach_data); 

    set_post_thumbnail($post_id, $attach_id); 
} 
+0

Es stellte sich heraus, dass das das Problem war ... Ich benutzte die falsche Zeichenfolge, um die Datei zu identifizieren ... Entschuldigung. Es ist ziemlich albern Bug ... 8- ( –

+0

@EnriqueMorenoTent keine Sorgen überhaupt. Bug ist nur der Bug, jeder macht den Bug, also keine Sorgen. :-) – Codemole

+0

Danke Kumpel. Erspart mir Stunden;) –

Verwandte Themen