Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

There's an action hook triggered in admin.php called 'admin_action_'.$_REQUEST['action']. Unfortunately, it's a bit useless for plugins as it's triggered at the very end of admin.php, after everything's loaded on the page (even the footer).

Does anyone know why it's there, why in that silly position and if anything is using it internally?

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

Via the "Annotate" function the the Trac, you can see that this was added three years ago, after the request for a generic POST handler that plugins can use.

Google code search tells me that at least Akismet uses this hook, and it appeared there at the time it was introduced in core. It works by calling admin.php directly (and not the plugin page). From there it can just do a redirect at the end. The trick is thus to call admin.php?action=your_action, other URLs are not guaranteed to work.

Many (but not all) admin pages include admin.php, as a sort of initialization. In this case your action would be fired before any output is sent, because the admin page includes admin-header.php after admin.php. This won't work on every admin page: not all of them include admin.php, and others have checks for "rogue" action query variables, and give you a "Are you sure you want to do that?" warning (due to missing nonces?). For a plugin page admin.php does everything: it displays the header (unless the noheader query variable is in the URL), calls your page, and displays the footer (unless you called exit() in your code!). admin.php then calls exit() itself, so the admin_action_ hook is not even called there.

share|improve this answer
    
@Jan Fabry: You're a bit mistaken there (i studied admin.php up and down). It includes everything (admin-header.php, the pages themselves, and admin-footer.php) within that single file. And after it's done all that comes that call. So it is the last thing executed. Thanks for the pointers, though, i'll have a look. –  wyrfel Feb 28 '11 at 17:59
    
@wyrfel: I know, I just finished a major edit of my answer. It now includes how Akismet does it. The trick is to not use your plugin page as the form action handler. When you are on a plugin page, admin.php even calls exit before this action is reached, so it is not called at all. –  Jan Fabry Feb 28 '11 at 18:35
    
@Jan Fabry: The noheader qv is a good hint, too. Thanks for digging that up. –  wyrfel Feb 28 '11 at 18:48
    
@wyrfel: OK, the fact that admin.php gets included in many (but not all) admin pages put me on the wrong track. This hook only works when you call admin.php directly. I have updated this answer, and now I know enough to answer your related question! –  Jan Fabry Feb 28 '11 at 19:31
    
@Jan Fabry: Lol...i think that should be flagged as a duplicate, now. ;-) Thanks for all that. –  wyrfel Feb 28 '11 at 19:33
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.