Category |
Company |
Email |
Destination |
Actions |
function uplaod_handle_image_upload($file)
{
if (isset($file['name'])) {
// Check file type
$file_info = wp_check_filetype($file['name']);
if ($file_info['ext'] && !in_array($file_info['ext'], array('jpg', 'jpeg', 'png', 'gif'))) {
// Handle invalid file type
return false;
}
// Check for a successful upload
if ($file['error'] !== UPLOAD_ERR_OK) {
return false;
}
// Handle the upload using move_uploaded_file
$file_name = basename($file['name']);
$upload_dir = wp_upload_dir();
$file_path = $upload_dir['path'] . $file_name; // Replace '/path/to/upload/directory/' with your desired upload directory
if (!move_uploaded_file($file['tmp_name'], $file_path)) {
// Handle the case where move_uploaded_file returns false
return false;
}
// Create attachment post
$attachment = array(
'post_mime_type' => $file_info['type'],
'post_title' => $file_name,
'post_content' => '',
'post_status' => 'inherit'
);
$attachment_id = wp_insert_attachment($attachment, $file_path);
if (is_wp_error($attachment_id)) {
// Handle the case where wp_insert_attachment returns an error
return false;
}
if (!function_exists('custom_generate_attachment_metadata')) {
function custom_generate_attachment_metadata($attachment_id, $file_path)
{
//echo "string" . $metadata;
// Check if file exists
if (!file_exists($file_path)) {
return new WP_Error('file_not_found', 'File not found.');
}
// Get file information
$file_info = wp_check_filetype($file_path);
$mime_type = $file_info['type'];
// Generate attachment metadata
$metadata = array(
'width' => 0,
'height' => 0,
'file' => basename($file_path),
'sizes' => array(),
'image_meta' => array()
);
// Set image dimensions if the file is an image
if (substr($mime_type, 0, 5) === 'image') {
$image_size = getimagesize($file_path);
if ($image_size) {
$metadata['width'] = $image_size[0];
$metadata['height'] = $image_size[1];
}
}
return $metadata;
}
}
// Generate attachment metadata and update attachment post
$attachment_data = custom_generate_attachment_metadata($attachment_id, $file_path);
if (is_wp_error($attachment_data)) {
// Error occurred, handle it
echo 'Error generating attachment metadata: ' . $attachment_data->get_error_message();
} else {
// Metadata generated successfully, update attachment
if (wp_update_attachment_metadata($attachment_id, $attachment_data)) {
//echo 'Attachment metadata updated successfully.';
} else {
echo 'Error updating attachment metadata.';
}
}
return $attachment_id;
}
}
add_shortcode('company_user', 'companyProfileShortcode');
Citadel | ittn.ie