Refresh page after form action

I’ve a listing post page where all posts are shown. On each post’s row I can perform actions like delete, archive etc. So imagine that:

  • I have an “archive” button;
  • I push on it and I get a modal (form) saying “Do you want archive this post?”
  • Into the modal I have the submit button, which should perform the form action and refresh the page, hiding the post just archived.

My problem: My post is archived but I have to refresh the page 2 times before I see it disappeared from my page. When I just click submit, the page refresh but I still see the post which is hidden the second time I reload.

I suppose that I’m doing wrong performing the action. My code in functions.php:

add_action('archive_post','archive_action');
function archive_action($pid) { 
    if(isset($_POST['archive']))
    {   
        update_post_meta($pid, 'archived', "1");
        //other code
    }

    ?>
<div id="archive-modal" class="modal fade" role="dialog">
    <div class="modal-dialog">
        <div class="box_title">
            <?php printf(__("Do you want archive this",'metheme'), $pid);?>
            <a class="pull-right" href="#" data-dismiss="modal">&times;</a>
        </div>
        <div class="box_content">
            <form method="post" action=""> 
                <input type="submit" name="archive" style="width:100%; text-align:center;" ?>" />
            </form>
        </div>
    </div>
</div>
<?php }

I call this function in my loop for passing the $post->id to archive like this:

do_action('archive_post', $pid);

What I’ve tried so far:

  • form action="" empty for refreshing the page, as in code: not working, the post stays and only if I refresh again it disappears;
  • wp_redirect(get_permalink()); in the $_POST function: same of before;
  • echo '<meta http-equiv="refresh" content="0.5;url=".$mylink."" />'; it works but, once again, it refreshes the page twice, it’s like before, only automated.

Is there some other solution? What am I missing please?

3 Answers
3

Searching around and not being able to implement the action through init hook I’ve found this workaround which for sure isn’t the best but does the job nicely.

echo "<script type="text/javascript">
        window.location=document.location.href;
        </script>";

at the end of $_POST instructions.
If somebody has a better solution, welcome to share.

Leave a Comment