Using custom post types in submenu + custom title

The (temporary?) solution I’ve settled for, is the following:

  1. Use the show_in_menu parameter of register_post_type
  2. Use javascript to display custom title after page load
  3. Solve the submenu page order (which automatically puts the custom post types at the top, which is not my intention), with the following helper function:

// Sort the children of a given toplevel menu item
// Keep original order intact as much as possible, while
//  enforcing the [before => after] rules given by $force_order
function help_sort_submenu_items($toplevel_slug, $force_order) {
  global $submenu;

  $done = array();
  $hold = array();
  $newmenu = array();

  foreach ($submenu[$toplevel_slug] as $i => $item) {
    $force_after = array_search($item[2], $force_order);
    if ($force_after !== false && empty($done[$force_after])) {
      $hold[$item[2]] = $item;
    } else {
      $newmenu []= $item;
      $done[$item[2]] = true;

      $just_added_id = $item[2];
      while ($just_added_id = $force_order[$just_added_id]) {
        $next = $hold[$just_added_id];
        $newmenu []= $next;
        $done[$just_added_id] = true;
      }
    }
  }

  $submenu[$toplevel_slug] = $newmenu;
}

Usage like so:

help_sort_submenu_items('my_plugin_toplevel_menu_item_slug', array(
  'submenu_page_with_this_slug_comes_before' => 'submenu_page_with_this_slug',
  'another' => 'rule'
));

Leave a Comment