我有一个用户帖子提交表单,它有三个问题,还有一个潜在问题。
下面是处理表单的代码。
<?php if(isset($_POST[\'user_submission_form\'])) {
if(wp_verify_nonce($_POST[\'user_submission_form\'], \'user_submission_form\')) {
$user_submitted_title = sanitize_text_field($_POST[\'user_submitted_title\']);
$user_submitted_progress = $_POST[\'user_submitted_progress\'];
$user_submitted_goals = $_POST[\'user_submitted_goals\'];
$user_submitted_categories = $_POST[\'user_submitted_categories\'];
$user_submitted_tags = sanitize_text_field($_POST[\'user_submitted_tags\']);
$user_submitted_video = sanitize_text_field($_POST[\'user_submitted_video\']);
$user_submitted_audio = sanitize_text_field($_POST[\'user_submitted_audio\']);
if(($user_submitted_title != \'\') && ($user_submitted_progress != \'\') && ($user_submitted_goals != \'\') && ($user_submitted_categories != \'\') && ($user_submitted_tags != \'\')) {
$user_post = array(
\'comment_status\' => \'open\',
\'post_author\' => $user_ID,
\'post_category\' => array($user_submitted_categories),
\'post_content\' => \'<h2>Project Progress</h2>\' . $user_submitted_progress . \'<h2>Project Goals</h2>\' . $user_submitted_goals,
\'post_status\' => \'publish\',
\'post_title\' => $user_submitted_title,
\'post_type\' => \'post\',
\'tags_input\' => $user_submitted_tags
);
$user_post_id = wp_insert_post($user_post);
add_post_meta($user_post_id, \'wpcf-video\', $user_submitted_video);
add_post_meta($user_post_id, \'wpcf-audio\', $user_submitted_audio);
global $post;
if ( $_FILES ) {
$files = $_FILES[\'upload_attachment\'];
foreach ($files[\'name\'] as $key => $value) {
if ($files[\'name\'][$key]) {
$file = array(
\'name\' => $files[\'name\'][$key],
\'type\' => $files[\'type\'][$key],
\'tmp_name\' => $files[\'tmp_name\'][$key],
\'error\' => $files[\'error\'][$key],
\'size\' => $files[\'size\'][$key]
);
$_FILES = array("upload_attachment" => $file);
foreach ($_FILES as $file => $array) {
$newupload = insert_attachment($file,$user_post_id);
}
}
}
}
$user_post_redirect = get_permalink($user_post_id);
wp_redirect($user_post_redirect); exit;
}
else {
//Notify the user that they must fill out all required fields
}
}
else {
print \'Sorry, your post did not verify.\';
exit;
}
}?>
<?php get_header(); ?>
这是表格(页眉后)
<?php if (is_user_logged_in()) { ?>
<form id="user_submitted_post" action="" method="POST" enctype="multipart/form-data">
<input type="hidden" name="user_submission_form" value="<?php echo wp_create_nonce(\'user_submission_form\'); ?>">
<h2>Title</h2>
<input type="text" id="user_submitted_title" name="user_submitted_title">
<p>Please provide detailed information about the long-term goals of your project, as well as your current project progress. Feel free to format your text using boldness, italics, lists, and block quotes. Simply click on the formatting option you\'d like to use and start typing. When you no longer need the format you have selected, hit enter to go to the next line, and click on the active format button to end formatting.</p>
<h2>Project Progress</h2>
<?php wp_editor( \'\', \'user_submitted_progress\', $settings = array(\'media_buttons\' => false, \'quicktags\' => false, \'textarea_rows\' => 15, \'editor_css\' => \'<style type="text/css">.wp_themeSkin .mceListBox .mceText {width: 81px;} .wp_themeSkin table.mceToolbar {margin: 5px;} td.mceToolbar > div {height: inherit;} tr.mceLast {display: none;} .wp_themeSkin .mceButton {margin: 1px 12px;}</style>\', \'tinymce\' => add_filter("mce_buttons", "base_extended_editor_mce_buttons", 0), add_filter("mce_buttons_2", "base_extended_editor_mce_buttons_2", 0)) ); ?>
<h2>Project Goals</h2>
<?php wp_editor( \'\', \'user_submitted_goals\', $settings = array(\'media_buttons\' => false, \'quicktags\' => false, \'textarea_rows\' => 15, \'tinymce\' => add_filter("mce_buttons", "base_extended_editor_mce_buttons", 0), add_filter("mce_buttons_2", "base_extended_editor_mce_buttons_2", 0)) ); ?>
<h2>Category</h2>
<?php wp_dropdown_categories($args = array(\'orderby\' => \'name\', \'hide_empty\' => 0, \'hierarchical\' => 1, \'id\' => \'user_submitted_categories\', \'name\' => \'user_submitted_categories\')); ?>
<h2>Tags</h2>
<p>Separate tags with commas.</p>
<input type="text" id="user_submitted_tags" name="user_submitted_tags">
<h2>Video</h2>
<p>Copy and paste links from Youtube and Vimeo in the field below</p>
<textarea id="user_submitted_video" name="user_submitted_video"></textarea>
<h2>Audio</h2>
<p>Copy and paste links from Soundcloud in the field below</p>
<textarea id="user_submitted_audio" name="user_submitted_audio"></textarea>
<h2>Images</h2>
<input type="file" name="upload_attachment[]" multiple="multiple">
<button name="submit">Submit</button>
</form>
<?php } else { ?>
<?php echo \'Sorry, but you need to be logged in to see that. You can <a href="\'; ?>
<?php echo wp_login_url( get_permalink() ); ?>
<?php echo \'" title="Login">login here</a>\'; ?>
<?php } ?>
这是函数中的函数。将图像附加到数据库中的帖子的php
//Attach Image to Post in Database
function insert_attachment($file_handler,$user_post_id,$setthumb=\'false\') {
if ($_FILES[$file_handler][\'error\'] !== UPLOAD_ERR_OK) __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, $user_post_id);
if ($setthumb) update_post_meta($user_post_id,\'_thumbnail_id\',$attach_id);
return $attach_id;
}
发布成功后,我会收到一个错误消息,说不能修改标题,但表单会进行处理,内容会发布到数据库。当用户没有填写所有必填字段时,页面将刷新,表单将完全为空,因此他们将失去表单进度,必须重新开始。说的部分
//Notify the user that they must fill out all required fields
是我要放置验证失败时发生的事件的位置。我希望它只是一个简单的警报弹出窗口或说填写所有必填字段的东西。除此之外,没有什么是必要的。它还有一个问题,如果你反复点击提交按钮,它将处理文章的次数与你点击提交按钮的次数相同,从而创建重复的文章。我怎样才能解决这个问题?
我唯一剩下的问题是,在这种情况下是否可以利用图像上传器。