Skip to main content
Home
Drupal life hacks

Main navigation

  • Drupal
  • React
  • WP
  • Contact
  • About
User account menu
  • Log in

Breadcrumb

  1. Home

Creating Submenu Items in WordPress and Drupal

By admin, 8 April, 2024

In WordPress, the function `add_submenu_page()` is used to add a submenu page to an existing top-level menu in the WordPress administration dashboard. This function allows developers to create custom administration pages and organize them under existing menu items.

Here's an example of how `add_submenu_page()` is used in WordPress:
```php
add_action('admin_menu', 'my_plugin_submenu');

function my_plugin_submenu() {
   add_submenu_page(
       'my_plugin_menu',     // Parent slug
       'Submenu Page',       // Page title
       'Submenu Page',       // Menu title
       'manage_options',     // Capability required
       'my_submenu_slug',    // Menu slug
       'my_submenu_callback' // Callback function
   );
}

function my_submenu_callback() {
   // Page content goes here
}
```

In Drupal, the concept of creating submenu links under a parent menu item is slightly different. Instead of using a specific function like `add_submenu_page()`, developers define menu links and their hierarchy in a `.links.menu.yml` file within their custom module.

Here's an example of how to define a submenu link in Drupal:

```yaml
mymodule.submenu:
 title: 'Submenu Page'
 parent: mymodule.main_menu
 route_name: mymodule.submenu_route
 description: 'Description of the submenu item'
 weight: 50
```

In this example, `mymodule.main_menu` is the parent menu item defined elsewhere in the module's `.links.menu.yml` file. The submenu link `mymodule.submenu` is defined with its title, parent, route name, description, and weight.

Overall, while both WordPress and Drupal allow developers to create submenu items, they have different approaches and syntax for achieving this functionality. In WordPress, it's done programmatically using `add_submenu_page()`, while in Drupal, it's configured using YAML files and defined routes.

Tags

  • #Drupal Planet

Comments

About text formats

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
Powered by Drupal