Jump to content

Hook to Change H1 Headings for Knowledgebase Articles and Categories


BakH

Recommended Posts

Hello guys,

I am trying to write a hook to replace the h1 heading (title) "Knowledgebase" on both knowledgebase categories and articles with the actual category/article headings. Currently, all articles and categories have "Knowledgebase" as the main heading then the category or article title as  sub-heading (h2). To me, this is not cool for both UX and SEO.

I am completely deleting the $kbarticle.title variable from the kb-article-title class section of the file knowledgebasearticle.tpl. I want $kbarticle.title to be loaded by hook  into the $title variable of pageheader.tpl so that it replaces the "Knowledgebase" on KB articles. Also, I want to load the category name $kbcat.name into the $title variable of pageheader.tpl on KB category pages. Also, I want category names to be used as page titles in the address bar by loading $kbcat.name into the $pagetitle variable of the header.tpl file.

Here is the hook I have written but it doesn't seem to work.

<?php

if (!defined("WHMCS"))
    die("This file cannot be accessed directly");
    

add_hook('ClientAreaPageKnowledgebase', 1, function($vars)
{
	if ($vars['kbarticle']['title'])
	{
	    $articleh1 = $var['kbarticle']['title']; //to preplace "Knowledgebase"
	    $output['title'] = $var['title'];
		$output['title'] = $articleh1;
		
		return $output;
	}
	else if ($vars['kbcat']['name'])
	{
	    $output['title'] = $vars['title'];
		$output['title'] = $vars['kbcat']['name'];
	    $output['pagetitle'] = $vars['pagetitle'];
		$output['pagetitle'] = $vars['kbcat']['name'];
		
		return $output;
	}
	else{
	}
});

Someone help.

Link to comment
Share on other sites

On 19/04/2019 at 12:38, BakH said:

I am trying to write a hook to replace the h1 heading (title) "Knowledgebase" on both knowledgebase categories and articles with the actual category/article headings. Currently, all articles and categories have "Knowledgebase" as the main heading then the category or article title as  sub-heading (h2). To me, this is not cool for both UX and SEO.

i'm tempted to suggest why don't you edit header.tpl and change...

<title>{if $kbarticle.title}{$kbarticle.title} - {/if}{$pagetitle} - {$companyname}</title>

to...

<title>{if $kbarticle.categoryname}{$kbarticle.categoryname} - {/if}{if $kbarticle.title}{$kbarticle.title} - {/if}{if $catname}{$catname} - {/if}{if !$templatefile|in_array:array('knowledgebasearticle','knowledgebasecat')}{$pagetitle} - {/if}{$companyname}</title>

sure, you'd have to update the template after ever update - but there's hardly ever more than six of them a year... less if you ignore the bugged major initial releases and wait for their first maintenance update to be released. 🙂

On 19/04/2019 at 12:38, BakH said:

I am completely deleting the $kbarticle.title variable from the kb-article-title class section of the file knowledgebasearticle.tpl. I want $kbarticle.title to be loaded by hook  into the $title variable of pageheader.tpl so that it replaces the "Knowledgebase" on KB articles. Also, I want to load the category name $kbcat.name into the $title variable of pageheader.tpl on KB category pages. Also, I want category names to be used as page titles in the address bar by loading $kbcat.name into the $pagetitle variable of the header.tpl file.

I think the above Smarty code should do all that. 🙂

On 27/04/2019 at 07:42, BakH said:

Guys anyone to come to my rescue here? @brian! any assistance from you on this?

if you desperately need it as a hook, i'm sure that it could be done that way. 🙂

Link to comment
Share on other sites

1 hour ago, brian! said:

<title>{if $kbarticle.categoryname}{$kbarticle.categoryname} - {/if}{if $kbarticle.title}{$kbarticle.title} - {/if}{if $catname}{$catname} - {/if}{if !$templatefile|in_array:array('knowledgebasearticle','knowledgebasecat')}{$pagetitle} - {/if}{$companyname}</title>

Thanks @brian!. This has played the trick. I just modified the code a little and it's now doing just what I was looking for. This is what I did just in case someone would like to do the same.

In header.tpl, I replaced the code 

<title>{if $kbarticle.title}{$kbarticle.title} - {/if}{$pagetitle} - {$companyname}</title>

with

<title>{if $kbarticle.title}{$kbarticle.title} - {elseif $kbarticle.categoryname}{$kbarticle.categoryname} - {elseif $catname}{$catname}- {/if}{if $templatefile eq 'knowledgebasecat'} {$LANG.knowledgebasetitle} {$LANG.knowledgebasecategories} - {/if}{if !$templatefile|in_array:array('knowledgebasearticle','knowledgebasecat')}{$pagetitle} - {/if}{$companyname}</title>

Then, in pageheader.tpl, I replaced the code

<h1>{$title}{if $desc} <small>{$desc}</small>{/if}</h1>

with

<h1>{if $kbarticle.title}{$kbarticle.title}{elseif $kbarticle.categoryname}{$kbarticle.categoryname}{elseif $catname}{$catname}{/if} {if !$templatefile|in_array:array('knowledgebasearticle','knowledgebasecat')}{$pagetitle} {/if}</h1>

Then in knowledgebasearticle.tpl, I deleted out the code

<h2>{$kbarticle.title}</h2>

And yes, this has served the purpose.

Link to comment
Share on other sites

1 hour ago, brian! said:

I think the above Smarty code should do all that. 🙂

Surely it did!

1 hour ago, brian! said:

if you desperately need it as a hook, i'm sure that it could be done that way. 🙂

It's what I had wanted but I couldn't figure out why it failed. Nonetheless, I had to go the Smarty way which worked.

1 hour ago, brian! said:

sure, you'd have to update the template after ever update - but there's hardly ever more than six of them a year... less if you ignore the bugged major initial releases and wait for their first maintenance update to be released. 🙂

It's what I wanted to avoid by using the hook, but oops, it's the only working option. So, I will have to go that way... at least for now!

Link to comment
Share on other sites

20 hours ago, BakH said:

It's what I wanted to avoid by using the hook, but oops, it's the only working option. So, I will have to go that way... at least for now!

quickly converting your hook code to a working hook would give you...

<?php

add_hook('ClientAreaPageKnowledgebase', 1, function($vars) 
{
	if ($vars['kbarticle']['title']) {
		return array ("title" => $vars['kbarticle']['title']);
	}
	elseif ($vars['catname']) {
		return array ("title" => $vars['catname'], "pagetitle" => $vars['catname']);
	}
});

now you should be able to take the logic from your updated Smarty to this hook... you know the variables now, and what you want to do with them - but if you use a hook, you may have to revert the templates back to their original state...

anyway, it's something for you to play with. 🙂

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