Jump to content

Add external link depending on language


Lek

Recommended Posts

Hi,

I used an hook to add an external link (a link that point out of my WHMCS website) in the navigation bar. It works. But as it is an external website the link is only one, it does not matter if you click from English or German. For example, if you click from the English version the link should point to the English version of a shoe website, if you click from the German version should point to another German website. So what I did is add some html in the lang file corresponding to the word to use in the link. And it is working. But it is not in line! I don't know why.

Here the hook syntax:

<?php
#adding Menu Item to primaryNavbar
use WHMCS\View\Menu\Item as MenuItem;
add_hook('ClientAreaPrimaryNavbar', 1, function (MenuItem $primaryNavbar)
{
   $primaryNavbar->addChild(
   'Homeextra',
           array(
               'name' => 'Homeextra',
               'label' => Lang::trans('extraHomePage2'),

               'order' => 00,
           )
       );


       }
);

Here the lang file syntax:

$_LANG['extraHomePage2'] = "<a href=https://google.com>External</a>";

I already try with

a href='https://google.com' and a href=\"https://google.com\"

But it is not in line:

not-in-line.jpg

Why?

Link to comment
Share on other sites

your code is almost right, change hook function to:

 

<?php
#adding Menu Item to primaryNavbar
use WHMCS\View\Menu\Item as MenuItem;
add_hook('ClientAreaPrimaryNavbar', 1, function (MenuItem $primaryNavbar)
{
   $primaryNavbar->addChild(
   'Homeextra',
           array(
               'name' => 'Homeextra',
               'label' => Lang::trans('extraHomePage2'),
               'uri' => Lang::trans('extraHomePage2Link'),
               'order' => 00,
           )
       );


       }
);

 

and in lang file add these lines:

$_LANG['extraHomePage2'] = "External";
$_LANG['extraHomePage2Link'] = "https://google.com";

Link to comment
Share on other sites

Perfect. It works. Thanks.

 

Then I did something more. I add a new link: Knowledgebase. So then I would like to remove the original Knowledgebase. There was no way with the instruction in http://docs.whmcs.com/Client_Area_Navigation_Menus_Cheatsheet#Hiding.2FRemoving_a_Menu_Item I just got a blank page. then I found another code from you https://forums.whmcs.com/showthread.php?104955-Using-hooks-to-change-sidebar-menus-works-on-some-pages-blank-screens-on-others and did work 50%: no blank page but the original Knowledgebase was still there but pointing to the original whmcs Knowledgebase link (I forgot to tell you I already modify in the past that link). So then I follow this instruction https://forum.whmcs.com/showthread.php?105623-Sidebar-Menu-issues-with-six-theme I adapted to the primary NavBar and I add a child that I could modify to link where I wanted. I named the child "Click here". I also remove the new link Knowledgebase I created some minutes before.

Now it is working and I think that click here can stay there forever...

Any idea about why I could not delete the original link Knowledgebase?

no-delete-link.jpg

Bye,

Lek

Link to comment
Share on other sites

you should be able to remove it without causing a blank screen - I can only assume there was an error in your hook code. :?:

 

although you can't have a child without a parent, so if you remove the parent, the child will go to - would you have preferred to have just changed the "Knowledgebase" label and link to the URL used in "Click here" ?

Link to comment
Share on other sites

Hi, yes I would

preferred to have just changed the "Knowledgebase" label and link to the URL used in "Click here"
but for some reason it did not work (probably I use a wrong code). So I remember I successfully add a child with a link I wanted and I use the same code. I modified the necessary things and it did work...
Link to comment
Share on other sites

https://forum.whmcs.com/showthread.php?119970-menu-link-Knowledgebase-to-point-elsewhee

 

the above hook will change the URL for the Knowledgebase link in the navbar for logged in and out users, and additionally in the sidebar if you want that too.

 

if you want to change the label, then you have two options - you can either add setLabel to each of the options in the code

 

    if (!is_null($primaryNavbar->getChild('Knowledgebase'))) {
       $primaryNavbar->getChild('Knowledgebase')
                       ->setLabel('My Label')
                       ->setURI('https://www.google.com');
   }

or if it's a default navbar/sidebar child and it uses a Language String (knowledgebase does) for a label, then you can just change the language string using Language Overrides.

 

http://docs.whmcs.com/Language_Overrides

 

$_LANG['knowledgebasetitle'] = "Knowledgebase";

Link to comment
Share on other sites

Hi,

