Using WordPress ‘custom_menu_order’ PHP filter

The custom_menu_order WordPress PHP filter enables or disables custom ordering of the administration menu.

Usage

add_filter('custom_menu_order', 'my_custom_menu_order_function');
function my_custom_menu_order_function($custom) {
  // your custom code here
  return $custom;
}

Parameters

  • $custom (bool): Determines whether custom ordering is enabled. Default is false.

More information

See WordPress Developer Resources: custom_menu_order

Examples

Enable custom menu ordering

Enable custom menu ordering by returning true:

add_filter('custom_menu_order', 'enable_custom_menu_order');
function enable_custom_menu_order($custom) {
  return true;
}

Disable custom menu ordering

Disable custom menu ordering by returning false:

add_filter('custom_menu_order', 'disable_custom_menu_order');
function disable_custom_menu_order($custom) {
  return false;
}

Enable custom menu ordering for admins only

Enable custom menu ordering only for users with the manage_options capability (usually administrators):

add_filter('custom_menu_order', 'enable_custom_menu_order_for_admins');
function enable_custom_menu_order_for_admins($custom) {
  if (current_user_can('manage_options')) {
    return true;
  }
  return false;
}

Enable custom menu ordering based on a user role

Enable custom menu ordering for users with a specific role, in this case, ‘editor’:

add_filter('custom_menu_order', 'enable_custom_menu_order_for_editors');
function enable_custom_menu_order_for_editors($custom) {
  $user = wp_get_current_user();
  if (in_array('editor', $user->roles)) {
    return true;
  }
  return false;
}

Enable custom menu ordering based on a user ID

Enable custom menu ordering for a specific user with a user ID of 3:

add_filter('custom_menu_order', 'enable_custom_menu_order_for_user_id');
function enable_custom_menu_order_for_user_id($custom) {
  $user = wp_get_current_user();
  if ($user->ID === 3) {
    return true;
  }
  return false;
}