WordPress表单提交后刷新页面,不带'Confirm Resubmission'警报


Refresh page after WordPress form submission, without 'Confirm Resubmission' alert

我有我的表单做什么需要(添加/删除标签从帖子取决于它是否存在)。但是-一旦我提交了表单页面不更新预期,$buttonTitle不更新,除非我在另一个选项卡中重新加载页面。如果我尝试刷新我得到消息'确认表单重新提交',我是一个完整的php表单新手,但这里是我的代码…

<?php
$idTag = $current_user->ID;
$spitOutTag = (string)$idTag;
if (has_tag( $spitOutTag, $post )) {
    $buttonTitle = 'REMOVE TAG';
} else {
    $buttonTitle = 'ADD TAG';
} ?>
<form name="primaryTagForm" action="<?php echo the_permalink() ?>" id="primaryTagForm" method="POST" enctype="multipart/form-data" >
    <fieldset>
        <input type="hidden" name="newtags" id="newtags" value="<?php echo $current_user->ID; ?>" />
        <?php wp_nonce_field( 'post_nonce', 'post_nonce_field' ); ?>
        <button id="btn-join" class="btn btn-lg" type="submit"><?php echo $buttonTitle ?></button>
    </fieldset>
</form>
<?php if ( is_user_logged_in() && isset( $_POST['newtags'] ) ) {
    if (has_tag( $spitOutTag, $post )) { 
        // Get all the tags as an array of names
        $tags = wp_get_post_tags( get_the_ID(), array('fields' => 'names') );
        // Remove the tag from the array
        $index = array_search( $idTag, $tags );
        unset( $tags[$index] );
        // Reset the tags
        wp_set_post_tags( get_the_ID(), $tags );
    } else {
        wp_set_post_tags(get_the_ID(), sanitize_text_field($_POST['newtags']), true );
    }   
} ?>

问题是你在检查标签后分配标签,这就是为什么按钮不会改变,你需要这样安排你的代码:

首先,检查是否发送了一些标签,并将其应用到帖子中:

<?php if ( is_user_logged_in() && isset( $_POST['newtags'] ) ) {
  ............
} ?>

然后检查按钮:

if (has_tag( $spitOutTag, $post )) {
    $buttonTitle = 'REMOVE TAG';
} else {
    $buttonTitle = 'ADD TAG';
} ?>

最后打印HTML:

<form name="primaryTagForm" action="<?php echo the_permalink() ?>"
   ....
</form>