• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

PHP media_handle_sideload函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了PHP中media_handle_sideload函数的典型用法代码示例。如果您正苦于以下问题:PHP media_handle_sideload函数的具体用法?PHP media_handle_sideload怎么用?PHP media_handle_sideload使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了media_handle_sideload函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的PHP代码示例。

示例1: media_sideload_image_1

function media_sideload_image_1($file, $post_id, $desc = null, $return = 'html')
{
    if (!empty($file)) {
        // Set variables for storage, fix file filename for query strings.
        preg_match('/[^\\?]+\\.(jpe?g|jpe|gif|png)\\b/i', $file, $matches);
        $file_array = array();
        $file_array['name'] = basename($matches[0]);
        // Download file to temp location.
        $file_array['tmp_name'] = download_url($file);
        // If error storing temporarily, return the error.
        if (is_wp_error($file_array['tmp_name'])) {
            return $file_array['tmp_name'];
        }
        // 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;
        }
        /*$fullsize_path = get_attached_file( $id ); // Full path
          if (function_exists('ewww_image_optimizer')) {
            ewww_image_optimizer($fullsize_path, $gallery_type = 4, $converted = false, $new = true, $fullsize = true);
          }*/
        $src = wp_get_attachment_url($id);
    }
    if (!empty($src)) {
        //update_post_meta($post_id, 'image_value', $src);
        set_post_thumbnail($post_id, $id);
        //update_post_meta($post_id, 'Thumbnail', $src);
        return get_post_meta($post_id, 'image_value', true);
    } else {
        return new WP_Error('image_sideload_failed');
    }
}
开发者ID:Gestiopolis,项目名称:GestiopolisExp1,代码行数:35,代码来源:fiximages.php


示例2: fabim_upload_action

function fabim_upload_action()
{
    $image = $_POST['image'];
    //Get the base-64 string from data
    $filteredData = substr($_POST['image'], strpos($_POST['image'], ",") + 1);
    //Decode the string
    $unencodedData = base64_decode($filteredData);
    $originalName = basename(parse_url('image_marker.png', PHP_URL_PATH));
    $tempName = tempnam('/tmp', 'php_files');
    $tempName = realpath($tempName);
    file_put_contents($tempName, $unencodedData);
    $data = array();
    $data['name'] = $originalName;
    $data['type'] = 'image/png';
    $data['tmp_name'] = $tempName;
    $data['error'] = 0;
    $data['size'] = strlen($unencodedData);
    $_FILES['image'] = array('name' => $originalName, 'type' => 'image/jpeg', 'tmp_name' => $tempName, 'error' => 0, 'size' => strlen($unencodedData));
    $movefile = media_handle_sideload($_FILES['image'], 0);
    if ($movefile && !isset($movefile['error'])) {
        die('Done');
    } else {
        _e('Failed to recreating image, please try again', 'fabric-marker');
    }
}
开发者ID:inefit,项目名称:WP-image-marker,代码行数:25,代码来源:image-marker.php


示例3: image

 public static function image($post_id)
 {
     $images = array();
     $xml = simplexml_load_file('https://unsplash.com/rss');
     foreach ($xml->channel->item as $item) {
         foreach ($item->image->url as $url) {
             $images[] = (string) $url;
         }
     }
     shuffle($images);
     $url = $images[0];
     $tmp = download_url($url);
     $file_array = array('name' => 'new test image', 'tmp_name' => $tmp);
     // Check for download errors
     if (is_wp_error($tmp)) {
         @unlink($file_array['tmp_name']);
     }
     $image_id = media_handle_sideload($file_array, $post_id);
     // Check for handle sideload errors.
     if (is_wp_error($image_id)) {
         @unlink($file_array['tmp_name']);
     }
     if (is_wp_error($image_id)) {
         return $image_id;
     }
 }
开发者ID:TheTypoMaster,项目名称:evans,代码行数:26,代码来源:class-test-content.php


示例4: wp_rp_upload_attachment

/**
 * Cron - Thumbnail extraction
 */
