WordPress admin_post_{$action} hooks and instructions for use

Posted in:
Update time:2024-11-13

When developing WordPress themes or plugins, we often need to add some custom operations, for example, we created a form and need to submit data to the backend, WordPress provides us with theadmin_post_{$action}hook to facilitate our implementation of this backend.

Plugin Description

The dynamic part of the hook name$action refers to the given request operation.

Usage and examples

This hook allows us to create custom handlers for our own custom GET and POST requests.admin_post_ The format is "admin _post_$action", of which $action is the "action" parameter of a GET or POST request.

Usage

If we need to add a new value for the "add_foobarThe "action request creates a request or form handler, and you can create hooks like this:

add_action( 'admin_post_add_foobar', 'prefix_admin_add_foobar' );

function prefix_admin_add_foobar() {
    // After processing the request, use echo to generate a response or leave PHP to use HTML
}

Using the example above, simply send a GET or POST request to WordPress with the request "manipulate"The parameter is set to "add_foobar", the hook will be executed automatically. For example, the action in the HTML below, the hook above will be executed when the user clicks "Submit".

<a href="http://www.example.com/wp-admin/admin-post.php?action=add_foobar&data=foobarid">submit (a report etc)</a>

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

Note: Data values(foobarid)) can be found in the hook function in the $_GET,$_POST maybe $_REQUEST available in the array.

usage example

The following example allows you to hook a GET or POST request in the above html.

add_action( 'admin_post_add_foobar', 'prefix_admin_add_foobar' );

// This hook allows non-logged-in users to submit data.
add_action( 'admin_post_nopriv_add_foobar', 'prefix_admin_add_foobar' );; // This hook allows unlogged users to submit data.

function prefix_admin_add_foobar() {
    status_header(200);
    //request handlers should exit() when they complete their task
    exit("Server received '{$_REQUEST['data']}' from your browser."); }
}

With hooks, we can easily handle requests submitted by forms in the background, and this Hook is similar to the wp_ajax_($action) hooksThe difference is that the wp_ajax_($action) hook appends some processing to our custom callback, such as setting the DOING_AJAX constant.

We offer WordPress Themes and Plugins Custom Development Services

This site has long undertaken WordPress themes, plugins, WooCommerce-based store mall development business. We have 10 years of experience in WordPress development, if you want to Developing Websites with WordPress, please contact WeChat: iwillhappy1314 or email: amos@wpcio.com for inquiries.

发表回复

Your email address will not be published. 必填项已用 * 标注

*