Skip to content

Instantly share code, notes, and snippets.

@itumulak
Last active September 24, 2018 16:41
Show Gist options
  • Save itumulak/a417073940b02422f92da5f474d32ccf to your computer and use it in GitHub Desktop.
Save itumulak/a417073940b02422f92da5f474d32ccf to your computer and use it in GitHub Desktop.
Wordpress Image Handling
<?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