function wp_rp_upload_attachment($url, $post_id)
{
    /* Parts copied from wp-admin/includes/media.php:media_sideload_image */
    include_once ABSPATH . 'wp-admin/includes/file.php';
    include_once ABSPATH . 'wp-admin/includes/media.php';
    include_once ABSPATH . 'wp-admin/includes/image.php';
    $tmp = download_url($url);
    preg_match('/[^\\?]+\\.(jpe?g|jpe|gif|png)\\b/i', $url, $matches);
    $file_array['name'] = sanitize_file_name(urldecode(basename($matches[0])));
    $file_array['tmp_name'] = $tmp;
    if (is_wp_error($tmp)) {
        @unlink($file_array['tmp_name']);
        return false;
    }
    $post_data = array('guid' => $url, 'post_title' => 'rp_' . $file_array['name']);
    $attachment_id = media_handle_sideload($file_array, $post_id, null, $post_data);
    if (is_wp_error($attachment_id)) {
        @unlink($file_array['tmp_name']);
        return false;
    }
    $attach_data = wp_get_attachment_metadata($attachment_id);
    $platform_options = wp_rp_get_platform_options();
    $min_width = $platform_options['custom_size_thumbnail_enabled'] ? WP_RP_CUSTOM_THUMBNAILS_WIDTH : WP_RP_THUMBNAILS_WIDTH;
    $min_height = $platform_options['custom_size_thumbnail_enabled'] ? WP_RP_CUSTOM_THUMBNAILS_HEIGHT : WP_RP_THUMBNAILS_HEIGHT;
    if (!$attach_data || $attach_data['width'] < $min_width || $attach_data['height'] < $min_height) {
        wp_delete_attachment($attachment_id);
        return false;
    }
    return $attachment_id;
}
开发者ID:johnreytepacia,项目名称:etarticles,代码行数:33,代码来源:thumbnailer.php


示例5: largo_media_sideload_image

/**
 * Similar to `media_sideload_image` except that it simply returns the attachment's ID on success
 *
 * @param (string) $file the url of the image to download and attach to the post
 * @param (integer) $post_id the post ID to attach the image to
 * @param (string) $desc an optional description for the image
 *
 * @since 0.5.2
 */
function largo_media_sideload_image($file, $post_id, $desc = null)
{
    if (!empty($file)) {
        include_once ABSPATH . 'wp-admin/includes/image.php';
        include_once ABSPATH . 'wp-admin/includes/file.php';
        include_once ABSPATH . 'wp-admin/includes/media.php';
        // Set variables for storage, fix file filename for query strings.
        preg_match('/[^\\?]+\\.(jpe?g|jpe|gif|png)\\b/i', $file, $matches);
        $file_array = array();
        $file_array['name'] = basename($matches[0]);
        // Download file to temp location.
        $file_array['tmp_name'] = download_url($file);
        // If error storing temporarily, return the error.
        if (is_wp_error($file_array['tmp_name'])) {
            return $file_array['tmp_name'];
        }
        // 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;
    }
}
开发者ID:DrewAPicture,项目名称:Largo,代码行数:34,代码来源:images.php


示例6: setFeaturedImage

function setFeaturedImage($post_id, $url, $featuredImageTitle)
{
    // Download file to temp location and setup a fake $_FILE handler
    // with a new name based on the post_id
    $tmp_name = download_url($url);
    //								echo $tmp_name;
    $file_array['name'] = $post_id . '-thumb.jpg';
    // new filename based on slug
    $file_array['tmp_name'] = $tmp_name;
    // If error storing temporarily, unlink
    if (is_wp_error($tmp_name)) {
        @unlink($file_array['tmp_name']);
        $file_array['tmp_name'] = '';
    }
    // do validation and storage .  Make a description based on the Post_ID
    $attachment_id = media_handle_sideload($file_array, $post_id, 'Thumbnail for ' . $post_id);
    // If error storing permanently, unlink
    if (is_wp_error($attachment_id)) {
        $error_string = $attachment_id->get_error_message();
        @unlink($file_array['tmp_name']);
        return;
    }
    // Set as the post attachment
    $post_result = add_post_meta($post_id, '_thumbnail_id', $attachment_id, true);
    //					echo $post_result);
}
开发者ID:adams0917,项目名称:woocommerce_eht,代码行数:26,代码来源:import_posts.php