I think I got confused by all code and link to other codes.

I use this:

<?php

<?php

use WHMCS\View\Menu\Item as MenuItem;

add_hook('ClientAreaPrimaryNavbar', 1, function (MenuItem $primaryNavbar)
{
if (!is_null($primaryNavbar->getChild('Knowledgebase'))) {
       $primaryNavbar->getChild('Knowledgebase', array(
                'label' => Lang::trans('knowledgebasetitle'),

                        'uri' => '../../hosting-domain-support.html',
               'order' => '100',
       ));
   }     

});

but I get a white screen.

I know the error is there and is the same syntax error I was doing last time before adding the extra "click here" solution.

I think I still don't know how to move from

->setURI('https://www.google.com');

to

  'uri' => 'https://google.com',

and relative label.

Can you point to my mistake?

Thank you, bye.

Lek

Link to comment
Share on other sites

Lek,

 

you have two "<?php" in your code which wouldn't help and would be the likely reason for the white screen.... but also the code is wrong. :)

 

you can't pass an array like that with getChild - a child can't be modified in that way... if you were using addChild, then you can.

 

to edit an existing child, this 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(Lang::trans('knowledgebasetitle'))
                       ->setURI('../../hosting-domain-support.html')
                       ->setOrder('100');
   }     
});

technically, you probably don't need to use setLabel as the child will already be using that language string - remember that it's not a new child, it's just an existing one that you've moved and changed the link of. :idea:

Link to comment
Share on other sites

Hi,

yes the double php was oversight.

But I still get blank page even with your new code.

I also try to remove the label and I also try to use " instead of ' but in all cases I got a blank screen.

I have been trying a lot but only with the child "click here" I can get it work. Also in the WHMCS instruction they indicate how to modify a child not the main link http://docs.whmcs.com/Client_Area_Navigation_Menus_Cheatsheet#Changing_where_a_Menu_Item_Points_To

Maybe should remove Knowledgebase link and create a new knowledgebase2?

Keep in touch.

Bye.

Lek

Link to comment
Share on other sites

Lek,

 

yes the double php was oversight.

But I still get blank page even with your new code.

I can't see why - the code was tested before posting and moved/changed the kb link... unless you have multiple hooks clashing with each other. :?:

 

I also try to remove the label and I also try to use " instead of ' but in all cases I got a blank screen.

not sure what you mean by 'instead of' - would likely need to see the code...

 

I have been trying a lot but only with the child "click here" I can get it work. Also in the WHMCS instruction they indicate how to modify a child not the main link http://docs.whmcs.com/Client_Area_Navigation_Menus_Cheatsheet#Changing_where_a_Menu_Item_Points_To

I tend to assume, with the navbar/sidebar hooks, that the code examples aren't necessarily accurate - i'd trust my code before theirs! :)

 

Maybe should remove Knowledgebase link and create a new knowledgebase2?

I don't think that's a good idea - what you're trying to do is basically simple, so you shouldn't need to use a hook to delete a parent and then re-add it.

 

in setup -> general settings -> other - tick the "Display Errors" checkbox and see what the error message is - it might tell you which hook and which line is causing the issue... once you know that, then it's easier to figure out the solution. :idea:

Link to comment
Share on other sites

Hi, good morning,

not sure what you mean by 'instead of' - would likely need to see the code...

for example:

->setLabel(Lang::trans('knowledgebasetitle'))

became:

->setLabel(Lang::trans("knowledgebasetitle"))

But anyway I think you right as I have another hook that create a new link with many children and I got the error with your instruction:

Fatal error: Call to a member function addChild() on null hook_add_link_all_products_child2.php on line 34

Here the hook:

<?php



use WHMCS\View\Menu\Item as MenuItem;



