2

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?

4

If your page has been submitted, then you can be pretty sure that $_SERVER['HTTP_REFERER'] is available.

So you can do either: wp_redirect($_SERVER['HTTP_REFERER']); or header('Location: ' . $_SERVER['HTTP_REFERER'])

This will do a redirect to the same page as you're already on.

| improve this answer | |
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.

| improve this answer | |
  • 1
    Was close to this approach but couldn't figure out how to trigger it from within my function. Something tells me I should maybe be fixing this earlier in the pipeline. – DrA7 Jun 3 '19 at 11:55
1

It's because your function archive_action is executed when the loop was already made. Try hooking your function into the init action, so it would be executed before the loop.

Another prettier approach would be using ajax. So you send the request through Ajax to archive and just use javascript to remove the post from the DOM.

| improve this answer | |
  • Thank you! But how could I pass a variable using init? I mean I should target a specific row. Now I send the post_id through do_action('archive_action', $pid);. Could you explain me it? It's not so clear for me. – middlelady Jun 8 '16 at 14:41
  • I've edited the question and I've explained that my function is in functions.php and converted in an action. I missed that. I call then this action in my loop passing the $post->id to archive. How could I do with init? – middlelady Jun 8 '16 at 14:51
  • You will need to separate this function and put it on the init hook – Bruno Cantuaria Jun 8 '16 at 22:01
  • Thanks Bruno, I really appreciate it. But if I'm asking it it's because I don't know how to do it. Do you have a link with examples? I can't find any reference around. Thanks again. – middlelady Jun 9 '16 at 7:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.