Это нужно настроить -
Это работает с URls изображений, но не уверен, как вы получите $image_data
по почте.
Сначала вам нужно будет декодировать изображение base64.
$newImageFile = base64_decode($getData['photoAdvertisement']);
Затем вам нужно будет найти каталоги для работы:
$upload_dir = wp_upload_dir();
$image_data = file_get_contents($newImageFile);
$filename = basename($newImageFile);
Теперь, когда выЕсли у вас есть имя файла, вы можете проверить, существует ли файл и имя файла в вашей библиотеке
if(wp_mkdir_p($upload_dir['path'])) $file = $upload_dir['path'] . '/' . $filename;
else $file = $upload_dir['basedir'] . '/' . $filename;
file_put_contents($file, $image_data);
$wp_filetype = wp_check_filetype($filename, null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name($filename),
'post_content' => '',
'post_status' => 'inherit'
);
Вам понадобятся функции post.php - так что если у вас его еще нет, включите его.
if ( ! function_exists( 'post_exists' ) ) {
require_once( ABSPATH . 'wp-admin/includes/post.php' );
}
таким образом, вы можете проверить, не использовалось ли то же имя файла в вашей библиотеке, как это
if (post_exists($filename)){
//Do whatever - maybe update the alt or something.
}else {
//The file does not exist, and can be inserted.
$attach_id = wp_insert_attachment( $attachment, $file, $post_id );
// $post_id comes from your earlier insertion
// You will need the functions from image.php as well..
require_once(ABSPATH . 'wp-admin/includes/image.php');
//Now we will generate the new image metadata
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
$res1= wp_update_attachment_metadata( $attach_id, $attach_data );
$filealt = "You alternative text here - maybe sanitize the filename or make an input for it";
//attach the metadata to the image
update_post_meta($attach_id, '_wp_attachment_image_alt', $filealt); // Add the alt text
//Now you can add the image to your meta fields
add_post_meta($post_id, 'photo', $attach_id);
}
Примечание. Я не проверял это.
Возможно, вам придется немного подправить его - особенно в части file_get_contents($newImageFile);
.
ПРИМЕЧАНИЕ : это нужно сделать после вашего $post_id = wp_insert_post( $my_post);
часть
Удачи с этим!