从前台上传损坏的特色图片

时间:2019-03-25 作者: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));



// 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();
}
输出

enter image description here

1 个回复
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();
}

相关推荐

Testing Plugins for Multisite

我最近发布了一个WordPress插件,它在单个站点上非常有效。我被告知该插件在多站点安装上不能正常工作,我理解其中的一些原因。我已经更新了代码,现在需要一种方法来测试更新后的代码,然后才能转到实时客户的多站点安装。我有一个用于测试的WordPress安装程序的单站点安装,但需要在多站点安装上进行测试。根据我所能找到的唯一方法是在网络上至少有两个站点来安装整个多站点安装,以测试我的插件。设置WordPress的整个多站点安装是插件开发人员的唯一/首选方式,还是有更快的测试环境可用。