Skip to content

Instantly share code, notes, and snippets.

@shramee
Last active May 22, 2018 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shramee/eb679ac8f6419465959610f5395363f9 to your computer and use it in GitHub Desktop.
Save shramee/eb679ac8f6419465959610f5395363f9 to your computer and use it in GitHub Desktop.
<?php
/**
* Saves the file from file input and return attachment ID
* @param string $input_name Name of file input
* @param bool $return_id Whether
* @return int|string|WP_Error ID of the attachment or a WP_Error object on failure.
*/
function shramee_upload_media( $input_name, $return_id = false ) {
if ( ! function_exists( 'media_handle_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_upload( $input_name, 0 );
if ( $return_id ) {
return $media_id;
}
return wp_get_attachment_url( $media_id );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment