I recently converted some old WordPress code over to using the admin-post.php
mechanism.
add_action( 'admin_post_myaction', function(){
//...my code to do stuff here...
//redirect back to the main page
$url = menu_page_url( 'my_original_menu_item', false );
wp_safe_redirect( $url );
} );
As you can see in the above simplified code sample, once I'm done handling my post, I grab the URL for my original menu item and redirect the user back to the page they came from.
However -- this isn't working. The menu_page_url
does not return a menu URL. When I investigated this, I discovered the global $_parent_pages;
variable that menu_page_url
relies on was empty. I was able to fix this my invoking the admin_menu hook
do_action( 'admin_menu', '' );
$url = menu_page_url( 'my_original_menu_item', false );
wp_safe_redirect( $url );
but this leaves me with a few questions.
Is the above (no loaded menus on an
admin-post.php
request) standard system behavior? Or is there something else that might be wrong with my system?What systems do and don't load during a request to
admin-post.php
? (possibly huge question, pointers appreciated)Is the above pattern (post to
admin-post.php
, redirect back usingmenu_page_url
considered an OK WordPress practice, or am I misusingadmin-post.php
?
Experienced programmer here -- I'm just trying to map WordPress concepts onto things I'm familiar with from web-MVC frameworks.