我使用 wordpress cms,我需要定制表格的帮助。它允许用户从前端发布。有几个字段和一个图像上传字段。这是一个由两部分组成的表格,第一部分是他提交数据的地方,第二部分是他预览刚刚提交的数据的地方。请记住,这发生在提交之后。
表格代码:
<?php
global $wpdb;
$this_page = $_SERVER['REQUEST_URI'];
$page = $_POST['page'];
if ( $page == NULL ) { ?>
<form method="post" action="">
<div>LOCATION : <input type="text" name="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" value="PROCEED"/>
</form>
<?php
} else if ( $page == 1 ) { ?>
<?php
$location=$_POST['location'];
$description=$_POST['details'];
$photo=$_POST['loc-image'];
echo 'Location : ' . $location . '</br>';
echo 'Details : ' . $description . '</br>';
echo 'Image : ' . $photo . '</br></br>';
?>
<?php
}
?>
问题:在最后一步中,我当前的代码能够显示所有提交的数据以及图像名称,但不能显示图像。我想在这一点上显示图像。基本上,我需要最后一步来做一个预览页面。
下面给出的代码处理表单,我想特别提请您注意代码中有助于插入附件的特定部分,因为它可能有助于设计解决方案。
表格处理器:
if( 'POST' == $_SERVER['REQUEST_METHOD'] && $_POST['action'] == "post_action") {
//some error checking done here
//set vars
$location = $_POST['location'];
$description = $_POST['details'];
if (empty($error)) {
$new_post = array( //insert form inputs, set var and define array
'post_title' => $location,
'post_content' => $description,
'post_status' => 'draft',
'post_type' => 'post',
// assigning tags and categoris is 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);
// $newupload returns the attachment id of the file that
// was just uploaded. Do whatever you want with that now.
}
} // end of attachment statement
}
}
脚注:除了问题之外,代码已经过大量定制,并且在我的 wp-install 上运行良好。因此,请忽略代码中的次要因素,例如。验证、安全等