有趣的练习,一个认为它应该拥有一级菜单页面的一页插件是错误的,我对Jetpack使用了同样的技术。
要在选项页加载项中创建子页,read the documentation.
此菜单/子菜单交换的逻辑是:
添加多个ACF选项页面创建我们的菜单一级页面删除(隐藏)我们的插件页面添加(移动)我们的插件页面到ACF的步骤1和步骤2是使此示例通用的
要将其与任何其他插件一起使用,只需要步骤3和4,即调整段塞
要将其移动到默认WP菜单中,例如,使用add_theme_page
(外观)或add_options_page
(设置)。
<?php
/**
* Plugin Name: Swap Menus and Sub-menus
* Plugin URI: http://wordpress.stackexchange.com/q/95981/12615
* Author: brasofilo
* Author URI: http://wordpress.stackexchange.com/users/12615/brasofilo
* Licence: GPLv2 or later
*/
class Swap_Menus_WPSE_95981 {
function __construct()
{
add_action( \'plugins_loaded\', array( $this, \'modify_menus\' ) );
}
function modify_menus()
{
// 1) Add ACF Options pages
if( function_exists( "register_options_page" ) )
{
register_options_page( \'Header\' );
register_options_page( \'Footer\' );
}
// 2) Create this plugin page
add_action( \'admin_menu\', array( $this, \'add_aux_menu\' ) );
// 3) Remove (hide) this plugin page
add_action( \'admin_init\', array( $this, \'remove_aux_menu\' ) );
// 4) Move this plugin page into ACF Options page
// Priority here (9999) is to put the submenu at last postition
// If the priority is removed, the submenu is put at first position
add_action( \'admin_menu\', array( $this, \'add_aux_menu_again\'), 9999 );
}
function add_aux_menu()
{
add_menu_page(
\'Dummy Page First Level\',
\'Dummy Title\',
\'edit_posts\',
\'dummy-page-slug\',
array( $this, \'menu_page_content\' )
);
}
function menu_page_content()
{
?>
<div id="icon-post" class="icon32"></div>
<h2>Dummy Page</h2>
<p> Lorem ipsum</p>
<?php
}
function remove_aux_menu()
{
remove_menu_page( \'dummy-page-slug\' );
}
function add_aux_menu_again()
{
// To move into default pages, f.ex., use add_theme_page or add_options_page
add_submenu_page(
\'acf-options-header\', // <---- Destination menu slug
\'Dummy Page Second Level\',
\'Dummy Page Second Level\',
\'edit_posts\',
\'dummy-page-slug\',
array( $this, \'menu_page_content\' )
);
}
}
new Swap_Menus_WPSE_95981();