1

My problem is that I can't refresh the page after a form action into a loop. In my case I have a listing post page where I can delete, archive, deliver posts through a modal form, row by row.

After asking a previous question here I've known that I should perform the form action through the init hook to make it run before headers are sent.

I've tried without success like this: In my functions.php

add_action('init', 'init_deliver'); //trying to run it before headers
function init_deliver($pid){
    //code to archive the post
    //update_post_meta etc...
}
add_action('deliver_post', 'delivered_action');
function delivered_action($pid) { 
    if(isset($_POST['deliver']))
    {   
        init_deliver($pid); //calling a the init function
    } ?>
 <div id="modal" class="modal fade" role="dialog">
     <form method="post"> 
        <input id="deliver" type="submit" name="deliver" class="submit_green" style="width:100%; text-align:center;" />
     </form>
 </div>
}

And I call all this in my loop like this

<a href="#" data-toggle="modal" data-target="#-modal">Deliver?</a>
<?php do_action("deliver_post", $pid); ?> <!--I pass here the post id $pid-->

The post is delivered but the page doesn't refresh when I submit the form. It refreshes always the second time I reload it. I need instead that the page is immediately refreshed after form submitting. Can you please give me an example or drive me through it? Thanks.

2
  • Why not wrap the code from init in if(isset($_POST['deliver'])) conditional? You can access all form data ($_POST here) from init or wp hook, in case some WordPress objects and functions don't work on init then switch to wp..
    – Ismail
    Commented Jun 9, 2016 at 12:03
  • The problem is that I'm in the loop and when $_POST is submitted the page doesn't refresh. I need to run the deliver before the loop.
    – middlelady
    Commented Jun 10, 2016 at 8:29

1 Answer 1

0

From the documentation: https://developer.wordpress.org/reference/hooks/init/

add_action( 'init', 'process_post' );
 
function process_post() {
     if( isset( $_POST['unique_hidden_field'] ) ) {
          // process $_POST data here
     }
}

In your example, perhaps having two different add_action is causing problems and burying your function where it's not firing during init

I'm going to test a similar solution and see what happens, I will update if I learn more.

1

Your Answer

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

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