• Resolved ace0930

    (@ace0930)


    I’m using admin_post_{$action} to handle the form. I just copy-paste the example from https://developer.www.ads-software.com/reference/hooks/admin_post_action/.

    I created front-page.php in the theme directory and just paste in:

    <form action="https://www.example.com/wp-admin/admin-post.php" method="post">
    <input type="hidden" name="action" value="our_action_hook">
    <input type="submit" value="Submit">
    </form>

    Then this in the functions.php :

    add_action( 'admin_post_our_action_hook', 'am_our_action_hook_function' );
    function am_our_action_hook_function() {
        // do something
    }

    But I got an error in the console: 400 bad request . If I make this as a plugin, it works. But if I do it in a theme, it doesn’t…

Viewing 9 replies - 1 through 9 (of 9 total)
  • @ace0930

    Change form action value as below

    <form action="<?php echo admin_url( 'admin-post.php' ); ?>p" method="post">

    Hope this will help you!

    Thread Starter ace0930

    (@ace0930)

    @weboccults Thanks, but I already tried that before…

    Moderator bcworkz

    (@bcworkz)

    I tried your code on my site, placing error_log('admin_post_our_action_hook action fired'); in place of //do something. (and using correct domain name in the form action of course). The action does indeed fire, the message was logged. Maybe there is an issue with whatever you had in place of //do something?

    Thread Starter ace0930

    (@ace0930)

    @bcworkz I have found the problem, it only occurs when I log out of WordPress.Why is that so…May I know how to solve this?

    Thread Starter ace0930

    (@ace0930)

    @bcworkz Found solution admin_post_nopriv_{action}

    But what’s the benefit of using admin_post_nopriv_{action} to handle the form instead of the traditional way like below which uses the currently executing script with get_permalink()?

    
    <form action="<?php echo get_permalink() ?>" method="post">
       <input type="submit" value="Submit">
    </form>
    

    and with the init hook

    
    add_action( 'init', 'capture_form' );
    
    function capture_form() {
       // Do something
    }
    
    Moderator bcworkz

    (@bcworkz)

    If you hook “init”, your callback will need to at least check to see if the current request came from the form. This must be done for every single request for anything. By using a specific admin_post action, there’s no need to check, your callback only executes for that specific form request.

    While there may be minimal added code to check every single request, we still should want to avoid doing so when there are more specific actions we can hook. It helps keep your overall page speed down by not loading up code on the “init” action.

    Thread Starter ace0930

    (@ace0930)

    @bcworkz I see your points but I don’t understand this, is it a typo?

    It helps keep your overall page speed down by not loading up code on the “init” action

    I think the page will speed up if handle the form for a specific hook like ajax_post instead of using the init which checks every request for everything?

    Why do you say the page speed will go down (slower) if we don’t load it through init?
    Should it be ‘It helps keep your overall page speed down by loading up code on the “init” action.’ or ‘It helps keep your overall page speed up by not loading up code on the “init” action’?

    Moderator bcworkz

    (@bcworkz)

    Ha! I guess up/down isn’t a good reference without context. I was thinking “down” as in fewer microseconds, e.g. faster. Down == slower would be as in page speed score and highway speed limits I guess? Since it’s ambiguous, we’re both right ?? Either way, you clearly understand my point.

    By itself, it wouldn’t make much difference. But if every potential process in a site did something similar, it starts adding up. In many cases, is more of a philosophy of good practice than any significant difference in performance. It’s not unheard of for people to use init to branch out to a process. It’s not the worst practice, since init fires relatively early. But I prefer to use an action intended for the purpose over a general use action.

    Thread Starter ace0930

    (@ace0930)

    @bcworkz Thank you for the detailed explanation and really appreciate your patience!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘admin_post_{$action} won’t work in theme’ is closed to new replies.