What I\'m doing:
发布一篇文章,该文章通过AJAX检查了部分字段。
The problem:
未保存发布的图像。其余的信息都是。图像未上载到服务器。
What I tried:
我试着使用$\\u POST,它就是这样工作的,在我不得不用AJAX检查内容之前,我不得不使用。preventDefault()和。提交()。由于我不得不使用AJAX,我尝试了很多东西,包括fileReader()。
What\'s being done in the code:
当用户单击“提交”时,我通过JS获取必须检查的字段,用AJAX将它们传递给PHP,如果一切正常,我将它们返回给JS并激活。使用表单ID提交()。
The part of the code that has the \'input=file\':
<input class="text-input file-input img_destaque" name="img_destaque" type="file" id="img_destaque" multiple="false" />
<input type="submit" class="btn-pub-ad cadastro-anuncio" id="publicar" name="publicar" value="PUBLICAR">
<?php wp_nonce_field( \'publicar\',\'nonce_anuncio\' ); ?>
The part of the code that has the submit button:
<input type="submit" class="btn-pub-ad cadastro-anuncio" id="publicar" name="publicar" value="PUBLICAR">
一
function adAnuncio(e){
e.preventDefault();
var form = document.getElementById(\'criar_anuncio\');
var formData = new FormData(form);
formData.append( \'action\', \'publicar_anuncio\' );
$.ajax({
type: "POST",
url: clocal.ajaxurl,
dataType: "html",
processData: false,
contentType: false,
data: formData,
}).success(function (data) {
$data = $(data);
$("#mensagem").html($data);
var anchor = $(\'#mensagem\');
$(\'html,body\').animate({scrollTop: anchor.offset().top},\'slow\');
if ($(\'.linha-sucesso\').length){
$(\'#criar_anuncio\').submit();
}
});
}
The part of the code that gets what was sent via AJAX and includes the PHP that has the code to create the post:
// if user is logged in
add_action(\'wp_ajax_publicar_anuncio\', \'publish_anuncio\');
// if user is not logged in
add_action(\'wp_ajax_nopriv_publicar_anuncio\', \'publish_anuncio\');
function publish_anuncio() {
if ( count( $errors->get_error_messages() ) < 1 ) :
echo \'<div class="linha-sucesso">\';
echo \'<img class="ico-sucesso" src="\'. get_template_directory_uri() .\'/assets/images/success-icon.png">\';
echo \'Seu anúncio foi cadastrado correctamente.\';
echo \'</div>\';
include \'inc/inserir-anuncios-verificacao.php\';
endif;
wp_die();
The part where the file should be handled, stored and attached to the post:
$post_type = \'cadastro_anuncios\';
//the array of arguements to be inserted with wp_insert_post
$postStatus = \'publish\';
$new_post = array(
\'post_title\' => $title,
\'post_status\' => $postStatus,
\'post_type\' => $post_type,
);
//treating the images
// These files need to be included as dependencies when on the front end.
require_once( ABSPATH . \'wp-admin/includes/image.php\' );
require_once( ABSPATH . \'wp-admin/includes/file.php\' );
require_once( ABSPATH . \'wp-admin/includes/media.php\' );
$img_id01 = media_handle_upload( $_POST[\'img_destaque\'], $postID );
update_post_meta($postID, \'img_id01\', $img_id01);
It doesnt work. Even if I remove the .submit(), no error is printed or showed on the console. If I keep the .submit() everything gets saved properly except for the images.