我將wordpress作爲CMS運行。我已經制作了一個簡單的多部分表單,該表單工作得很好,並允許從前端發佈。這是一個兩部分的形式,第一部分是他提交數據的位置,第二步是他剛剛提交的數據預覽。提交的數據按預期在wp中創建了一個很好的草稿。在頁面刷新時重新提交表單 - 而在wordpress的預覽頁面
這是表單頁面模板中的第一個代碼塊,它顯示了表單。在代碼的下半部分,您將注意到預覽部分(閱讀代碼中的註釋),其中顯示剛剛提交的數據。所有這些工作真的很好,我甚至可以在提交之後retrieve an image。
<?php
global $wpdb;
$this_page = $_SERVER['REQUEST_URI'];
$page = $_POST['page'];
if ($page == NULL) { ?>
<form method="POST" id="test_form" name="test_form" enctype="multipart/form-data" action="">
<div>LOCATION : <input type="text" name="location" id="location"/></div>
<div>DESCRIPTION : <textarea id="details" cols="80" rows="10 maxlength="600" name="details" rows="20"></textarea></div>
<div>UPLOAD IMAGE : <input type="file" name="loc-image" id="loc-image" tabindex="25" /></div>
<input type="hidden" value="1" name="page" />
<input type="hidden" name="action" value="post_action" />
<input type="submit" name="submit" id="submit" value="PROCEED"/>
</form>
<?php
} else if ($page == 1) { ?>
<?php include_once('validate_first_step.php'); ?>
<?php if (isset($_POST['submit']) && (!empty($error))) { ?>
<h3>Submission Failed. Errors highlighted below.</h3><br/>
<a href="javascript:history.go(-1)">GO BACK</a><br/><br/>
<?php echo $error . '</br>';
} else { ?>
<?php echo 'h2>'.'YOUR SUBMISSION IS ACCEPTED. PREVIEW.'. '</h2>';?>
<?php //PREVIEW SECTION OF THE FORM BEGINS
$location=$_POST['location'];
$description=$_POST['details'];
?>
<?php
echo 'Location : ' . $location . '</br>';
echo 'Details : ' . $description . '</br>';
?>
<?php echo wp_get_attachment_image($newupload,'medium'); ?>
<?php //PREVIEW ENDS
}
}
?>
這是如何處理表格的。實際的代碼非常複雜,我只是放在這裏,這是實現這個想法所必需的。相當標準的東西插入WordPress的。
if('POST' == $_SERVER['REQUEST_METHOD'] && !empty($_POST['action']) && $_POST['action'] == "post_action") {
// Do some minor form validation to make sure there is content
if (isset($_POST['submit'])) {
$error = "";
if ($_POST['details'] != null) {
$description = trim(strip_tags($_POST['details']));
} else {
$error .= 'Put description.</br>';
}
if ($_POST['location'] != null) {
$location = trim(strip_tags($_POST['location']));
} else {
$error .= 'Put location.</br>';
}
}
if (empty($error)) {
$new_post = array( //insert form inputs, set var and define array
'post_title' => $location,
'post_content' => $description,
'post_status' => 'draft',
'post_author' => '2',
'post_type' => 'post'
// assigning tags and categories are no issue
);
$pid = wp_insert_post($new_post);
//attachment helper function
function insert_attachment($file_handler,$post_id,$setthumb='false') {
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK){ return __return_false();
}
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
$attach_id = media_handle_upload($file_handler, $post_id);
//set post thumbnail
if ($setthumb) update_post_meta($post_id,'_thumbnail_id',$attach_id);
return $attach_id;
}
//INSERT OUR MEDIA ATTACHMENTS
if ($_FILES) {
foreach ($_FILES as $file => $array) {
$newupload = insert_attachment($file,$pid);
}
}
} //end of if empty error
}
ISSUE:這裏是我在測試的形式注意到這個問題。表單提交後,當我在預覽部分,數據正常顯示正常,但當我REFRESH頁面相同的數據再次提交到wordpress,創建一個重複的草稿。
THINGS受審:我知道一個重定向解決方案,並能夠運用wp_redirect,但重定向是不是我在看,因爲我需要向他們展示預覽。我花了幾個小時尋找解決方案和替代方案,仍然在做。
REQUEST:請讓我知道如何阻止這種情況發生。我不能指望人們不點擊刷新,他們基本上會做任何他們想要的。所以,而不是一個JavaScript通知,我想使用服務器端PHP來執行此操作。因爲它可能會執行,即使JavaScript被禁用,我的代碼的其餘部分。基本上我需要一個解決方案,在提交後忽略刷新。請給我一個代碼或提示,讓正確的方向可以做到這一點。非常感謝。
UPDATE:由於@ user2758004建議我嘗試了對比法和它的工作。我已將下面的代碼放在處理器的頂部。
$location=$_POST['location'];
$args = array(
'post__not_in'=> array($id),
'post_type' => 'post',
'post_status' => array('publish','pending','draft','future','private'),
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'location',
'value' => $location,
'compare' => '='
),
)
);
現在在處理器中,我檢查meta_key'full_name'的meta_value是否已經存在。如果是這樣,表單會顯示一個錯誤,並簡單地停止該過程。希望它能幫助別人。
$existingMeta = get_posts($args);
if(!empty($existingMeta)){
$error .= 'Bummer, ' .$location. ' is already in database.</br>';
return;
}
else {
//check for rest of the errors
}
怎麼樣在第一步中的會話數據保存和檢索在2步,以顯示預覽? – codepixlabs
我正在嘗試做你所建議的,但我無法獲得圖像,我只知道wordpress鉤子。不知道如何存儲和從會話拉圖像。有任何想法嗎? – gurung
在會話中存儲圖像是複雜的,不建議,因爲它更多的內存..你可以在存儲過程中存儲圖像的URL和回收圖像,同時保存數據 – codepixlabs