Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dlxsnippets/b119b7950eedc432020f52962436157c to your computer and use it in GitHub Desktop.
Save dlxsnippets/b119b7950eedc432020f52962436157c to your computer and use it in GitHub Desktop.
Register Multiple Admin Menu Locations in WordPress
<?php
/**
* Plugin Name: Fancy Tools: Multiple Admin Menu Locations
* Plugin URI: https://dlxplugins.com
* Description: A demo plugin showing how to register a menu in multiple locations.
* Version: 1.0.0
* Requires at least: 6.0
* Requires PHP: 7.3
* Author: DLX Plugins
* Author URI: https://dlxplugins.com
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*
* @package DLXPlugins\FancyTools
*/
namespace DLXPlugins\FancyTools;
define( 'FANCY_TOOLS_ADMIN_URL', admin_url( 'tools.php?page=fancy-plugin' ) );
/**
* Initialize the admin panel.
*/
add_action( 'admin_menu', __NAMESPACE__ . '\add_admin_menu' );
/**
* Add the admin menu.
*/
function add_admin_menu() {
add_management_page(
'Fancy Tools',
'Fancy Tools',
'manage_options',
'fancy-plugin',
__NAMESPACE__ . '\output_admin_page',
10
);
$hook = add_options_page(
'Fancy Tools',
'Fancy Tools',
'manage_options',
'fancy-plugin',
'__return_empty_string',
10
);
define( 'FANCY_TOOLS_OPTIONS_HOOK', $hook );
}
/**
* Output the admin page.
*/
function output_admin_page() {
?>
<div class="wrap">
<h1>Fancy Tools</h1>
<p>Here is some fancy content.</p>
</div>
<?php
}
// Add a redirect to the admin settings.
add_action( 'current_screen', __NAMESPACE__ . '\maybe_redirect_to_settings', 10 );
/**
* Redirect to settings if someone tries to access the options menu.
*/
function maybe_redirect_to_settings() {
if ( ! is_admin() ) {
return;
}
$screen = get_current_screen();
if ( defined( 'FANCY_TOOLS_OPTIONS_HOOK' ) && FANCY_TOOLS_OPTIONS_HOOK === $screen->id ) {
wp_safe_redirect( esc_url_raw( FANCY_TOOLS_ADMIN_URL ) );
exit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment