😆[Upload Image Form] How to Integrate Ajax File Upload in WordPress 😒 use change P 1 (ok)
https://artisansweb.net/ajax-file-upload-in-wordpress/
Ví dụ 1: Inserts an attachment to a parent with a post ID of 37:
function media_selector_settings_page_callback()
{
// just for simplicity saving the data right here, but in practice you wouldn't want that.
$id = get_current_user_id();
update_option('media_ids', 11845);
wp_enqueue_media();
// $media_ids_raw = get_option('media_ids');
// $media_ids = explode(',', $media_ids_raw);
$html = '';
$html .= '<div class="form-group form-group-upload" style=" display: flex; align-items: center; ">';
$html .= '<form class="fileUpload" enctype="multipart/form-data">';
// $html .= '<div id="upload_image_button">Upload your ID</div>';
$html .= '<div class="form-group">';
$html .= '<label id="upload_image_button">Upload your ID:</label>';
$html .= '<input type="file" id="filett" accept="image/*">';
$html .= '</div>';
// $html .= '<input type="hidden" name="media_ids" id="image_attachment_id" value="' . $media_ids_raw . '">';
$html .= '</form>';
// if(current_user_can('administrator') && get_current_user_id()) :
// $html .= '<div class="image-preview-wrapper">';
// $html .= '<div id="image-preview">';
// if (count($media_ids) > 0) :
// foreach ($media_ids as $i) :
// $html .= '<img src="' . wp_get_attachment_thumb_url($i) . '">';
// endforeach;
// endif;
// $html .= '</div>';
// $html .= '</div>';
// endif;
$html .= '</div>';
return $html;
}
add_shortcode("upload_your_id", "media_selector_settings_page_callback");
add_action('wp_ajax_file_upload', 'file_upload_callback');
add_action('wp_ajax_nopriv_file_upload', 'file_upload_callback');
function file_upload_callback()
{
check_ajax_referer('file_upload', 'security');
$arr_img_ext = array('image/png', 'image/jpeg', 'image/jpg', 'image/gif');
if (in_array($_FILES['file']['type'], $arr_img_ext)) {
$upload = wp_upload_bits($_FILES["file"]["name"], null, file_get_contents($_FILES["file"]["tmp_name"]));
if ($upload['url']) {
// $filename should be the path to a file in the upload directory.
$url_to_array = parse_url($upload['url']);
$filename = str_replace("/media/upload/", "", $url_to_array['path']);
$wp_upload_dir = wp_upload_dir();
// The ID of the post this attachment is for.
$parent_post_id = 638;
// Check the type of file. We'll use this as the 'post_mime_type'.
$filetype = wp_check_filetype(basename($filename), null);
// Get the path to the upload directory.
// Prepare an array of post data for the attachment.
$attachment = array(
'guid' => basename($filename),
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
'post_content' => '',
'post_status' => 'inherit'
);
// Insert the attachment.
$attach_id = wp_insert_attachment($attachment, $filename, $parent_post_id);
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
require_once(ABSPATH . 'wp-admin/includes/image.php');
// Generate the metadata for the attachment, and update the database record.
$attach_data = wp_generate_attachment_metadata($attach_id, $filename);
wp_update_attachment_metadata($attach_id, $attach_data);
set_post_thumbnail($parent_post_id, $attach_id);
$return = array(
'message' => 'saved'
);
wp_send_json($return);
}
}
wp_die();
}Ví dụ 2:



Create a HTML
Include JS File in WordPress Environment
Write a JavaScript to Upload a File
Upload File on Server
Upload Multiple Files
PreviousHow to Set Featured Image Programmatically post, product in WordPress (ok)Next[Upload Image Form] How to Integrate Ajax File Upload in WordPress 😒 use submit P2 (ok)
Last updated