Last active
September 24, 2018 16:41
-
-
Save itumulak/a417073940b02422f92da5f474d32ccf to your computer and use it in GitHub Desktop.
Wordpress Image Handling
This file contains hidden or 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 | |
| /** | |
| * Wordpress Image Handling. | |
| * | |
| * @param array $file $_FILE. File Path Request. | |
| * @return array $status Status | |
| */ | |
| function imageHandling( $file ) | |
| { | |
| $imageArray = wp_handle_upload( $file, ['test_form' => false] ); | |
| if ( $imageArray && ! isset( $imageArray['error'] ) ) | |
| { | |
| $pathParts = pathinfo( $imageArray['file'] ); | |
| $attachmentArgs = [ | |
| 'guid' => $imageArray['url'], | |
| 'post_mime_type' => $imageArray['type'], | |
| 'post_title' => $pathParts['basename'], | |
| 'post_name' => $pathParts['basename'], | |
| 'post_status' => 'inherit' | |
| ]; | |
| $attachmentID = wp_insert_attachment( $attachmentArgs, $imageArray['file'] ); | |
| $attachData = wp_generate_attachment_metadata( $attachmentID, $imageArray['file'] ); | |
| wp_update_attachment_metadata( $attachmentID, $attachData ); | |
| $status = shortcode_atts( ['status' => 'success'], $imageArray ); | |
| $status['attached_id'] = $attachmentID; | |
| $status['attached_data'] = $attachData; | |
| } | |
| else | |
| $status = $imageArray['error']; | |
| return $status; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment