---
title: Bulk generating menu hierarchies
description: "If you have ever needed to bulk generate menu items in Drupal (I used this for theming some drop-downs), here is a snippet to accomplish it.  The third parameter is for recursively creating a hierarchy, so be careful with it – you can blow up the menu admin page in a hurry!
This code requires the devel module; the ambitious reader might consider adapting this to patch devel itself."
---

[Blog ](https://www.metaltoad.com/blog)

# [Bulk generating menu hierarchies](https://www.metaltoad.com/blog/bulk-generating-menu-hierarchies)

 Written by [Metal Toad Staff](https://www.metaltoad.com/blog/author/metal-toad-staff) | Oct 7, 2009 12:00:00 AM

 Filed under:

- [Drupal](https://www.metaltoad.com/help/drupal)

If you have ever needed to bulk generate menu items in Drupal (I used this for theming some drop-downs), here is a snippet to accomplish it. The third parameter is for recursively creating a hierarchy, so be careful with it – you can blow up the menu admin page in a hurry!

This code requires the devel module; the ambitious reader might consider adapting this to patch devel itself.

When you're finished, the generated items can be cleared quickly with `DELETE FROM {menu_links} WHERE link_path = 'http://example.com/';`

```
<?php
/**
 * Bulk generate menu links.
 * @param $count Number of links to generate.
 * @param $plid  Menu id  of the parent item.
 * @param $depth Recursion depth; 0 for a single set of menu items.
 */
function menugen($count, $plid = NULL, $depth = 0) {
  require_once(drupal_get_path('module', 'devel_generate') . '/devel_generate.inc');
  foreach (range(1,$count) as $x) {
    $item = array(
      'menu_name' => 'primary-links',
      'link_path' => 'http://example.com/',
      'link_title' => devel_create_greeking(2, TRUE),
    );
    if (! empty($plid)) {
      $item['plid'] = $plid;
    }
    $mlid = menu_link_save($item);
 
    if($depth != 0) {
      menugen($count, $mlid, $depth - 1);
    }
  }
}
```

[View full post](https://www.metaltoad.com/blog/bulk-generating-menu-hierarchies)

```json
{
  "@context" : "http://schema.org",
  "@type" : "BlogPosting",
  "author" : {
    "@type" : "Person",
    "name" : "Metal Toad Staff"
  },
  "dateModified" : "2023-12-06T23:34:29.001Z",
  "datePublished" : "2009-10-07T00:00:00Z",
  "headline" : "Bulk generating menu hierarchies",
  "image" : {
    "@type" : "ImageObject",
    "height" : 500,
    "url" : "https://6104926.fs1.hubspotusercontent-na1.net/hubfs/6104926/Imported_Blog_Media/Metal_Toad_blog_generic-Aug-11-2023-10-24-31-3278-PM.jpg",
    "width" : 870
  },
  "mainEntityOfPage" : "https://www.metaltoad.com/blog/bulk-generating-menu-hierarchies",
  "publisher" : {
    "@type" : "Organization",
    "logo" : {
      "@type" : "ImageObject",
      "height" : 60.0,
      "url" : "https://6104926.fs1.hubspotusercontent-na1.net/hubfs/6104926/social-suggested-images/6104926.fs1.hubspotusercontent-na1.nethub6104926hubfsMetal_Toad_Logo_orange_horizontal-50-1.jpg",
      "width" : 240.00002
    },
    "name" : "Blog"
  }
}
```