Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6804379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:27:30+00:00 2026-05-26T19:27:30+00:00

I want to make a different menu between user and guest, here the code

  • 0

I want to make a different menu between user and guest, here the code that i’m trying

<?php
  function my_module_menu() {
    if(user_is_logged_in()) {
       $items = array();
       $items['my_module/user1']=array(
         'title' => t('1. User menu 1'),
           'page callback' => 'test1',
           'access arguments' => array('access content'),
           'type' => MENU_NORMAL_ITEM,
       'expanded' => TRUE,
       );
       $items['my_module/user2']=array(
         'title' => t('2. User Menu 2'),
           'page callback' => 'test2',
           'access arguments' => array('access content'),
           'type' => MENU_NORMAL_ITEM,
       'expanded' => TRUE,
       );
   }
   else{
         $items = array();
       $items['my_module/guest1']=array(
         'title' => t('1. Guest menu 1'),
           'page callback' => '1test1',
           'access arguments' => array('access content'),
           'type' => MENU_NORMAL_ITEM,
       'expanded' => TRUE,
       );
       $items['my_module/user2']=array(
         'title' => t('2. Guest menu 2'),
           'page callback' => '1test2',
           'access arguments' => array('access content'),
           'type' => MENU_NORMAL_ITEM,
       'expanded' => TRUE,
       );
   }
   return $items;
?>

problem is, the menu view i get only user view (user is logged in) whether user is logged in or not. Is something wrong with my code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:27:31+00:00Added an answer on May 26, 2026 at 7:27 pm

    The problem is that hook_menu is called when the menu is rebuilt, not for every single page load (as this would be very bad for performance).

    The easiest way to do what you want is to use the access callback key instead of access arguments key in your menu items to make the on-the-fly decision about whether the user is logged in. As Drupal won’t show the menu items for any user for which that access callback function returns FALSE, you’ll get the effect you want:

    function mymodule_menu() {
      $items['my_module/user1']=array(
        'title' => t('1. User menu 1'),
        'page callback' => 'test1',
        'access callback' => 'user_is_logged_in',
        'type' => MENU_NORMAL_ITEM,
        'expanded' => TRUE,
      );
    
      $items['my_module/user2']=array(
        'title' => t('2. User Menu 2'),
        'page callback' => 'test2',
        'access callback' => 'user_is_logged_in',
        'type' => MENU_NORMAL_ITEM,
        'expanded' => TRUE,
      );
    
      $items['my_module/guest1']=array(
        'title' => t('1. Guest menu 1'),
        'page callback' => '1test1',
        'access callback' => 'user_is_anonymous',
        'type' => MENU_NORMAL_ITEM,
        'expanded' => TRUE,
      );
    
      $items['my_module/user2']=array(
        'title' => t('2. Guest menu 2'),
        'page callback' => '1test2',
        'access callback' => 'user_is_anonymous',
        'type' => MENU_NORMAL_ITEM,
        'expanded' => TRUE,
      );
    
       return $items;
    }
    

    All of these menu items will be added to the same menu, but only two will ever show at any one time for a user.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make Friendly URLs, but when I try my PHP code works
I want to make my application Blackberry in different languages. That mean I use
I want to make a menu, which contains 4 different LinearLayouts. Top one has
I want to make an etag that matches what Apache produces. How does apache
I want to make a table in SqlServer that will add, on insert, a
I want to make an entity that has an autogenerated primary key, but also
I want to make sure people can't type the name of a PHP script
I am trying to make a simple menu with a nice transition effect using
overflowians, I am trying to represent a 3 tiered Navigation menu that are inside
Howdy, I want to make a dynamically filled Menu in Windows Phone 7. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.