示例7: create_file

 private function create_file($name, $vararray)
 {
     $string = $this->generate_csv_string($vararray);
     $upload_dir = wp_upload_dir();
     $file = $upload_dir['path'] . "/" . $name . ".csv";
     file_put_contents($file, $string);
     $url = $upload_dir['url'] . "/" . $name . ".csv";
     $tmp = download_url($url);
     $post_id = $this->options[$this->page_id_field];
     $file_array = array();
     // Set variables for storage
     // fix file filename for query strings
     preg_match('/[^\\?]+\\.(jpg|jpe|jpeg|gif|png|csv)/i', $url, $matches);
     $file_array['name'] = basename($matches[0]);
     $file_array['tmp_name'] = $tmp;
     // If error storing temporarily, unlink
     if (is_wp_error($tmp)) {
         print_r($tmp);
         @unlink($file_array['tmp_name']);
         $file_array['tmp_name'] = '';
     }
     // 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)) {
         print_r($id);
         @unlink($file_array['tmp_name']);
         return $id;
     }
     $src = wp_get_attachment_url($id);
     echo $src;
 }
开发者ID:shawfactor,项目名称:lh-backup,代码行数:32,代码来源:lh-backup.php


示例8: load_thumbnail

 public function load_thumbnail($post_id, $url, $desc = null)
 {
     if (!empty($url)) {
         // Download file to temp location
         $tmp = download_url($url);
         // Set variables for storage
         // fix file filename for query strings
         preg_match('/[^\\?]+\\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $url, $matches);
         if (isset($matches[0])) {
             $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, $post_id, $desc);
         if (!is_wp_error($id)) {
             return $id;
         }
         // If error storing permanently, unlink
         @unlink($file_array['tmp_name']);
     }
     return false;
 }
开发者ID:markoheijnen,项目名称:life-control,代码行数:27,代码来源:updater.php


示例9: soundpress_add_track_details_to_post

/**
 * Add track details when a post is saved.
 *
 * @param int 	$post_id 		The post ID.
 * @param post 	$post 			The post object.
 * @param bool 	$update 		Whether this is an existing post being updated or not.
 */
function soundpress_add_track_details_to_post($post_id, $post, $update)
{
    $trackurl = get_post_meta($post_id, 'soundpress_soundcloud_url', true);
    if (wp_is_post_revision($post_id)) {
        return;
    }
    if ($trackurl) {
        $track_details = soundcloud_remote_get($trackurl);
        if (is_object($track_details)) {
            // Check for thumbnail. If not present, get the board Image
            if (!has_post_thumbnail($post_id)) {
                $thumbnailurl = $track_details->artwork_url;
                if (empty($thumbnailurl)) {
                    $thumbnailurl = $track_details->user->avatar_url;
                }
                $tmp = download_url($thumbnailurl);
                if (is_wp_error($tmp)) {
                }
                $desc = get_the_title($post_id);
                $file_array = array();
                // Set variables for storage
                // fix file filename for query strings
                preg_match('/[^\\?]+\\.(jpg|jpe|jpeg|gif|png)/i', $thumbnailurl, $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, $post_id, $desc);
                // If error storing permanently, unlink
                if (is_wp_error($id)) {
                    @unlink($file_array['tmp_name']);
                    return $id;
                }
                set_post_thumbnail($post_id, $id);
            }
            // Check for Description
            if ("" == get_the_content()) {
                $postcontentarray = array('ID' => $post_id, 'post_content' => $track_details->description);
                // Update the post into the database
                wp_update_post($postcontentarray);
            }
            // Get The Duration
            $durationseconds = $track_details->duration / 1000;
            update_post_meta($post_id, 'podcast_duration', esc_attr($durationseconds));
            if (TRUE == $track_details->downloadable) {
                $download_url = esc_attr($track_details->download_url);
                update_post_meta($post_id, 'podcast_download_url', $download_url);
            }
        }
    }
}
开发者ID:nervewax,项目名称:podcast-system,代码行数:62,代码来源:api-connection.php


示例10: insert_attachment

function insert_attachment($image, $post_id, $setthumb = 'false')
{
    require_once ABSPATH . "wp-admin" . '/includes/image.php';
    require_once ABSPATH . "wp-admin" . '/includes/file.php';
    require_once ABSPATH . "wp-admin" . '/includes/media.php';
    // use image exif/iptc data for title and caption defaults if possible
    $array = array('name' => basename($image), 'type' => 'image/jpeg', 'tmp_name' => $image, 'error' => 0, 'size' => filesize($image));
    $imageId = media_handle_sideload($array, $post_id);
    if ($setthumb) {
        update_post_meta($post_id, '_thumbnail_id', $imageId);
    }
    return $imageId;
}
开发者ID:slavic18,项目名称:cats,代码行数:13,代码来源:files.php


示例11: after_save

 public function after_save()
 {
     if (is_array($this->file_upload)) {
         //do the upload
         require_once ABSPATH . 'wp-admin/includes/image.php';
         require_once ABSPATH . 'wp-admin/includes/file.php';
         require_once ABSPATH . 'wp-admin/includes/media.php';
         $media_id = media_handle_sideload($this->file_upload['file'], $this->id, $this->content, array('post_status' => 'inherit'));
         if (is_wp_error($media_id)) {
             //todo log
             //return $media_id;
         } else {
             //all good, store the value now
             update_post_meta($this->id, '_file', $media_id);
         }
     }
 }
开发者ID:Artgorae,项目名称:wp-artgorae,代码行数:17,代码来源:ig-uploader-model.php


示例12: loadImg

function loadImg($url)
{
    // во фронтэнде нужны эти файлы
    require_once ABSPATH . "wp-admin" . '/includes/image.php';
    require_once ABSPATH . "wp-admin" . '/includes/file.php';
    require_once ABSPATH . "wp-admin" . '/includes/media.php';
    $file_array = array();
    $tmp = download_url($url);
    // корректируем умя файла в строках запроса.
    $nameR = end(explode('.', end(explode('/', $url))));
    $file_array['name'] = rand(100000, 999999) . '.' . $nameR;
    $file_array['tmp_name'] = $tmp;
    $id = media_handle_sideload($file_array, 0);
    // удалим временный файл
    @unlink($file_array['tmp_name']);
    return $id;
}
开发者ID:alex-bro,项目名称:test_dnepr,代码行数:17,代码来源:albr_set_img.php


示例13: sideload_icon

 /**
  * Get a fresh icon image from grabicon.com and cache it.
  */
 public function sideload_icon()
 {
     require_once ABSPATH . 'wp-admin/includes/file.php';
     require_once ABSPATH . 'wp-admin/includes/media.php';
     require_once ABSPATH . 'wp-admin/includes/image.php';
     $grab_base_url = 'http://grabicon.com/icon';
     $home_url_parts = parse_url(home_url());
     $grab_url = add_query_arg(array('size' => $this->size, 'domain' => $home_url_parts['host'], 'origin' => $home_url_parts['host'], 'reset' => 'true', 'key' => self::$api_key), $grab_base_url);
     $file_info = array('name' => 'prompt-site-icon-' . $this->size . '.png', 'tmp_name' => download_url($grab_url, 5));
     if (is_wp_error($file_info['tmp_name'])) {
         return;
     }
     $id = media_handle_sideload($file_info, 0);
     if (!is_wp_error($id)) {
         $this->attachment_id = $id;
     }
 }
开发者ID:postmatic,项目名称:beta-dist,代码行数:20,代码来源:grab-icon.php


示例14: wprss_media_sideload_image

/**
 * An enhanced version of WP's media_sideload_image function.
 *
 * If media_sideload_image fails, the file is downloaded manually
 * as an image, inserted as an attachment, and attached to the post.
 * 
 * @since 3.5.1
 */
function wprss_media_sideload_image($file, $post_id, $desc = null)
{
    try {
        if (!empty($file)) {
            // Download file to temp location
            $tmp = download_url($file);
            // Set variables for storage
            // fix file filename for query strings
            preg_match('/[^\\?]+\\.(jpe?g|jpe|gif|png)\\b/i', $file, $matches);
            if (count($matches) > 0) {
                $file_array['name'] = basename($matches[0]);
            } else {
                preg_match('/[\\/\\?\\=\\&]([^\\/\\?\\=\\&]*)[\\?]*$/i', $file, $matches2);
                if (count($matches2) > 1) {
                    $file_array['name'] = $matches2[1] . '.png';
                } else {
                    @unlink($tmp);
                    return "<img src='{$file}' alt='' />";
                }
            }
            $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, $post_id, $desc);
            // If error storing permanently, unlink
            if (is_wp_error($id)) {
                @unlink($file_array['tmp_name']);
                return "<img src='{$file}' alt='' />";
            }
            $src = wp_get_attachment_url($id);
        }
        // Finally check to make sure the file has been saved, then return the html
        if (!empty($src)) {
            $alt = isset($desc) ? esc_attr($desc) : '';
            $html = "<img src='{$src}' alt='{$alt}' />";
            return $html;
        }
    } catch (Exception $e) {
        return "<img src='{$file}' alt='' />";
    }
}
开发者ID:manishkhanchandani,项目名称:mkgxy,代码行数:53,代码来源:misc-functions.php


