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; }