add_hook('ClientAreaPrimaryNavbar', 1, function (MenuItem $primaryNavbar)
{

   if (!is_null($primaryNavbar->getChild('All products'))) 

{

       $primaryNavbar->getChild('All products')

           ->addChild('Wise Hosting', array(

               'label' => Lang::trans('AllHosting'),

               'uri' => 'cart.php',

               'order' => '001',

           ));

   }

{

       $primaryNavbar->getChild('All products')

           ->addChild('Ready website', array(

               'label' => Lang::trans('ReadyWebsite'),

               'uri' => 'cart.php?gid=3',

               'order' => '002',

           ));

   }

   {

       $primaryNavbar->getChild('All products')

           ->addChild('Extra Email Services', array(

               'label' => Lang::trans('ExtraEmailServices'),

               'uri' => 'cart.php?gid=6',

               'order' => '003',

           ));

   }

 {

       $primaryNavbar->getChild('All products')

           ->addChild('SSL IP', array(

               'label' => Lang::trans('ExtraSSLIP'),

               'uri' => 'cart.php?gid=7',

               'order' => '004',

           ));

   }

{

       $primaryNavbar->getChild('All products')

           ->addChild('Domain Link', array(

               'label' => Lang::trans('ExtraDomainLink'),

               'uri' => 'cart.php?a=add&domain=register',

               'order' => '005',

           ));

   }

   {

       $primaryNavbar->getChild('All products')

           ->addChild('Bulk Domain Link', array(

               'label' => Lang::trans('ExtraDomainLinkBulk'),

               'uri' => 'domainchecker.php?search=bulk',

               'order' => '006',

           ));

   }



});

on line 34:

   ->addChild('Ready website', array(

I also create the one for the new link (below) and the above one for the children of the new link (the above one is the one of the error)

<?php
#adding Menu Item to primaryNavbar
use WHMCS\View\Menu\Item as MenuItem;
add_hook('ClientAreaPrimaryNavbar', 1, function (MenuItem $primaryNavbar)
{
   $primaryNavbar->addChild(
   'All products',
           array(
               'name' => 'AllProduct',
               'label' => Lang::trans('AllProductsAvailable'),
               'uri' => '#',
               'order' => 99,
           )
       );


       }
);

So probably something is wrong in here. But the image show how they work:

2_hooks_working_fine_but_creating_problem.png

Waiting for your comments.

Bye,

Lek

Link to comment
Share on other sites

Lek,

 

the { } around the addchild blocks are not required if it's not using an IF statement... plus the way you've used 'order' => '001' looks slightly weird, but should still work. :)

personally, I wouldn't use two hooks in this way - one to create the parent and another to add the children - it would be far simpler to just have one hook that does both...

 

<?php

#adding Menu Item to primaryNavbar
use WHMCS\View\Menu\Item as MenuItem;

add_hook('ClientAreaPrimaryNavbar', 1, function (MenuItem $primaryNavbar)
{
   $primaryNavbar->addChild('All products',array(
               'name' => 'AllProduct',
               'label' => Lang::trans('AllProductsAvailable'),
               'order' => 99,
               )
   );

   $primaryNavbar->getChild('All products')
               ->addChild('Wise Hosting', array(
               'label' => Lang::trans('AllHosting'),
               'uri' => 'cart.php',
               'order' => 1,
               )
   );

   $primaryNavbar->getChild('All products')
               ->addChild('Ready website', array(
               'label' => Lang::trans('ReadyWebsite'),
               'uri' => 'cart.php?gid=3',
               'order' => 2,
               )
   );

   $primaryNavbar->getChild('All products')
               ->addChild('Extra Email Services', array(
               'label' => Lang::trans('ExtraEmailServices'),
               'uri' => 'cart.php?gid=6',
               'order' => 3,
               )
   );

   $primaryNavbar->getChild('All products')
           ->addChild('SSL IP', array(
               'label' => Lang::trans('ExtraSSLIP'),
               'uri' => 'cart.php?gid=7',
               'order' => 4
               )
   );

   $primaryNavbar->getChild('All products')
           ->addChild('Domain Link', array(
               'label' => Lang::trans('ExtraDomainLink'),
               'uri' => 'cart.php?a=add&domain=register',
               'order' => 5,
               )
   );

   $primaryNavbar->getChild('All products')
           ->addChild('Bulk Domain Link', array(
               'label' => Lang::trans('ExtraDomainLinkBulk'),
               'uri' => 'domainchecker.php?search=bulk',
               'order' => 6,
               )
   );
}
); 

the above hook should create the parent (no need for a uri # link) and add the children to it.

 

when adding the children, you don't really need to check that the parent exists because the hook has created it... I suppose technically you should check (adding the !is null line of code before each getchild), but unless you've got another hook that removes/changes the parent in some way, errors shouldn't occur. :idea:

Link to comment
Share on other sites

Hi, I understand now.

Maybe in the "full of things to do" while set up WHMCS I just did not think too much when something was working. I think now is the correct way. Both the code to change the link in knowledgebase and in all offers are now working.

Thanks, I will have more questions. I will post soon, you will find around in the forum... :)

Bye,

Lek

Link to comment
Share on other sites

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