Skip to content

Instantly share code, notes, and snippets.

@onigetoc
Last active July 20, 2016 17:12
Show Gist options
  • Save onigetoc/7a1d4cdf2565504468d44919057f4eb7 to your computer and use it in GitHub Desktop.
Save onigetoc/7a1d4cdf2565504468d44919057f4eb7 to your computer and use it in GitHub Desktop.
<div class="wpuf-attachment-upload-filelist" data-type="file" data-required="yes">
<a id="wpuf-featured_image-pickfiles" data-form_id="61" class="button file-selector wpuf_featured_image_61" href="#" style="position: relative; z-index: 1; display: none;">Select Image</a>
<ul class="wpuf-attachment-list thumbnails">
<li class="wpuf-image-wrap thumbnail">
<div class="attachment-name"><img src="http://example.com/wp-content/uploads/2016/07/1400x1400_10808553-400x4001-1-150x150.jpg" alt="1400x1400_10808553-400x400[1]"></div>
<input type="hidden" name="wpuf_files[featured_image][]" value="100">
<div class="caption"><a href="#" class="btn btn-danger btn-small attachment-delete" data-attach_id="100">Delete</a></div>
</li>
</ul>
</div>
<?php
// If you want to use this function outside of the context of /wp-admin/
// (typically if you are writing a more advanced custom importer script) you need to include media.php and depending includes:
require_once(ABSPATH . 'wp-admin/includes/media.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/image.php');
$url = "http://wordpress.org/about/images/logos/wordpress-logo-stacked-rgb.png";
$post_id = 1;
$desc = "The WordPress Logo";
$image = media_sideload_image($url, $post_id, $desc);
/*********** OR GET ATTACHEMENT ID AFTER UPLOAD ***************/
function get_attachment_id_from_src ($image_src) {
global $wpdb;
$query = "SELECT ID FROM {$wpdb->posts} WHERE guid='$image_src'";
$id = $wpdb->get_var($query);
return $id;
}
// Upload an Image
$image = media_sideload_image($downloadable_path, $post_id);
// Remove any unwanted HTML, keep just a plain URL (or whatever is in the image src="..." )
$image = preg_replace("/.*(?<=src=["'])([^"']*)(?=["']).*/", '$1', $image);
// Get the Attachment ID
$attachment_id = $this -> get_attachment_id_from_src ($image);
/***************** SOLUTION 3 ******************/
private function _uploadImageToMediaLibrary($postID, $url, $alt = "blabla") {
require_once("../sites/$this->_wpFolder/wp-load.php");
require_once("../sites/$this->_wpFolder/wp-admin/includes/image.php");
require_once("../sites/$this->_wpFolder/wp-admin/includes/file.php");
require_once("../sites/$this->_wpFolder/wp-admin/includes/media.php");
$tmp = download_url( $url );
$desc = $alt;
$file_array = array();
// Set variables for storage
// fix file filename for query strings
preg_match('/[^\?]+\.(jpg|jpe|jpeg|gif|png)/i', $url, $matches);
$file_array['name'] = basename($matches[0]);
$file_array['tmp_name'] = $tmp;
// If error storing temporarily, unlink
if ( is_wp_error( $tmp ) ) {
@unlink($file_array['tmp_name']);
$file_array['tmp_name'] = '';
}
// do the validation and storage stuff
$id = media_handle_sideload( $file_array, $postID, $desc);
// If error storing permanently, unlink
if ( is_wp_error($id) ) {
@unlink($file_array['tmp_name']);
return $id;
}
return $id;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment