videopress_download_poster_image

Similar to `media_sideload_image` -- but returns an ID.

Description

(int|mixed|object|WP_Error) videopress_download_poster_image( $url, $attachment_id ); 

Returns (int|mixed|object|WP_Error)

Parameters (2)

0. $url
The url.
1. $attachment_id
The attachment id.

Usage

  1. if ( !function_exists( 'videopress_download_poster_image' ) ) { 
  2. require_once '/modules/videopress/utility-functions.php'; 
  3.  
  4. // The url. 
  5. $url = null; 
  6.  
  7. // The attachment id. 
  8. $attachment_id = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = videopress_download_poster_image($url, $attachment_id); 
  12.  

Defined (1)

The function is defined in the following location(s).

/modules/videopress/utility-functions.php  
  1. function videopress_download_poster_image( $url, $attachment_id ) { 
  2. // Set variables for storage, fix file filename for query strings. 
  3. preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $url, $matches ); 
  4. if ( ! $matches ) { 
  5. return new WP_Error( 'image_sideload_failed', __( 'Invalid image URL', 'jetpack' ) ); 
  6.  
  7. $file_array = array(); 
  8. $file_array['name'] = basename( $matches[0] ); 
  9. $file_array['tmp_name'] = download_url( $url ); 
  10.  
  11. // If error storing temporarily, return the error. 
  12. if ( is_wp_error( $file_array['tmp_name'] ) ) { 
  13. return $file_array['tmp_name']; 
  14.  
  15. // Do the validation and storage stuff. 
  16. $thumbnail_id = media_handle_sideload( $file_array, $attachment_id, null ); 
  17.  
  18. // Flag it as poster image, so we can exclude it from display. 
  19. update_post_meta( $thumbnail_id, 'videopress_poster_image', 1 ); 
  20.  
  21. return $thumbnail_id;