Last active
May 22, 2018 09:44
-
-
Save shramee/eb679ac8f6419465959610f5395363f9 to your computer and use it in GitHub Desktop.
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 | |
/** | |
* 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