Creating a menu link programatically for certain roles

by user3718920   Last Updated May 24, 2017 18:07 PM

My goal is to create links directly to main profile and edit pages when using profile2. I couldn't find how to do this so I found some code that I think would let me make my own links...

$item = array(
    'link_path' => "users/{$GLOBALS['user']->name}",
    'link_title' => $GLOBALS['user']->name,
    'menu_name' => 'main-menu',
    'weight' => 48,
    'expanded' => 1
$item_id = menu_link_save($item);
$item = array(
    'link_path' => "user/{$GLOBALS['user']->uid}/edit/main",
    'link_title' => 'Edit',
    'menu_name' => 'main-menu',
    'plid' => $item_id;

In addition to "does this work?", the 2 things I need now are...

How do I make the links only visible to authenticated users?

What other things do I need to do to make this a module or run it in some other way?

Tags : hooks navigation

Related Questions

HOOK_menu_alter access callback

Updated March 07, 2018 10:07 AM

Menu depth in drupal 8

Updated August 22, 2018 20:07 PM

When does hook_menu run?

Updated October 08, 2018 18:07 PM

Edit account menu item in user menu

Updated May 03, 2017 12:07 PM

Access to menu in drupal

Updated May 29, 2017 11:07 AM