示例15: upload

 public function upload($url)
 {
     $tmp = download_url($url);
     $file_array = ['name' => basename($url), 'tmp_name' => $tmp];
     // Check for download errors
     if (is_wp_error($tmp)) {
         @unlink($file_array['tmp_name']);
         return false;
     }
     $id = media_handle_sideload($file_array, 0);
     // Check for handle sideload errors.
     if (is_wp_error($id)) {
         @unlink($file_array['tmp_name']);
         return false;
     }
     $attachment_url = wp_get_attachment_url($id);
     return $attachment_url;
     // Do whatever you have to here
 }
开发者ID:alhenaconseil,项目名称:wordpress-sociallymap,代码行数:19,代码来源:VideoDownloader.php


示例16: flickr_image_attach

function flickr_image_attach($flickrurl, $post_id)
{
    preg_match('/http\\:\\/\\/www\\.flickr\\.com\\/photos\\/(.*?)\\/([0-9]+)\\//si', $flickrurl, $m);
    if ($m) {
        $flickruser = $m[1];
        $photo_id = $m[2];
    } else {
        return;
    }
    //$flickruser = getFlickrUser($photo_id);
    $file = getFlickrURL($photo_id);
    if (!empty($file)) {
        // Download file to temp location
        $tmp = download_url($file);
        // Set variables for storage
        // fix file filename for query strings
        preg_match('/[^\\?]+\\.(jpe?g|jpe|gif|png)\\b/i', $file, $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, $post_id, $desc = null);
        // If error storing permanently, unlink
        if (is_wp_error($id)) {
            @unlink($file_array['tmp_name']);
            return $id;
        }
        $src = wp_get_attachment_url($id);
    }
    if (!empty($src)) {
        update_post_meta($post_id, 'image_url_value', $flickrurl);
        update_post_meta($post_id, 'image_author_t_value', $flickruser);
        update_post_meta($post_id, 'image_value', $src);
        set_post_thumbnail($post_id, $id);
        return update_post_meta($post_id, 'Thumbnail', $src);
    } else {
        return false;
    }
}
开发者ID:Gestiopolis,项目名称:GestiopolisExp1,代码行数:43,代码来源:frontendeditget.php


