admin管理员组

文章数量:1292308

I am developing a plugin that is going to be used internally (inside the admin panel only).

I wonder what is the correct way to build the requests (action, params, etc) and what hook to use to be able to catch those requests. Can somebody help?

I am developing a plugin that is going to be used internally (inside the admin panel only).

I wonder what is the correct way to build the requests (action, params, etc) and what hook to use to be able to catch those requests. Can somebody help?

Share Improve this question asked Oct 31, 2016 at 8:26 user105980user105980 11 bronze badge 1
  • admin_init is your guy or if( is_admin() && ( !defined('DOING_AJAX') || !DOING_AJAX ) ) { # catch'em } – Ismail Commented Oct 31, 2016 at 10:17
Add a comment  | 

2 Answers 2

Reset to default 1

You should post some code examples, explaining what are you trying to do, to get more useful answers.

There are various hooks, and the one you should use depends on what you need. Anyway, here Codex: Actions Run During an Admin Page Request and here Codex: Administrative Actions the codex list the hooks to use for admin panel requests.

Example: If you have to activate something in the admin initialization, you can use the admin_init action hook, so the code will be: add_action('admin_init', 'your-custom-function'); If you have to operate when a page load in the dashboard, you can use the load-(page) hook; for example: add_action('load-post.php', 'your-custom-function'); and so on.

You can manage GET and POST requests with $_GET['x'] and $_POST['x'], where x is the variable you want to take from the query string. Example:

wp-admin/post.php?post=69
$_GET['post'] == 69

I don't know much, but I like this way.

For forms:

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

Backend

add_action( 'admin_post_nopriv_my_action', 'myhandler' );
add_action( 'admin_post_my_action', 'myhandler' );

function myhandler(){

    // Do magic :)
    print_r($_POST);

}

本文标签: pluginsThe correct way to handle GET or POST requests in the admin panel