Jump to content

Hiding Custom Menu Items only on Logged in Users - Using Hooks


kevinstan

Recommended Posts

Hello everyone. I recently made some modifications to my WHMCS primary nav menu, only to find out that once a user is logged in - there are other menu items that show up and for my theme its just too many. I now need to figure out how to  hide menu items only for logged in users. Specifically how to hide the logged in user menu items using hooks - but ONLY for logged in users. I know how to use hooks to hide the regular menu items, but how can I do it for menu items that are only shown to logged in users? Can anyone help me out? Thank you in advance!

Link to comment
Share on other sites

19 hours ago, kevinstan said:

I know how to use hooks to hide the regular menu items, but how can I do it for menu items that are only shown to logged in users?

there are two ways... the lazy way (which usually works) and the accurate correct way (which will always work!). :idea:

so i'll remove a navbar menu using the two methods...

1. the lazy way - this relies on the menuitemname being unique... some aren't, such as "Home" and you can't use this method for them, but some will be unique, e.g "Support" and only used for logged-in clients...

Uod23Xp.png

<?php

use WHMCS\View\Menu\Item as MenuItem;

add_hook('ClientAreaPrimaryNavbar', 1, function (MenuItem $primaryNavbar)
{
	if (!is_null($primaryNavbar->getChild('Support'))) {
            $primaryNavbar->removeChild('Support');
	}	
});

as long as you're checking the item exists before you try to remove it, then you will be fine... e.g the above hook wouldn't cause any issues for non-logged in users because the "Support" navbar menu won't exist.... unless of course, you've got another hook that is adding a support menu for non-logged in clients, but that way madness lies... so when creating your own menu items, don't use existing names!!

2. the correct way - let's remove "Open Ticket" for logged-in clients... in reality, only logged in clients see the link anyway, but you could do this for "Home" or any other logged-in link (unique or not).

<?php

use WHMCS\View\Menu\Item as MenuItem;

add_hook('ClientAreaPrimaryNavbar', 1, function (MenuItem $primaryNavbar)
{
	$client = Menu::context('client');
	if (!is_null($client) && !is_null($primaryNavbar->getChild('Open Ticket'))) {
            $primaryNavbar->removeChild('Open Ticket');
	}
});

if you get into the habit of using either method, do it the "correct" second way and you won't go far wrong - especially if you need to modify a menu item common to both and require it to do different things depending on if the client is logged in or not.

Link to comment
Share on other sites

Thank you for the reply Brian, but I have tried both methods and neither of them work. I created an additional 5 menu items for non logged in users, and once logged in, the additional menu items are too much and break my menu layout, so I am trying to remove some of them for logged in users to fix the primary nav menu. Not sure what I am missing, but I did try both of the above mentioned methods with no luck.

Link to comment
Share on other sites

I guess I should have clarified - I have created three menu items: Make Payment, Client Login, & Get Support. Once logged in the menu breaks from too many items so I want to hide those three menu items that I created using hooks when a user is logged in.

Link to comment
Share on other sites

19 hours ago, kevinstan said:

I have tried both methods and neither of them work.

they will if used correctly. :)

19 hours ago, kevinstan said:

I created an additional 5 menu items for non logged in users, and once logged in, the additional menu items are too much and break my menu layout, so I am trying to remove some of them for logged in users to fix the primary nav menu.

well if you have one hook creating them, and then another trying to remove them, you're asking for trouble.

18 hours ago, kevinstan said:

I guess I should have clarified - I have created three menu items: Make Payment, Client Login, & Get Support. Once logged in the menu breaks from too many items so I want to hide those three menu items that I created using hooks when a user is logged in.

you're thinking about this the wrong way - don't use multiple hooks to create/remove, create them however you want them and then you won't have to remove them!

the first step might be to only use one file for hooks in the primary navbar - that way, they're in all one place and you can see what you're doing... if there are multiple files all doing different things to the navbar, whether they work correctly will be down to the coding and the order in which they are called... e.g if the hook to remove them is called before the hook that creates them, then the removal hook is irrelevant.

the second thing is to determine when you want each of these navbar items to appear - logged-in, not logged-in, or both.

the following hook should be a good example for you to follow with regards to this...

<?php

use WHMCS\View\Menu\Item as MenuItem;

add_hook('ClientAreaPrimaryNavbar', 1, function (MenuItem $primaryNavbar)
{
	$client = Menu::context('client');
		
	if (is_null($client) && is_null($primaryNavbar->getChild('Make Payment'))) {
            $primaryNavbar->addChild('Make Payment', array(
				'label' => Lang::trans('makepayment'),
				'order'	=> 30,
				));
	}

	if (!is_null($client) && is_null($primaryNavbar->getChild('Get Support'))) {
            $primaryNavbar->addChild('Get Support', array(
				'label' => Lang::trans('getsupport'),
				'order'	=> 40,
				));
	}

	if (is_null($primaryNavbar->getChild('Client Login'))) {
            $primaryNavbar->addChild('Client Login', array(
				'label' => 'Client Login',
				'order'	=> 50,
				));
	}
});

so what this hook does is create 3 navbar items...

  1. "Make Payment" - visible only to non logged-in visitors.
  2. "Get Support" - visible only to logged-in clients.
  3. "Client Login" - visible to both (ignore that it makes no sense for already logged-in clients to see a login link - it's just an example!!!!)

using the above example, you should now be able to rewrite your hook so that the menu items are only visible when you want them to be... obviously, you can expand the addChild code to include links, icons etc, and you'll notice on the first two menu items that i've used language strings to show the label in the user's chosen language... if you don't want/need that, you can just hardcode them like I did in the third.

also, you can use additional hooks in the same file if you need to move/remove default navbar menu items.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated