0

I managed to create a view which submits data from a form to my plugin via admin_post_* action.

This is what I have:

class MyPluginClass {
    public function __construct() {
        add_action('admin_post_formsbmt', array($this, 'formSubmit'));
    }

    public function formSubmit() {
        // handle the post here
    }
}

Then, in the form:

<form method="post" action="<?=admin_url('admin-post.php')?>">
    <input type="hidden" name="action" value="formsbmt">
    [other inputs]
</form>

It works, but this way the user must be an admin to perform the POST request.

Is there an equivalent for guest user? I need to put this form on a public page so that anyone can submit forms.

Is that possible?

3

If you look at admin-post.php, there is a check for wp_validate_auth_cookie() not whether the user is an administrator. So anyone with a valid authorization cookie will trigger the "admin_post_{$action}" hook.

If the user doesn't have a valid authorization cookie, then WP will trigger "admin_post_nopriv_{$action}".

  • 1
    I was just writing an answer, but you beat me to it ;-) Maybe you could also mention the general action less admin_post_nopriv for completeness? – birgire Feb 26 '17 at 21:17
  • Well for me it works when I'm logged in and it shows a blank page when I logout and try to submit the form... Then I log back in and it works again. I've just tried binding admin_post_nopriv_formsbmt too and nothing changed. – BackSlash Feb 26 '17 at 22:52
  • Nevermind... Cache in Chrome is a pain :) Even if dev tools are open and the setting says "Disable cache (while DevTools is open)" it still caches everything up. Your solution worked after hundreds of page refreshes, thank you! – BackSlash Feb 26 '17 at 22:58

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.