排序管理员菜单项
3 个回答
- 投票数
-
- 2013-01-08
可以对全局
$submenu
进行排序.应用的排序将重置子数组
$submenu['options-general.php']
的键号,即:array 'options-general.php' => array 10 => array 0 => string 'General' 1 => string 'manage_options' 2 => string 'options-general.php' 15 => array 0 => string 'Writing' 1 => string 'manage_options' 2 => string 'options-writing.php' // etc
并变为:
array 'options-general.php' => array 0 => array 0 => string 'Discussion' 1 => string 'manage_options' 2 => string 'options-discussion.php' 1 => array 0 => string 'General' 1 => string 'manage_options' 2 => string 'options-general.php' // etc
在这里,我们正在考虑默认项目的长度等于六.在WordPress 3.5之前,有7个项目,隐私现在不见了,并嵌入了 Reading 标签.
在本地进行测试,不会产生任何意外的结果,并且可以正常工作.也许如果某个插件依靠
$submenu['options-general.php'][15]
定位自己或其他东西,则可能会发生错误.排序将分别应用于默认项目和其余项目.只需禁用第一个块的
usort
,您就可以得到所需的输出.add_action( 'admin_menu', 'sort_settings_menu_wpse_2331', 999 ); function sort_settings_menu_wpse_2331() { global $submenu; // Sort default items $default = array_slice( $submenu['options-general.php'], 0, 6, true ); usort( $default, 'sort_arra_asc_so_1597736' ); // Sort rest of items $length = count( $submenu['options-general.php'] ); $extra = array_slice( $submenu['options-general.php'], 6, $length, true ); usort( $extra, 'sort_arra_asc_so_1597736' ); // Apply $submenu['options-general.php'] = array_merge( $default, $extra ); } //http://stackoverflow.com/a/1597788/1287812 function sort_arra_asc_so_1597736( $item1, $item2 ) { if ($item1[0] == $item2[0]) return 0; return ( $item1[0] > $item2[0] ) ? 1 : -1; }
It can be done sorting the global
$submenu
.The sorting that's applied resets the key number of the sub-array
$submenu['options-general.php']
, which is:array 'options-general.php' => array 10 => array 0 => string 'General' 1 => string 'manage_options' 2 => string 'options-general.php' 15 => array 0 => string 'Writing' 1 => string 'manage_options' 2 => string 'options-writing.php' // etc
and becomes:
array 'options-general.php' => array 0 => array 0 => string 'Discussion' 1 => string 'manage_options' 2 => string 'options-discussion.php' 1 => array 0 => string 'General' 1 => string 'manage_options' 2 => string 'options-general.php' // etc
Here, we are considering the length of the default items equal to six. Prior to WordPress 3.5, there were 7 items, Privacy is now gone and embedded with the Reading tab.
Testing locally, this doesn't produce any unexpected result and works ok. Maybe if a plugin depended on
$submenu['options-general.php'][15]
to position itself or something else, bugs could happen.Sort is being applied separately for the default items and for the rest of them. Just disable the
usort
of the first block and you have your desired output.add_action( 'admin_menu', 'sort_settings_menu_wpse_2331', 999 ); function sort_settings_menu_wpse_2331() { global $submenu; // Sort default items $default = array_slice( $submenu['options-general.php'], 0, 6, true ); usort( $default, 'sort_arra_asc_so_1597736' ); // Sort rest of items $length = count( $submenu['options-general.php'] ); $extra = array_slice( $submenu['options-general.php'], 6, $length, true ); usort( $extra, 'sort_arra_asc_so_1597736' ); // Apply $submenu['options-general.php'] = array_merge( $default, $extra ); } //http://stackoverflow.com/a/1597788/1287812 function sort_arra_asc_so_1597736( $item1, $item2 ) { if ($item1[0] == $item2[0]) return 0; return ( $item1[0] > $item2[0] ) ? 1 : -1; }
-
- 2010-09-29
我一直在寻找相同的东西,并认为我会复制Mike提供的代码,在下面他声称仍然是alpha版本,因为只有他在使用它.
我已经对其进行了测试,它似乎对我来说效果很好.我确定有些用例尚未处理.
也许Mike会在这里发表评论,并提供有关如何使用他的班级实现目标的其他帮助.
您可以从以下位置下载文件以放入主题目录: https://gist.github.com/792b7aa5b695d1092520
以下内容显示了如何调用主题的functions.php文件中的函数:
<?php require_once('wp-admin-menu-classes.php'); add_action('admin_menu','my_admin_menu'); function my_admin_menu() { swap_admin_menu_sections('Pages','Posts'); // Swap location of Posts Section with Pages Section rename_admin_menu_section('Media','Photos & Video'); // Rename Media Section to "Photos & Video" delete_admin_menu_section('Links'); // Get rid of Links Section $movie_tags_item_array = get_admin_menu_item_array('Movies','Movie Tags'); // Save off the Movie Tags Menu update_admin_menu_section('Movies',array( // Rename two Movie Menu Items and Delete the Movie Tags Item array('rename-item','item'=>'Movies','new_title'=>'List Movies'), array('rename-item','item'=>'Add New','new_title'=>'Add Movie'), array('delete-item','item'=>'Movie Tags'), )); copy_admin_menu_item('Movies',array('Actors','Add New')); // Copy the 'Add New' over from Actors renamed_admin_menu_item('Movies','Add New','Add Actor'); // Rename copied Actor 'Add New' to 'Add Actor add_admin_menu_item('Movies',array( // (Another way to get a 'Add Actor' Link to a section.) 'title' => 'Alt Add Actor ', 'slug' => 'post-new.php?post_type=actor', ), array(// Add Back the Movie Tags at the end. 'where'=>'end' )); add_admin_menu_item('Movies',$movie_tags_item_array,array(// Add Back the Movie Tags at the end. 'where'=>'end' )); delete_admin_menu_section('Actors'); // Finally just get rid of the actors section }
此外,这些功能甚至都在考虑之中(作为基础)包含在WordPress 3.1中,因此,如果我们幸运的话,这些功能甚至可能成为标准功能!
I was looking for the same thing and figured I would copy code provide by Mike below which he claims is still very much in alpha since its only been him that has been using it.
I have tested it and it seems to work well for me. I'm sure there are use-cases they do not yet handle.
Maybe Mike will comment in here and provide additional help on how you can use his class to achieve your goals.
You can download the file to drop in your theme's directory here: https://gist.github.com/792b7aa5b695d1092520
What follows below shows how you might call the functions in your theme's functions.php file:
<?php require_once('wp-admin-menu-classes.php'); add_action('admin_menu','my_admin_menu'); function my_admin_menu() { swap_admin_menu_sections('Pages','Posts'); // Swap location of Posts Section with Pages Section rename_admin_menu_section('Media','Photos & Video'); // Rename Media Section to "Photos & Video" delete_admin_menu_section('Links'); // Get rid of Links Section $movie_tags_item_array = get_admin_menu_item_array('Movies','Movie Tags'); // Save off the Movie Tags Menu update_admin_menu_section('Movies',array( // Rename two Movie Menu Items and Delete the Movie Tags Item array('rename-item','item'=>'Movies','new_title'=>'List Movies'), array('rename-item','item'=>'Add New','new_title'=>'Add Movie'), array('delete-item','item'=>'Movie Tags'), )); copy_admin_menu_item('Movies',array('Actors','Add New')); // Copy the 'Add New' over from Actors renamed_admin_menu_item('Movies','Add New','Add Actor'); // Rename copied Actor 'Add New' to 'Add Actor add_admin_menu_item('Movies',array( // (Another way to get a 'Add Actor' Link to a section.) 'title' => 'Alt Add Actor ', 'slug' => 'post-new.php?post_type=actor', ), array(// Add Back the Movie Tags at the end. 'where'=>'end' )); add_admin_menu_item('Movies',$movie_tags_item_array,array(// Add Back the Movie Tags at the end. 'where'=>'end' )); delete_admin_menu_section('Actors'); // Finally just get rid of the actors section }
What's more, these functions are even under consideration (as a base) for inclusion in WordPress 3.1 so if we're lucky these might even become standard!
-
听起来像是很多代码,但是我看不到它在哪里对子条目进行自动排序.It sounds like a great bit of code, but I don't see where it automatically sorts sub-entries.
- 0
- 2010-09-30
- dgw
-
- 2017-09-07
以为这可能是另一个有用的选择.这是对全局$子菜单的简单使用.我使用admin_init钩子,因为admin_menu可能还有其他插件添加了子菜单项(在我的情况下确实如此,因此进行排序还为时过早).另外请注意usort中的匿名(lambda)函数需要PHP 5.3 +.
<?php add_action('admin_init','my_admin_init'); function my_admin_init() { global $submenu; $submenu_key = 'my_key'; // See note below on $submenu_key if ( array_key_exists( $submenu_key, $submenu ) ) { // alphabetize submenu items usort( $submenu[ $submenu_key ], function ( $a, $b ) { // format of a submenu item is [ 'My Item', 'read', 'manage-my-items', 'My Item' ] return strcasecmp( $a[0], $b[0] ); } ); } }
请注意, add_submenu_page ,与 add_menu_page .
Thought this could be useful as another option. Here is a simple usort on the global $submenu. I use admin_init hook, because admin_menu may still have other plugins adding submenu items (in my case it did, so that was too early to do sorting). Also note the anonymous (lambda) function in the usort requires PHP 5.3+.
<?php add_action('admin_init','my_admin_init'); function my_admin_init() { global $submenu; $submenu_key = 'my_key'; // See note below on $submenu_key if ( array_key_exists( $submenu_key, $submenu ) ) { // alphabetize submenu items usort( $submenu[ $submenu_key ], function ( $a, $b ) { // format of a submenu item is [ 'My Item', 'read', 'manage-my-items', 'My Item' ] return strcasecmp( $a[0], $b[0] ); } ); } }
Note that
$submenu_key
is$parent_slug
in add_submenu_page, which is the same as$menu_slug
in add_menu_page.-
美观小巧.$ submenu_key是什么意思?这就是WordPress在[add_submenu_page](https://developer.wordpress.org/reference/functions/add_submenu_page/)文档中所说的$parent slug吗?Nice and compact. What is `$submenu_key` representing? Is that what WordPress calls the `$parent slug` in the [add_submenu_page](https://developer.wordpress.org/reference/functions/add_submenu_page/) docs?
- 1
- 2017-09-08
- random_user_name
-
@cale_b很好的问题!您是正确的,在add_submenu_page文档中为`$parent_slug`,与在add_menu_page中为$$menu_slug相同.@cale_b Great question! You are correct, it is `$parent_slug` in add_submenu_page docs, which is the same as `$menu_slug` in add_menu_page.
- 0
- 2017-09-08
- ryanm
有关" ">更改"管理"菜单部分的顺序的相关说明?",我正在寻找一种方法,可以按字母顺序对WordPress管理区域每个子节中的条目进行排序.
当前,无论何时添加新插件,其条目都会出现在"设置"/"工具"/"插件"下的一个看似随机的位置,并且通常很难找到新菜单项. (我已经有很多插件,所以我的菜单很全.)
由于我会相当定期地添加和删除插件,所以我不需要连续进入菜单排序插件的设置页面并调整顺序.
很抱歉,这个问题很长;我只想清楚我要寻找的东西.
示例
代替:
我可以按字母顺序(或通过其他逻辑方法)对菜单进行排序吗,例如:
一种更好的排序方法是将核心条目保留在默认位置,并且仅对插件添加的项目进行排序: