我在主题目录中的一个单独PHP文件中有一个表单,它包含在主页模板的侧栏中。表单位于Boostrap模式中。我一直试图使用此代码允许用户使用表单提交新帖子,但此代码没有插入任何内容:
<?php
if ( isset( $_POST[\'submitted\'] ) && isset( $_POST[\'post_nonce_field\'] ) && wp_verify_nonce( $_POST[\'post_nonce_field\'], \'post_nonce\' ) ) {
$subjectError = \'\';
if ( trim($_POST[\'subject\'] ) === \'\' ) {
$subjectError = "Please enter the subject.";
$error = true;
}
$post_information = array(
\'post_title\' => wp_strip_all_tags( $_POST[\'subject\'] ),
\'post_content\' => $_POST[\'postContent\'],
\'post_type\' => \'post\',
\'post_status\' => \'pending\'
);
$post_id = wp_insert_post( $post_information );
if ( $post_id ) {
wp_redirect("/home");
exit;
}
}
?>
<div class="submit-content">
<div id="submit-modal" class="modal hide fade">
<div class="modal-header">
<h3>Submit content</h3>
</div>
<div class="modal-body">
<form class="form" id="submit-content-form" method="POST" action="<?php the_permalink(); ?>">
<?php if ( $subjectError != \'\' ) { ?>
<span class="error"><?php echo $subjectError; ?></span>
<div class="clearfix"></div>
<?php } ?>
<div class="control-group subject-group">
<label for="" class="control-label">Subject</label>
<div class="controls">
<input type="text" name="subject" id="subject" class="required" value="<?php if ( isset( $_POST[\'subject\'] ) ) echo $_POST[\'subject\']; ?>" />
</div>
</div>
<div class="control-group content-group">
<label for="" class="control-label">Content</label>
<div class="controls">
<textarea name="postContent" id="postContent" cols="30" rows="10" class="required">
<?php if ( isset( $_POST[\'postContent\'] ) ) { if ( function_exists( \'stripslashes\' ) ) { echo stripslashes( $_POST[\'postContent\'] ); } else { echo $_POST[\'postContent\']; } } ?>
</textarea>
</div>
</div>
<div class="control-group upload-group">
<label for="" class="control-label">Upload file</label>
<div class="controls">
<input type="file" value="Choose file">
</div>
</div>
</form>
</div>
<div class="modal-footer">
<input type="hidden" name="submitted" id="submitted" value="true" />
<?php wp_nonce_field( \'post_nonce\', \'post_nonce_field\' ); ?>
<a href="" class="btn btn-vq-green" onclick="document.getElementById(\'submit-content-form\').submit()">Submit</a>
</div>
请帮忙!我在这里完全迷路了。我一直在网上到处搜索,但找不到解决方法。我做错了什么?
谢谢
EDIT: 这是工作代码。它现在创建帖子,但验证不起作用,允许创建空帖子,直到我添加了一个检查,以确保提交表单时主题字段不为空。现在正在创建帖子,额外的检查将阻止添加空白帖子。只有当我添加一个elseif块来单独检查表单提交时哪个字段仍然为空时,验证才起作用。
<?php
if ( isset( $_POST[\'submitted\'] ) && !empty ( $_POST[\'subject\'] ) && isset( $_POST[\'post_nonce_field\'] ) && wp_verify_nonce( $_POST[\'post_nonce_field\'], \'post_nonce\' ) ) {
$subjectError = \'\';
if ( trim($_POST[\'subject\'] ) === \'\' ) {
$subjectError = "Please enter the subject.";
$error = true;
}
$post_information = array(
\'post_title\' => wp_strip_all_tags( $_POST[\'subject\'] ),
\'post_content\' => $_POST[\'postContent\'],
\'post_type\' => \'post\',
\'post_status\' => \'pending\'
);
$post_id = wp_insert_post( $post_information );
if ( $post_id ) {
wp_redirect("/home");
exit;
}
} elseif ( isset( $_POST[\'submitted\'] ) && empty ( $_POST[\'subject\'] ) ) {
$subjectError = "Please enter the subject.";
$error = true;
}
?>
<div class="submit-content">
<div id="submit-modal" class="modal hide fade">
<div class="modal-header">
<h3>Submit content</h3>
</div>
<div class="modal-body">
<form class="form" id="submit-content-form" method="POST" action="">
<div class="control-group subject-group">
<label for="" class="control-label">Subject</label>
<div class="controls">
<input type="text" name="subject" id="subject" class="required" value="<?php if ( isset( $_POST[\'subject\'] ) ) echo $_POST[\'subject\']; ?>" />
</div>
</div>
<div class="control-group content-group">
<label for="" class="control-label">Content</label>
<?php if ( $subjectError != \'\' ) { ?>
<span class="error"><?php echo $subjectError; ?></span>
<div class="clearfix"></div>
<?php } ?>
<div class="controls">
<textarea name="postContent" id="postContent" cols="30" rows="10" class="required">
<?php if ( isset( $_POST[\'postContent\'] ) ) { if ( function_exists( \'stripslashes\' ) ) { echo stripslashes( $_POST[\'postContent\'] ); } else { echo $_POST[\'postContent\']; } } ?>
</textarea>
</div>
</div>
<div class="control-group upload-group">
<label for="" class="control-label">Upload file</label>
<div class="controls">
<input type="file" value="Choose file">
<input type="hidden" name="submitted" id="submitted" value="submitted" />
<?php wp_nonce_field( \'post_nonce\', \'post_nonce_field\' ); ?>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<a href="javascript:void(0)" class="btn btn-vq-green" onclick="document.getElementById(\'submit-content-form\').submit()">Submit</a>
</div>
</div>
</div>
最合适的回答,由SO网友:engelen 整理而成
nonce字段不会添加到表单中,因此在提交表单时不会在POST请求中传递。因此isset( $_POST[\'post_nonce_field\'] )
退货false
条件从来都不是真的。尝试将nonce字段移动到开头表单标记(<form>
) 和关闭表单标签(</form>
).
EDIT: 问题实际上出在JavaScript中。您当前拥有的“提交”链接提交表单,但不会停止链接的正常点击事件的传播,该链接实际上是在链接之后。因此href
属性为空,它实际上会通过正常的GET请求将您指向当前页面。
要解决此问题,您可以添加return false;
就在submit()
表单的JavaScript方法调用。然而,实现这一点的更好方法是使用button
或submit
元素,或者至少通过JavaScript调用表单提交函数,以便使用preventDefault
而不是返回false。
TL;DR 使用按钮或输入submit元素(最佳选择),通过jQuery调用submit并添加e.preventDefault()
(次佳)或添加return false;
在JavaScript之后submit()
调用(最差但最简单的解决方案)。