-
-
Save aibrean/ee4a9e9fef1ff9017b53 to your computer and use it in GitHub Desktop.
Programmatically create the image attachment and return the new media upload id.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Create the image attachment and return the new media upload id. | |
* | |
* @author Joshua David Nelson, [email protected] | |
* | |
* @see http://codex.wordpress.org/Function_Reference/wp_insert_attachment#Example | |
* | |
* @link https://joshuadnelson.com/programmatically-add-images-to-media-library/ | |
* | |
* @param string $image_url The url to the image you're adding to the Media library. | |
* @param int $parent_post_id Optional. Use to attach the media file to a specific post. | |
*/ | |
function jdn_create_image_id( $image_url, $parent_post_id = null ) { | |
// Bail if the image url isn't valid | |
if( empty( $image_url ) || ! esc_url( $image_url ) ) | |
return false; | |
// Escape the url, just to be save | |
$image_url = esc_url( $image_url ); | |
// Cache info on the wp uploads dir | |
$wp_upload_dir = wp_upload_dir(); | |
// get the file path | |
$path = parse_url( $image_url, PHP_URL_PATH ); | |
// File base name, e.g. image.jpg | |
$file_base_name = basename( $image_url ); | |
// Full path, set up to work with a WP in a subdirectory or default location | |
if( site_url() != home_url() ) { | |
$home_path = dirname( dirname( dirname( dirname( dirname( __FILE__ ) ) ) ) ); | |
} else { | |
$home_path = dirname( dirname( dirname( dirname( __FILE__ ) ) ) ); | |
} | |
// Remove the trailing slash on the home path | |
$home_path = untrailingslashit( $home_path ); | |
// Combine the two to get the uploaded file path | |
$uploaded_file_path = $home_path . $path; | |
// Check the type of file. We'll use this as the 'post_mime_type'. | |
$filetype = wp_check_filetype( $file_base_name, null ); | |
// error check | |
if( !empty( $filetype ) && is_array( $filetype ) ) { | |
// Create attachment title - basically, pull out the text | |
$post_title = preg_replace( '/\.[^.]+$/', '', $file_base_name ); | |
// Prepare an array of post data for the attachment. | |
$attachment = array( | |
'guid' => $wp_upload_dir['url'] . '/' . basename( $uploaded_file_path ), | |
'post_mime_type' => $filetype['type'], | |
'post_title' => esc_attr( $post_title ), | |
'post_content' => '', | |
'post_status' => 'inherit' | |
); | |
// Set the post parent id if there is one | |
if( ! is_null( $parent_post_id ) && absint( $parent_post_id ) ) | |
$attachment['post_parent'] = absint( $parent_post_id ); | |
// Insert the attachment. | |
$attach_id = wp_insert_attachment( $attachment, $uploaded_file_path ); | |
//Error check | |
if( !is_wp_error( $attach_id ) ) { | |
//Generate wp attachment meta data | |
if( file_exists( ABSPATH . 'wp-admin/includes/image.php') && file_exists( ABSPATH . 'wp-admin/includes/media.php') ) { | |
require_once( ABSPATH . 'wp-admin/includes/image.php' ); | |
require_once( ABSPATH . 'wp-admin/includes/media.php' ); | |
$attach_data = wp_generate_attachment_metadata( $attach_id, $uploaded_file_path ); | |
wp_update_attachment_metadata( $attach_id, $attach_data ); | |
} // end if file exists check | |
} // end if error check | |
return $attach_id; | |
} else { | |
return false; | |
} // end if $filetype | |
} // end function jdn_create_image_id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment