Ich habe eine Funktion, die das Bild von einer URL speichert. Das einzige Problem ist, dass alles gut zu funktionieren scheint, außer dass das Bild „nicht gefunden“ werden kann.
Das Bild und der Name befinden sich in der Medienbibliothek, aber das Bild ist fehlerhaft, da beim Öffnen des Bilds über die URL die Meldung „nicht gefunden“ angezeigt wird.
Können Sie mir Bescheid geben, wenn ich etwas verpasst habe?
function set_image_from_url($url) {
$tmp = download_url( $url );
$file_array = array(
'name' => basename( $url ),
'tmp_name' => $tmp
);
/**
* Check for download errors
* if there are error unlink the temp file name
*/
if ( is_wp_error( $tmp ) ) {
@unlink( $file_array[ 'tmp_name' ] );
return $tmp;
}
/**
* now we can actually use media_handle_sideload
* we pass it the file array of the file to handle
* and the post id of the post to attach it to
* $post_id can be set to '0' to not attach it to any particular post
*/
$post_id = '0';
$id = media_handle_sideload( $file_array, $post_id );
/**
* We don't want to pass something to $id
* if there were upload errors.
* So this checks for errors
*/
if ( is_wp_error( $id ) ) {
@unlink( $file_array['tmp_name'] );
return $id;
}
/**
* No we can get the url of the sideloaded file
* $value now contains the file url in WordPress
* $id is the attachment id
*/
$value = wp_get_attachment_url( $id );
// Now you can do something with $value (or $id)
return $id;
}
Wenn Sie wirklich sicher sind, dass der Pfad zum Bild korrekt ist, wenn Sie es mit einer URL anfordern, haben Sie eine
.htaccess
Datei? Wenn ja, posten Sie bitte den Inhalt in der Frage.– Ala Eddine JEBALI
12. Februar 2017 um 17:38
Verwenden Sie die WordPress-Funktion media_sideload_image
– Online-Website
12. Februar 2017 um 18:19 Uhr
@onlinewebsite, was ist der Unterschied zwischen dieser und der media_handle_sideload-Funktion, die ich verwendet habe?
– Kyon147
12. Februar 2017 um 18:32 Uhr
core.trac.wordpress.org/browser/tags/4.7.2/src/wp-admin/…
– Online-Website
12. Februar 2017 um 19:31 Uhr