Skip to content

Instantly share code, notes, and snippets.

@peterwilsoncc
Created July 31, 2023 23:58
Show Gist options
  • Save peterwilsoncc/cef6473cc0cb449fddd8438e4fa9e5b8 to your computer and use it in GitHub Desktop.
Save peterwilsoncc/cef6473cc0cb449fddd8438e4fa9e5b8 to your computer and use it in GitHub Desktop.
<?php
namespace PWCC\Nope\CustomAdminPage;
/**
* Register a custom menu page.
*/
function register_custom_menu_page(){
add_menu_page(
__( 'Custom Menu Title', 'custom-admin-page' ),
'custom menu',
'manage_options',
'custompage',
__NAMESPACE__ . '\\display_custom_menu_page',
null,
6
);
}
add_action( 'admin_menu', __NAMESPACE__ . '\\register_custom_menu_page' );
/**
* Process a custom action.
*
* This action is fired when the form on the custom menu page is submitted.
* It redirects back to the custom menu page.
*/
function process_custom_action(){
if( isset( $_POST['page'] ) && $_POST['page'] === 'custompage' ){
wp_safe_redirect( admin_url( 'admin.php?page=custompage' ) );
}
}
add_action( 'admin_action_my_custom_admin_action', __NAMESPACE__ . '\\process_custom_action' );
/**
* Display a custom menu page.
*
* This is a simple form that posts data to admin.php without a querystring parameter.
* It fires the action my_custom_admin_action which redirects back to this form.
*/
function display_custom_menu_page(){
?>
<form action="admin.php" method="post">
On the custom page.
<input type="hidden" name="page" value="custompage">
<input type="hidden" name="action" value="my_custom_admin_action">
<input type="submit" value="Submit">
</form>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment