functions - Init action and refresh page after form action

admin2025-01-07  3

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.

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.

Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Jun 9, 2016 at 9:27 middleladymiddlelady 5531 gold badge6 silver badges17 bronze badges 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
Add a comment  | 

1 Answer 1

Reset to default 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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736253535a130.html

最新回复(0)