Anders Östman
Anders Östman

Reputation: 3832

What hooks to use for Wordpress POST request.

There seems to be two hooks to handle POST request in Wordpress. The first is the admin-post hook. And the second is the admin_action_{$_REQUEST[‘action’]} hook.

Are they suitable for different occasions? Is one preferable over the other?

Upvotes: 0

Views: 1818

Answers (1)

user8262086
user8262086

Reputation:

The 'admin_post_' . $_REQUEST['action'] action hooks are used to handle forms e.g.,

<form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post">
    <input type="hidden" name="action" value="your_action">
    ...
</form>

The 'admin_action_' . $_REQUEST['action'] action hooks are used to handle actions on certain admin urls (the php files that include wp-admin/admin.php), e.g.,

'<a href="' . admin_url( "admin.php?action=your_action&..." ) . '">...</a>
'<a href="' . admin_url( "edit.php?action=your_action&..." ) . '">...</a>
'<a href="' . admin_url( "post.php?action=your_action&..." ) . '">...</a>

I don't think either of these hooks are frequently used. At least not in the plugins that I am using. Usually, the action is handled later by the called php file, e.g. wp-admin/edit.php, wp-admin.post.php, ...

Upvotes: 1

Related Questions