5

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?

1

3 Answers 3

4

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.

1
  • 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. Jun 3, 2019 at 11:55
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.

0
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.

5
  • 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, 2016 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, 2016 at 14:51
  • You will need to separate this function and put it on the init hook Jun 8, 2016 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, 2016 at 7:39
  • Her follow up question is here: wordpress.stackexchange.com/questions/229228/… Jul 24, 2022 at 18:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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