示例17: attachFileToPost

 public function attachFileToPost($file, $post_id, $desc = null)
 {
     if (empty($file)) {
         return null;
     }
     // Set variables for storage
     // fix file filename for query strings
     $file_array['name'] = basename($file);
     $file_array['tmp_name'] = $file;
     // do the validation and storage stuff
     $id = media_handle_sideload($file_array, $post_id, $desc);
     // If error storing permanently, show
     if (is_wp_error($id)) {
         echo '<hr /><hr /><hr /><hr /><pre>';
         var_dump($id);
         exit;
     }
     return $id;
 }
开发者ID:migumuno,项目名称:nordicainteriores,代码行数:19,代码来源:class.ffAttachmentUpdater.php


示例18: sideload_attachment

 private function sideload_attachment($attachment, $_to_post_id, $date)
 {
     if ('image' === $attachment->type) {
         $response = wp_remote_head($attachment->url);
         if (200 == wp_remote_retrieve_response_code($response)) {
             $_mimes = array_flip(wp_get_mime_types());
             $_content_type = wp_remote_retrieve_header($response, 'content-type');
             if (isset($_mimes[$_content_type])) {
                 $_ext = strtok($_mimes[$_content_type], '|');
                 $_temp_file = download_url($attachment->url);
                 // TODO check for WP_Error
                 $_new_file = str_replace('.tmp', '.' . $_ext, $_temp_file);
                 rename($_temp_file, $_new_file);
                 $file_array = array();
                 $file_array['name'] = basename($_new_file);
                 $file_array['tmp_name'] = $_new_file;
                 $attachment_id = media_handle_sideload($file_array, $_to_post_id, '', array('post_date' => $date, 'post_date_gmt' => $date));
             }
         }
     }
 }
开发者ID:westi,项目名称:pjw-groupme-importer,代码行数:21,代码来源:class-pjw-groupme-wp-api.php


示例19: wpcom_vip_download_image

/**
 * Downloads an external image and optionally attaches it to a post.
 *
 * Contains most of core's media_sideload_image() but returns an attachment ID instead of HTML.
 *
 * Note: this function does not validate the domain that the image is coming from. Please make sure
 *   to validate this before downloading the image. Should only pull down images from trusted sources.
 *
 * Note: This function does not support GET params because these will not work on WPCOM production servers see r157060
 *
 * @param string $image_url URL of the image.
 * @param int $post_ID ID of the post it should be attached to.
 * @return $thumbnail_id id of the thumbnail attachment post id
 */
