Skip to content

WordPress tip: Catching callbacks in admin

I wanted to share this quick WordPress tip that you can use when you need some custom processing or catching callbacks in admin.

There is a wonderful WordPress hook (that I only found recently) that enables user to do some custom action very easily – admin_action_YOUR_ACTION_NAME.

It is setup in three quick and easy steps:

First you need to initialize the action:
[code lang=”php”]
add_action(‘admin_action_YOUR_ACTION_NAME’, ‘YOUR_FUNCTION_NAME’);
[/code]

Then in your function (YOUR_FUNCTION_NAME), after you do all the processing, you need to redirect the user somewhere:
[code lang=”php”]
// For example we’ll redirect him to dashboard here
wp_redirect(admin_url());
// good to exit after redirect
exit;
[/code]

And the last thing you need to do is call your function when you go to URL looking like this:
[code lang=”php”]
admin_url(‘admin.php?action=YOUR_ACTION_NAME’);
[/code]

For multi-site installation, the hook is network_admin_YOUR_ACTION_NAME

This is very useful when you need a callback after oAuth action to return to your WordPress admin for example.

Do you know some useful usage of this wonderful hook? Share it in comments below…