Jump to content

Nav Menus Hooks


Recommended Posts

Hi there,

I'm new to WHMCS and would like to get some guidelines how to setup a hook or any straight-forward direction how to edit the label of my "Knowledgebase" primary and secondary navigations into FAQs? (instead of Knowledgebase).

I would also like to add a new external link for the same navs as above which I will label as my official "Knowledgebase".

Modifications should result into:

1. WHMCS Knowledgebase edited label into FAQs
2. Added new Knowledgebase external link

Thank you for the further help.

Link to comment
Share on other sites

14 hours ago, Mauwiks said:

I'm new to WHMCS and would like to get some guidelines how to setup a hook or any straight-forward direction how to edit the label of my "Knowledgebase" primary and secondary navigations into FAQs? (instead of Knowledgebase).

there are two ways - either use a hook, or if you only want to change the label of a default navbar/sidebar, you can use a Language Override... though you'd have to add an override for each language that you want to change the label for.

so if wanting to use an override..

$_LANG['knowledgebasetitle'] = "Brian's FAQs";

hwiFAax.png

WrH5Ctn.png

15 hours ago, Mauwiks said:

I would also like to add a new external link for the same navs as above which I will label as my official "Knowledgebase".

you need to decide where that link is going to be (navbar and/or sidebar), and whether the user needs to be logged in or not to see the new link - the answers to those questions will determine the required code.

the Navbar Cheatsheet documentation may be of help to you, but there are plenty of similar working navbar examples posted in the forums... if you can't figure it out, then you ask for specific help.

as an example, if you wanted to change the links on the existing knowledgebase navbar./sidebar links, then you could use the hook posted in the thread below...

 

 

Link to comment
Share on other sites

 

1 hour ago, brian! said:

there are two ways - either use a hook, or if you only want to change the label of a default navbar/sidebar, you can use a Language Override... though you'd have to add an override for each language that you want to change the label for.

so if wanting to use an override..


$_LANG['knowledgebasetitle'] = "Brian's FAQs";
2

Thank you very much for your response. Much appreciated.

I would prefer what is easier and cannot be overridden by an upgrade.

1 hour ago, brian! said:

you need to decide where that link is going to be (navbar and/or sidebar), and whether the user needs to be logged in or not to see the new link - the answers to those questions will determine the required code.

Both Navbar and sidebar, and I want everyone can have access to it whether they are logged in or not.

Please let me know if you could give me some coded sample

Link to comment
Share on other sites

21 hours ago, Mauwiks said:

I would prefer what is easier and cannot be overridden by an upgrade. 

neither using language overrides or hooks should get overwritten during an upgrade.

21 hours ago, Mauwiks said:

Both Navbar and sidebar, and I want everyone can have access to it whether they are logged in or not. 

Please let me know if you could give me some coded sample

the following hook should work...

<?php

use WHMCS\View\Menu\Item as MenuItem;

add_hook('ClientAreaPrimaryNavbar', 1, function (MenuItem $primaryNavbar)
{
	if (!is_null($primaryNavbar->getChild('Knowledgebase'))) {
		$primaryNavbar->getChild('Knowledgebase')->setLabel('FAQs');
		$primaryNavbar->addChild('kb2')->setLabel(Lang::trans('knowledgebasetitle'))->setURI('http://www.google.com')->setOrder(30);
	}

	if (!is_null($primaryNavbar->getChild('Support'))) {
		$primaryNavbar->getChild('Support')->getChild('Knowledgebase')->setLabel('FAQs');
		$primaryNavbar->getChild('Support')->addChild('kb2')->setLabel(Lang::trans('knowledgebasetitle'))->setURI('http://www.google.com')->setOrder(30);
	}
});

add_hook('ClientAreaSecondarySidebar', 1, function(MenuItem $secondarySidebar)
{
	if (!is_null($secondarySidebar->getChild('Support'))) {
		$secondarySidebar->getChild('Support')->getChild('Knowledgebase')->setLabel('FAQs');
		$secondarySidebar->getChild('Support')->addChild('kb2')->setLabel(Lang::trans('knowledgebasetitle'))->setURI('http://www.google.com')->setOrder(30)->setIcon('far fa-books');
	}
});

so screenshots below show what the navbar should look like for non-logged in users, logged in clients and the sidebar...

Co2GCBm.png

kJCeXQf.pngTcdwBky.png

Link to comment
Share on other sites

Everything worked well but I decided to do the language overrides (changed the Knowledgebase text to FAQs) and then add a new menu using hooks function but my site returns an error after that:

I followed this https://docs.whmcs.com/Client_Area_Navigation_Menus_Cheatsheet#Adding_a_Menu_Item

and then changed from your code to this:

----------

<?php

use WHMCS\View\Menu\Item as MenuItem;