function wpcom_vip_download_image($image_url, $post_id = 0, $description = '')
{
    if (isset($_SERVER['REQUEST_METHOD']) && strtoupper($_SERVER['REQUEST_METHOD']) == 'GET') {
        return new WP_Error('invalid-request-method', 'Media sideloading is not supported via GET. Use POST.');
    }
    if (!is_admin()) {
        return new WP_Error('not-in-admin', 'Media sideloading can only be done in when `true === is_admin()`.');
    }
    if ($post_id < 0) {
        return new WP_Error('invalid-post-id', 'Please specify a valid post ID.');
    }
    if (!filter_var($image_url, FILTER_VALIDATE_URL)) {
        return new WP_Error('not-a-url', 'Please specify a valid URL.');
    }
    $image_url_path = parse_url($image_url, PHP_URL_PATH);
    $image_path_info = pathinfo($image_url_path);
    if (!in_array(strtolower($image_path_info['extension']), array('jpg', 'jpe', 'jpeg', 'gif', 'png'))) {
        return new WP_Error('not-an-image', 'Specified URL does not have a valid image extension.');
    }
    // Download file to temp location; short timeout, because we don't have all day.
    $downloaded_url = download_url($image_url, 30);
    // We couldn't download and store to a temporary location, so bail.
    if (is_wp_error($downloaded_url)) {
        return $downloaded_url;
    }
    $file_array['name'] = $image_path_info['basename'];
    $file_array['tmp_name'] = $downloaded_url;
    if (empty($description)) {
        $description = $image_path_info['filename'];
    }
    // Now, let's sideload it.
    $attachment_id = media_handle_sideload($file_array, $post_id, $description);
    // If error storing permanently, unlink and return the error
    if (is_wp_error($attachment_id)) {
        @unlink($file_array['tmp_name']);
        // unlink can throw errors if the file isn't there
        return $attachment_id;
    }
    return $attachment_id;
}
开发者ID:gopinathshiva,项目名称:wordpress-vip-plugins,代码行数:54,代码来源:vip-media.php


示例20: kt_download_media

/**
 * Download an image from the specified URL and attach it to a post.
 *
 * @since 1.0
 *
 * @param string $post The serialize of post
 */
function kt_download_media($id, $post_title, $guid)
{
    global $posts_id;
    $new_id = kt_get_post_id($id, 'attachment', 'kt_demo_attachment');
    if (!$new_id) {
        require_once ABSPATH . 'wp-admin/includes/media.php';
        require_once ABSPATH . 'wp-admin/includes/file.php';
        require_once ABSPATH . 'wp-admin/includes/image.php';
        require_once ABSPATH . 'wp-includes/pluggable.php';
        // Set variables for storage, fix file filename for query strings.
        preg_match('/[^\\?]+\\.(jpe?g|jpe|gif|png)\\b/i', $guid, $matches);
        $file_array = array();
        $file_array['name'] = basename($matches[0]);
        // Download file to temp location.
        $file_array['tmp_name'] = download_url($guid);
        // If error storing temporarily, return the error.
        if (is_wp_error($file_array['tmp_name'])) {
            @unlink($file_array['tmp_name']);
            echo 'is_wp_error $file_array: ' . $guid;
            print_r($file_array['tmp_name']);
            return $file_array['tmp_name'];
        }
        // Do the validation and storage stuff.
        $new_id = media_handle_sideload($file_array, '', $post_title);
        //$id of attachement or wp_error
        // If error storing permanently, unlink.
        if (is_wp_error($new_id)) {
            @unlink($file_array['tmp_name']);
            echo 'is_wp_error $id: ' . $new_id->get_error_messages() . ' ' . $guid;
            return $new_id;
        }
        if (isset($posts_id['attachment']['kt_demo_attachment'])) {
            $posts_id['attachment']['kt_demo_attachment'][$id] = $new_id;
        }
        update_post_meta($new_id, 'kt_demo_attachment', $id);
        return $new_id;
    }
    return $new_id;
}
开发者ID:acamboy,项目名称:kutetheme-wp,代码行数:46,代码来源:utility.php



注:本文中的media_handle_sideload函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
PHP media_isexternal函数代码示例发布时间:2022-05-15
下一篇:
PHP mediaFN函数代码示例发布时间:2022-05-15
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap