我在前端有一个表单,我想从前端创建帖子,一切正常,但当我上传特色图片时,会上传破碎的特色图片。我看到上传文件夹中有图像,但我不明白为什么上传时图像会断开,如果有任何帮助,将不胜感激。谢谢
这是我的密码。
<?php
add_action( \'wp_ajax_bpem_event_form_response\', \'bpem_event_form_response\' );
add_action( \'wp_ajax_nopriv_bpem_event_form_response\', \'bpem_event_form_response\' );
function bpem_event_form_response() {
$title = sanitize_text_field($_POST[\'ev_title\']);
$content = $_POST[\'ev_desc\'];
$image_url = sanitize_text_field($_POST[\'ev_image\']);
$location = sanitize_text_field($_POST[\'ev_location\']);
$start_date = sanitize_text_field($_POST[\'ev_start_date\']);
$start_time = sanitize_text_field($_POST[\'ev_start_time\']);
$end_date = sanitize_text_field($_POST[\'ev_end_date\']);
$end_time = sanitize_text_field($_POST[\'ev_end_time\']);
$ev_organizer = sanitize_text_field($_POST[\'ev_organizer\']);
$ev_organizer_url = sanitize_text_field($_POST[\'ev_organizer_url\']);
$group = sanitize_text_field($_POST[\'ev_group\']);
$post_id = wp_insert_post(array (
\'post_type\' => \'bpem_event\',
\'post_title\' => $title,
\'post_content\' => $content,
\'post_status\' => \'publish\'
));
add_post_meta( $post_id, \'evn_location\', $location );
add_post_meta( $post_id, \'evn_startDate\', $start_date);
add_post_meta( $post_id, \'evn_startTime\', $start_time);
add_post_meta( $post_id, \'evn_endDate\', $end_date);
add_post_meta( $post_id, \'evn_endTime\', $end_time);
add_post_meta( $post_id, \'evn_organizer\', $ev_organizer);
add_post_meta( $post_id, \'evn_organizer_url\', $ev_organizer_url);
add_post_meta( $post_id, \'evn_group\', $group);
add_post_meta( $post_id, \'evn_group_slug\', sanitize_title($group));
// Add Featured Image to Post
$imagebase = basename( $image_url );
$image_name = $imagebase;
$upload_dir = wp_upload_dir(); // Set upload folder
$image_data = file_get_contents($image_url); // Get image data
$unique_file_name = wp_unique_filename( $upload_dir[\'path\'], $image_name ); // Generate unique name
$filename = basename( $unique_file_name ); // Create image file name
// Check folder permission and define file location
if( wp_mkdir_p( $upload_dir[\'path\'] ) ) {
$file = $upload_dir[\'path\'] . \'/\' . $filename;
} else {
$file = $upload_dir[\'basedir\'] . \'/\' . $filename;
}
// Create the image file on the server
file_put_contents( $file, $image_data );
// Check image file type
$wp_filetype = wp_check_filetype( $filename, null );
// Set attachment data
$attachment = array(
\'post_mime_type\' => $wp_filetype[\'type\'],
\'post_title\' => sanitize_file_name( $filename ),
\'post_content\' => \'\',
\'post_status\' => \'publish\'
);
// Create the attachment
$attach_id = wp_insert_attachment( $attachment, $file, $post_id );
// Include image.php
require_once(ABSPATH . \'wp-admin/includes/image.php\');
// Define attachment metadata
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
// Assign metadata to attachment
wp_update_attachment_metadata( $attach_id, $attach_data );
// And finally assign featured image to post
set_post_thumbnail( $post_id, $attach_id );
echo "Success";
wp_die();
}
输出
SO网友:Zaheer Abbas
这是我自己问题的答案,它肯定会帮助其他人解决同样的问题。
<?php
add_action( \'wp_ajax_bpem_event_form_response\', \'bpem_event_form_response\' );
add_action( \'wp_ajax_nopriv_bpem_event_form_response\', \'bpem_event_form_response\' );
function bpem_event_form_response() {
$title = sanitize_text_field($_POST[\'ev_title\']);
$content = $_POST[\'ev_desc\'];
$image_url = sanitize_text_field($_POST[\'ev_image\']);
$location = sanitize_text_field($_POST[\'ev_location\']);
$start_date = sanitize_text_field($_POST[\'ev_start_date\']);
$start_time = sanitize_text_field($_POST[\'ev_start_time\']);
$end_date = sanitize_text_field($_POST[\'ev_end_date\']);
$end_time = sanitize_text_field($_POST[\'ev_end_time\']);
$ev_organizer = sanitize_text_field($_POST[\'ev_organizer\']);
$ev_organizer_url = sanitize_text_field($_POST[\'ev_organizer_url\']);
$group = sanitize_text_field($_POST[\'ev_group\']);
$post_id = wp_insert_post(array (
\'post_type\' => \'bpem_event\',
\'post_title\' => $title,
\'post_content\' => $content,
\'post_status\' => \'publish\'
));
add_post_meta( $post_id, \'evn_location\', $location );
add_post_meta( $post_id, \'evn_startDate\', $start_date);
add_post_meta( $post_id, \'evn_startTime\', $start_time);
add_post_meta( $post_id, \'evn_endDate\', $end_date);
add_post_meta( $post_id, \'evn_endTime\', $end_time);
add_post_meta( $post_id, \'evn_organizer\', $ev_organizer);
add_post_meta( $post_id, \'evn_organizer_url\', $ev_organizer_url);
add_post_meta( $post_id, \'evn_group\', $group);
add_post_meta( $post_id, \'evn_group_slug\', sanitize_title($group));
echo "Success 1";
require_once(ABSPATH . \'wp-admin/includes/media.php\');
require_once(ABSPATH . \'wp-admin/includes/file.php\');
require_once(ABSPATH . \'wp-admin/includes/image.php\');
$image = media_sideload_image($image_url, $post_id,"Image",\'id\');
set_post_thumbnail( $post_id, $image );
echo "Success";
wp_die();
}