add_hook('ClientAreaPrimaryNavbar', 1, function (MenuItem $primaryNavbar)
{
	if (!is_null($primaryNavbar->addChild('kb2')->setLabel(Lang::trans('knowledgebasetitle'))
->setURI('https://kindtechgroup.net/support')
->setOrder(30);
	}

	if (!is_null($primaryNavbar->getChild('Support'))) {
		$primaryNavbar->addChild('kb2')->setLabel(Lang::trans('knowledgebasetitle'))
->setURI('https://kindtechgroup.net/support')->setOrder(30);
		
		}
});

add_hook('ClientAreaSecondarySidebar', 1, function(MenuItem $secondarySidebar) 
{ if (!is_null($secondarySidebar->getChild('Support'))) { $secondarySidebar->getChild('Support')->addChild('kb2')
->setLabel(Lang::trans('knowledgebasetitle'))->setURI('https://kindtechgroup.net/support')
->setOrder(30)->setIcon('far fa-books'); } });

 

I know something's wrong with my code. Please enlighten me

 

 

 

Link to comment
Share on other sites

12 hours ago, Mauwiks said:

I know something's wrong with my code. Please enlighten me

this should work..

<?php

use WHMCS\View\Menu\Item as MenuItem;

add_hook('ClientAreaPrimaryNavbar', 1, function (MenuItem $primaryNavbar) {
	
	$client = Menu::context("client");
	if (!$client) {
		$primaryNavbar->addChild('kb2')->setLabel(Lang::trans('knowledgebasetitle'))->setURI('https://kindtechgroup.net/support')->setOrder(30);
	}

	if (!is_null($primaryNavbar->getChild('Support'))) {
		$primaryNavbar->getChild('Support')->addChild('kb2')->setLabel(Lang::trans('knowledgebasetitle'))->setURI('https://kindtechgroup.net/support')->setOrder(30);		
	}
});

add_hook('ClientAreaSecondarySidebar', 1, function(MenuItem $secondarySidebar) {
	if (!is_null($secondarySidebar->getChild('Support'))) {
		$secondarySidebar->getChild('Support')->addChild('kb2')->setLabel(Lang::trans('knowledgebasetitle'))->setURI('https://kindtechgroup.net/support')->setOrder(30)->setIcon('far fa-books');
	}
});

i'm assuming that you want non logged-in users see it on the navbar and logged-in clients to see it within the support dropdown on the navbar - if so the above should do it... if you want everyone to see the KB link in the same place on the navbar,whether logged in or not,

<?php

use WHMCS\View\Menu\Item as MenuItem;

add_hook('ClientAreaPrimaryNavbar', 1, function (MenuItem $primaryNavbar) {
	
	$primaryNavbar->addChild('kb2')->setLabel(Lang::trans('knowledgebasetitle'))->setURI('https://kindtechgroup.net/support')->setOrder(30);

});

add_hook('ClientAreaSecondarySidebar', 1, function(MenuItem $secondarySidebar) {
	if (!is_null($secondarySidebar->getChild('Support'))) {
		$secondarySidebar->getChild('Support')->addChild('kb2')->setLabel(Lang::trans('knowledgebasetitle'))->setURI('https://kindtechgroup.net/support')->setOrder(30)->setIcon('far fa-books');
	}
});
Link to comment
Share on other sites

I restored my installation because of the error.

And I decided to keep the "knowledgebase" term instead and just add a new Menu as follows:

PrimaryNavMenu and SecondaryNavMenu for (logged in users or not):

1. Add new "Support" Menu with an external link to https://kindtechgroup.net/support

2. Under the Support SubMenu, add a new Menu with the same link labeled as Support Center

3. Add "Support Center" under the Support (SideBar Header) but change the label of the (SideBar Header) to "Support Panel"

If this is too much, can you give me a hint for #1 instead? Thanks further. I've searched for a while but it doesn't fit my needs

Link to comment
Share on other sites

1. is basically going to be...

<?php

use WHMCS\View\Menu\Item as MenuItem;

add_hook('ClientAreaPrimaryNavbar', 1, function (MenuItem $primaryNavbar)
{
	if (is_null($primaryNavbar->getChild('Support Center'))) {
		$primaryNavbar->addChild('Support Center')->setOrder(30);
		$primaryNavbar->getChild('Support Center')->addChild('kb2')->setLabel(Lang::trans('knowledgebasetitle'))->setURI('http://www.google.com');
	}
});

YRFXqGK.png

3. is going to be a label change using either Language Overrides....

$_LANG['navsupport'] = "Support";

ot0fdfm.png

changing that string will rename the parent of both the sidebar and the loggedin support navbar parent menu.

or setLabel... the other hooks will show you how to do that.

Link to comment
Share on other sites

  • 3 weeks later...
17 minutes ago, Mauwiks said:

How do we add another external link like Shop to the NavMenu in the client area which should probably be here?

<?php

use WHMCS\View\Menu\Item as MenuItem;

add_hook('ClientAreaPrimaryNavbar', 1, function (MenuItem $primaryNavbar) {
	
	if (!is_null($primaryNavbar->getChild('Store'))) {
		$primaryNavbar->getChild('Store')->addChild('newproduct1')->setLabel('My New Product Link')->setURI('http://www.google.com')->setOrder(25);
	}
});

Y597XDV.png

i'm assuming that you didn't want to get rid of the divider that you highlighted - though if you did, then that's just uses a removeChild... the same as removing any other child item.

the value within ->setOrder determines where the new link will be added... if you made it 1, it would be before "Browse All"; if you made it 1000, it would likely be at the bottom of the dropdown... anywhere between just requires you to enter different values until you have it in the position that you want it.

Link to comment
Share on other sites

  • 2 years later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • 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