Adding an item for logged in users with WHMCS

With the hooks and menu system available in WHMCS v6, it’s entirely possible to have your users see a completely different menu item if they’re logged in, as opposed to if they aren’t. This quick blog post will guide you through adding an item for logged in users with WHMCS.

As always, if you need assistance, please, do contact us. If you’d like us to modify this for you, or put something similar together for you with our custom development experience? Again, contact us. We’re absolutely here to help you out!

The problem:

Sometimes you just want your logged in users to see a link to something , but not make it publicly available to everyone else. Even better, sometimes you want the opposite, for your users who aren’t logged in to see something , but not show it to your logged in users.

Or maybe, you just want your logged in users to be directed to a separate URL. Well, this hook will help you there, too?

The solution:

The following code will assis you with adding an item for logged in users with WHMCS. Save it as a php file in your whmcs/includes/hooks directory . Of course, you want to edit it as necessary

<?php
use WHMCS\View\Menu\Item as MenuItem;

add_hook('ClientAreaPrimaryNavbar', 1, function (MenuItem $primaryNavbar) {
/** @var \WHMCS\User\Client $client */
$client = Menu::context('client');
/*are we logged in?*/
if (!is_null($client)) {
$primaryNavbar->addChild('Menu Name')
->setUri('https://www.example.com/')
->setOrder(70);
}
/*are we logged out?*/
if (is_null($client)) { 
$primaryNavbar->addChild('Menu Name 2')
->setUri('https://www.example.com/')
->setOrder(70);
}
});

 

See? Easy as pie. Or cake, if you prefer…

So, how can I use this to redirect to different urls if the client is logged in or not?

I’m glad you asked that. See the ‘Menu Name’ and ‘Menu Name 2’ items? Change them to what you want them to be. Then, change the setUri link to be appropriate. The menu item will show up as appropriate.