0

您好,我有一个带有文件上传的前端表单,在提交 wp_redirect 之后,header() 也没有工作。

<?php if( 'POST' == $_SERVER['REQUEST_METHOD'] ) { // if form has been submitted
update_field('field_2', $title, $postID);
update_field('field_3', $efternamn, $postID);
update_field('field_4', $epost, $postID);
update_field('field_6', $telefon, $postID);
update_field('field_7', $motiv, $postID);
if (!function_exists('wp_generate_attachment_metadata')){
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
}
if ($_FILES) {
foreach ($_FILES as $file => $array) {
if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
return "upload error : " . $_FILES[$file]['error'];
}
$attach_id = media_handle_upload( $file, $postID );
}
}
//if ($attach_id > 0){
//and if you want to set that image as Post then use:
//update_post_meta($postID,'_thumbnail_id',$attach_id);
//}
update_field('field_5', $attach_id, $postID);
$dialog_url='http://tatchit.com';
wp_redirect(home_url());
}
?>

有什么建议么?

4

1 回答 1

0

解决了,在functions.php中添加了一个带有函数的add_action(),如下所示:

  add_action( 'wp_loaded', 'submitBlogForm', 10, 2);
 function submitBlogForm($postID,$attach_id){
if( isset( $_POST['submitBlogForm'] ) ):
    // process form, and then
    //$postID = $_POST['postID']; 
 $title = $_POST['title']; 
 $efternamn = $_POST['efternamn']; 
 $epost = $_POST['epost'];
 $motiv = $_POST['motiv'];
 $telefon = $_POST['telefon'];
    update_field('field_2', $title, $postID);
    update_field('field_3', $efternamn, $postID);
    update_field('field_4', $epost, $postID);
    update_field('field_6', $telefon, $postID);
    update_field('field_7', $motiv, $postID);


   update_field('field_5', $attach_id, $postID);

    wp_redirect( home_url() );
endif;
 }

然后在我的模板文件中:

if (!function_exists('wp_generate_attachment_metadata')){
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
 require_once(ABSPATH . "wp-admin" . '/includes/media.php');
}
if ($_FILES) {
foreach ($_FILES as $file => $array) {
if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
return "upload error : " . $_FILES[$file]['error'];
}
$attach_id = media_handle_upload( $file, $postID );
}
 }

do_action( 'wp_loaded', $postID,$attach_id);
于 2013-03-30T09:22:14.593 回答