Skip to content

Instantly share code, notes, and snippets.

@gzalinski
Created September 9, 2021 05:25
Show Gist options
  • Save gzalinski/6cb4ef5c7fec16e8191d83419ffeafc7 to your computer and use it in GitHub Desktop.
Save gzalinski/6cb4ef5c7fec16e8191d83419ffeafc7 to your computer and use it in GitHub Desktop.
WP Upload Image from Front-End
<?php
/**
* Create the image attachment and return the new media upload id.
*
* @param $fileObject
* @return false|int|WP_Error
* @see http://codex.wordpress.org/Function_Reference/wp_insert_attachment#Example
* @since 1.0.0
*/
function xc_create_image_id($fileObject)
{
// Cache info on the wp uploads dir
$wp_upload_dir = wp_upload_dir();
// $wp_upload_dir['path'] is the full server path to wp-content/uploads/2017/05, for multisite works good as well
// $wp_upload_dir['url'] the absolute URL to the same folder, actually we do not need it, just to show the link to file
$i = 1; // number of tries when the file with the same name is already exists
$new_file_path = $wp_upload_dir['path'] . '/' . $fileObject['name'];
$new_file_mime = mime_content_type($fileObject['tmp_name']);
if (empty($fileObject))
die('File is not selected.');
if ($fileObject['error'])
die($fileObject['error']);
if ($fileObject['size'] > wp_max_upload_size())
die('It is too large than expected.');
if (!in_array($new_file_mime, get_allowed_mime_types()))
die('WordPress doesn\'t allow this type of uploads.');
while (file_exists($new_file_path)) {
$i++;
$new_file_path = $wp_upload_dir['path'] . '/' . $i . '_' . $fileObject['name'];
}
$upload_id = false;
if (move_uploaded_file($fileObject['tmp_name'], $new_file_path)) {
$upload_id = wp_insert_attachment(array(
'guid' => $new_file_path,
'post_mime_type' => $new_file_mime,
'post_title' => preg_replace('/\.[^.]+$/', '', $fileObject['name']),
'post_content' => '',
'post_status' => 'inherit'
), $new_file_path);
// wp_generate_attachment_metadata() won't work if you do not include this file
require_once(ABSPATH . 'wp-admin/includes/image.php');
// Generate and save the attachment metas into the database
wp_update_attachment_metadata($upload_id, wp_generate_attachment_metadata($upload_id, $new_file_path));
}
//Error check
if (!is_wp_error($upload_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($upload_id, $new_file_path);
wp_update_attachment_metadata($upload_id, $attach_data);
} // end if file exists check
} // end if error check
return $upload_id;
}
/**
* CALL ON AJAX ACTION
$avatarURL = false;
if ( ! empty( $_FILES["avatar"]["name"] ) ) {
$attach_id = xc_create_image_id( $_FILES["avatar"] );
update_field( 'avatar', $attach_id, $post_id );
$avatarURL = wp_get_attachment_url( $attach_id );
}
**/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment