我正在尝试创建前端发布页面,但有时“wp\\u redirect”无法处理“cannot modify header information”错误。添加数据工作正常,但我不知道为什么有时工作正常而没有错误,有时显示错误。。
对不起,我英语不好,请帮帮我。。。我不是程序员,只是懂一些代码而已。。。
非常感谢!!
if (isset ($_POST[\'post_title\'])) {
$title = $_POST[\'post_title\'];
} else {
echo \'Please enter a game title\';
}
if (isset ($_POST[\'description\'])) {
$description = $_POST[\'description\'];
} else {
echo \'Please enter the content\';
}
$new_post = array(
\'post_title\' => $title,
\'post_content\' => $description,
\'post_status\' => \'publish\',
\'post_type\' => \'property\',
\'tax_input\' => array( \'property_type\' => array($property_type), \'suburbs\' => array($suburbs) )
);
//save the new post and return its ID
$pid = wp_insert_post($new_post);
//add thumbnail
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, $pid );
}
}
if ($attach_id > 0){
//and if you want to set that image as Post then use:
update_post_meta($pid,\'_thumbnail_id\',$attach_id);
} //add thumbnail end
update_post_meta($new_post_id, \'tax_input\', $property_type);
update_post_meta($new_post_id, \'tax_input\', $suburbs);
//insert custom fields
update_post_meta($pid,\'bathrooms\',$_POST[\'bathrooms\']);
update_post_meta($pid,\'property_lease\',$_POST[\'property_lease\']);
update_post_meta($pid,\'lost_found_date\',$_POST[\'lost_found_date\']);
update_post_meta($pid,\'dog_sex\',$_POST[\'dog_sex\']);
update_post_meta($pid,\'latitude\',$_POST[\'latitude\']);
update_post_meta($pid,\'longitude\',$_POST[\'longitude\']);
$url = get_permalink( $pid );
wp_redirect($url);
exit();
}
get_header();