WordPress tip: Catching callbacks in admin

W

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:

add_action('admin_action_YOUR_ACTION_NAME', 'YOUR_FUNCTION_NAME');

Then in your function (YOUR_FUNCTION_NAME), after you do all the processing, you need to redirect the user somewhere:

// For example we’ll redirect him to dashboard here
wp_redirect(admin_url());
// good to exit after redirect
exit;

And the last thing you need to do is call your function when you go to URL looking like this:

admin_url('admin.php?action=YOUR_ACTION_NAME');

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…

About the author

Luka Peharda

Web and Android developer. And a bit of a designer.

By Luka Peharda

Tags