Skip to content

Instantly share code, notes, and snippets.

@kingkool68
Created November 21, 2020 04:19
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kingkool68/a66d2df7835a8869625282faa78b489a to your computer and use it in GitHub Desktop.
Save kingkool68/a66d2df7835a8869625282faa78b489a to your computer and use it in GitHub Desktop.
Two WordPress functions for maybe side loading URLs to the media library. Useful for content migrations that need to be run multiple times without producing duplicate downloads.
<?php
/**
* Example useage:
*
* maybe_sideload_image( 'https://dummyimage.com/600x400/000/fff.png' ); // Downloads image to the media library and returns an attachment ID
* maybe_sideload_image( 'https://dummyimage.com/600x400/000/fff.png' ); // Returns an attachment ID as the image has already been downloaded and added to the media library
*/
/**
* Given an image URL return an attachment_id. Image will be sideloaded into the media library if it doesn't exist.
*
* @param string $url The image URL to maybe sideload
* @uses media_sideload_image
*/
function maybe_sideload_image( $url = '' ) {
global $wpdb;
require_once ABSPATH . 'wp-admin/includes/media.php';
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/image.php';
// Check to see if the URL has already been fetched, if so return the attachment ID
$attachment_id = $wpdb->get_var(
$wpdb->prepare( "SELECT `post_id` FROM {$wpdb->postmeta} WHERE `meta_key` = '_source_url' AND `meta_value` = %s", $url )
);
if ( ! empty( $attachment_id ) ) {
return $attachment_id;
}
$attachment_id = $wpdb->get_var(
$wpdb->prepare( "SELECT `ID` FROM {$wpdb->posts} WHERE guid=%s", $url )
);
if ( ! empty( $attachment_id ) ) {
return $attachment_id;
}
// If the URL doesn't exist, sideload it to the media library
return media_sideload_image( $url, $post_id = 0, $desc = null, $return = 'id' );
}
/**
* Given a file URL return an attachment ID. If the URL doesn't already exist then it gets sideloaded into the media library.
*
* @param string $url URL of the file to maybe sideload
* @uses media_handle_sideload
*/
function maybe_sideload_url( $url = '' ) {
global $wpdb;
require_once ABSPATH . 'wp-admin/includes/media.php';
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/image.php';
// Check to see if the URL has already been fetched, if so return the attachment ID
$attachment_id = $wpdb->get_var(
$wpdb->prepare( "SELECT `post_id` FROM {$wpdb->postmeta} WHERE `meta_key` = '_source_url' AND `meta_value` = %s", $url )
);
if ( ! empty( $attachment_id ) ) {
return $attachment_id;
}
$attachment_id = $wpdb->get_var(
$wpdb->prepare( "SELECT `ID` FROM {$wpdb->posts} WHERE guid=%s", $url )
);
if ( ! empty( $attachment_id ) ) {
return $attachment_id;
}
// If the URL doesn't exist, sideload it to the media library
$file_array = array();
$file_array['name'] = wp_basename( $url );
// Download file to temp location.
$file_array['tmp_name'] = download_url( $url );
// If error storing temporarily, return
if ( is_wp_error( $file_array['tmp_name'] ) ) {
return 0;
}
// Do the validation and storage stuff.
$id = media_handle_sideload( $file_array, $post_id, $desc );
// If error storing permanently, unlink.
if ( is_wp_error( $id ) ) {
@unlink( $file_array['tmp_name'] );
return $id;
}
// Store the original attachment source in meta. Mirrors media_sideload_image
add_post_meta( $id, '_source_url', $url );
return $id;
}
@electricbrick
Copy link

Line 7 seems like it should read:

  • maybe_sideload_url(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment