钩子文档

menu_order

💡 云策文档标注

概述

menu_order 过滤器用于自定义 WordPress 管理后台菜单项的显示顺序。必须先启用 custom_menu_order 过滤器才能使其生效。

关键要点

  • menu_order 过滤器接收一个有序数组参数 $menu_order,用于指定菜单项的顺序。
  • 使用前需通过 add_filter( 'custom_menu_order', '__return_true' ) 启用自定义菜单排序功能。
  • 未在数组中提及的菜单项将排在提及的菜单项之后,并保持其默认相对顺序。

代码示例

add_filter( 'custom_menu_order', '__return_true' );

add_filter( 'menu_order', 'custom_menu_order' );

function custom_menu_order() {
    return array( 'index.php', 'edit.php', 'edit-comments.php' );
}

📄 原文内容

Filters the order of administration menu items.

Description

A truthy value must first be passed to the ‘custom_menu_order’ filter for this filter to work. Use the following to enable custom menu ordering:

add_filter( 'custom_menu_order', '__return_true' );

Parameters

$menu_orderarray
An ordered array of menu items.

More Information

When filtering the order array, any menus that are not mentioned in the array will be sorted after ones that are mentioned. Unmentioned menus are sorted in their usual order, relative to other unmentioned menus.

Source

$menu_order = apply_filters( 'menu_order', $menu_order );

Changelog

Version Description
2.8.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Example Migrated from Codex:

    The example below orders menus with Dashboard, Posts, and Comments in the first menu group. The remaining menus follow in their usual order.

    add_filter( 'custom_menu_order', '__return_true' );
    
    add_filter( 'menu_order', 'custom_menu_order' );
    
    function custom_menu_order() {
    	return array( 'index.php', 'edit.php', 'edit-comments.php' );
    }