下面是我用来编写简短代码表单和添加帖子的代码。
以下是我试图解决的问题的基本概述:
-此表单显示在/addmember/页面上-此页面上还有现有成员的列表,包括当前成员和挂起成员(成员为自定义帖子类型)
-此表单用于提交待定成员
-提交此表单时,该成员将自动被赋予挂起状态,然后显示在/addmember/页面的挂起成员部分,因此,提交成员后,页面将刷新,以在/addmember/页面的挂起成员列表中显示新的挂起成员
-现在,这是正常的,但在用户单击提交后,/addmember/page会出现刷新--用户看到的是同一个表单,其中有空字段--列表不会出现,在/addmember/page上不会出现其他信息。他们看到的只是那张空表格-最后,几秒钟后,重定向到/the/addmember/page刷新
***我想找出一种方法来消除这种奇怪的时刻,当他们看到几乎是空的页面,只有空的表单,然后才最终重定向/刷新。我该怎么做?我尝试使用wp\\u重定向,但似乎没有效果。我无法在代码中进一步上移刷新操作。
<?php
function ty_front_end_form() { ?>
<form id="mreg" method="POST" action="">
<label for="mfname">Name</label>
<input type="text" id="mfname" name="mfname" value="" /><br/>
<label for="mem">Email</label>
<input type="text" id="mem" name="mem" value="" /><br/>
<label for="mpw">Why</label>
<textarea id="mpw" cols="19" rows="4" name="mpw"/></textarea><br/>
<input type="submit" name="submit" value="Submit" style="margin-left: 201px; background: #000;"/>
<input type="hidden" name="second-nom" id="second-nom" value="No" />
<input type="hidden" name="start" id="start" value="1" />
<input type="hidden" name="post-type" id="post-type" value="add-members" />
<input type="hidden" name="action" value="add-members" />
</form>
<?php
if( isset( $_POST[\'start\'] ) ) {
ty_save_post_data();
} ?>
<?php }
add_shortcode(\'add-members\',\'ty_front_end_form\');
function ty_save_post_data() {
$post = array(
\'post_title\' => $_POST[ \'mfname\' ],
\'post_status\' => \'publish\',
\'post_type\' => \'add-members\'
);
$post_id = wp_insert_post($post);
add_post_meta($post_id, \'mfname\', sanitize_text_field( $_POST[ \'mfname\' ] ), true);
add_post_meta($post_id, \'mem\', sanitize_text_field( $_POST[ \'mem\' ] ), true);
add_post_meta($post_id, \'mpw\', esc_attr( $_POST[\'mpw\'] ), true);
add_post_meta($post_id, \'second-nom\', sanitize_text_field( $_POST[ \'second-nom\' ] ), true );
$location = \'http://example.com/addmember/\';
echo "<meta http-equiv=\'refresh\' content=\'0;url=$location\' />";
exit;
}
?>
PER MILO\'S ANSWER HERE IS THE UPDATED CODE
<?php
function ty_front_end_form() { ?>
<form id="mreg" method="POST" action="">
<label for="mfname">Name</label>
<input type="text" id="mfname" name="mfname" value="" /><br/>
<label for="mem">Email</label>
<input type="text" id="mem" name="mem" value="" /><br/>
<label for="mpw">Why</label>
<textarea id="mpw" cols="19" rows="4" name="mpw"/></textarea><br/>
<input type="submit" name="submit" value="Submit" style="margin-left: 201px; background: #000;"/>
<input type="hidden" name="second-nom" id="second-nom" value="No" />
<input type="hidden" name="start" id="start" value="1" />
<input type="hidden" name="post-type" id="post-type" value="add-members" />
<input type="hidden" name="action" value="add-members" />
</form>
<?php }
add_shortcode(\'add-members\',\'ty_front_end_form\');
function ty_save_post_data() {
if( isset( $_POST[\'start\'] ) ) {
$post = array(
\'post_title\' => $_POST[ \'mfname\' ],
\'post_status\' => \'publish\',
\'post_type\' => \'add-members\'
);
$post_id = wp_insert_post($post);
add_post_meta($post_id, \'mfname\', sanitize_text_field( $_POST[ \'mfname\' ] ), true);
add_post_meta($post_id, \'mem\', sanitize_text_field( $_POST[ \'mem\' ] ), true);
add_post_meta($post_id, \'mpw\', esc_attr( $_POST[\'mpw\'] ), true);
add_post_meta($post_id, \'second-nom\', sanitize_text_field( $_POST[ \'second-nom\' ] ), true );
}
}
add_action( \'init\', \'ty_save_post_data\' );
?>