Dynamische Menüs in der WordPress admin bar

I wrote some lines of php code to generate an dynamic WordPress admin menu. You only have to configure an array and the script does everything for you.

Add this line to your functions.php within you child theme directory
require_once("/path_to_your/function_admin_menu_example.php");

Copy this code into function_admin_menu_example.php and adjust the obove path to link to this file. Then you only have to customize the $example_menu_array and you’re done.

< ?php

/******************************************************************************
example Menue in admin bar
******************************************************************************/
add_action( 'admin_bar_menu', 'example_add_nodes_and_groups_to_toolbar', 999 );

function example_add_nodes_and_groups_to_toolbar( $wp_admin_bar )
{
  // parent
  $parent_menu_id = 'example_main_menu';
  $args = array('id' => $parent_menu_id, 'title' => 'example'); 
  $wp_admin_bar->add_node($args);

  $example_menu_array = array
  (
    'mixed_menu_example' => array
    (
      'title'     => 'mixed example',
      'menues'   => array
                   (
                     'group_01' => array
                     (
                       'grp_node_01' => array
                       (
                         'title'   => 'Group Node 01',
                         'href'    => 'http://anhalter.net/blog'
                       )
                       ,'grp_node_02' => array
                       (
                         'title'   => 'Group Node 02',
                         'href'    => 'http://anhalter.net/blog'
                       )
                     )
                     ,'single_node_01' => array
                     (
                        'title'   => 'Single Node 01',
                        'href'    => 'http://anhalter.net/blog'
                     )
                   )
    )

    ,'nogroup_menu_example' => array
    (
      'title'     => 'no group example',
      'menues'   => array
                   (
                      'node_01' => array
                      (
                        'title'   => 'Node 01',
                        'href'    => 'http://anhalter.net/blog'
                      )
                      ,'node_02' => array
                      (
                        'title'   => 'Node 02',
                        'href'    => 'http://anhalter.net/blog'
                      )
                    )
    )
  );

  // create menu
  foreach ($example_menu_array as $menu_id => $menu_info)
  {
    $args = array
    (
      'id'     => $menu_id, 
      'title'  => $menu_info['title'],
      'parent' => $parent_menu_id
    );
    $wp_admin_bar->add_node($args); 
	
	// create menu nodes or groups and menu groups within the group
	foreach ($menu_info['menues'] as $submenu_id => $submenu_info)
	{
      // menu node or group?
	  if ($submenu_info['title']) // menu node
	  {
	    $args = array
        (
          'id'      => $submenu_id,
          'title'   => $submenu_info['title'],
          'href'    => $submenu_info['href'],
          'parent'  => $menu_id,
          'meta'    => array('target' => '_blank')
        );
        $wp_admin_bar->add_node($args);
	  }
      else //group
      {
        // create group
		$args = array
        (
          'id'      => $submenu_id,
          'parent'  => $menu_id
        );
        $wp_admin_bar->add_group($args);
		
        // create menu nodes
		foreach ($submenu_info as $grp_menu_id => $grp_menu_info)
        {
	      $args = array
          (
            'id'      => $grp_menu_id,
            'title'   => $grp_menu_info['title'],
            'href'    => $grp_menu_info['href'],
            'parent'  => $submenu_id,
            'meta'    => array('target' => '_blank')
          );
          $wp_admin_bar->add_node($args);
        }
      }
	}
  }
}

?>

Schreib einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *