• Home
  • Get help
  • Ask a question
Last post 2 min ago
Posts last week 81
Average response time last week 4 hours 29 min
All time posts 67947
All time tickets 10505
All time avg. posts per day 20

Helpdesk is open from Monday through Friday CET

Please create an (free) account to post any question in the support area.
Please check the development versions area. Look at the changelog, maybe your specific problem has been resolved already!
All tickets are private and they cannot be viewed by anyone. We have made public only a few tickets that we found helpful, after removing private information from them.

#7229 – Override Page Title in PHP?

Posted in ‘sh404SEF’
This is a public ticket. Everybody will be able to see its contents. Do not include usernames, passwords or any other sensitive information.
Thursday, 04 June 2020 15:39 UTC
TheSDHotel
 Hello,
Is there any way I can manually override the Page Title in PHP for a com_content element?

For example, for a category page.

I try to do:

// Set meta title
$doc = JFactory::getDocument();
$doc->setTitle('test');

But it will still use the default category name in the page title (no custom page title is set for the URL in sh404sef)

Thanks!
Thursday, 04 June 2020 17:25 UTC
wb_weeblr
Hi

// Set meta title

$doc = JFactory::getDocument();

$doc->setTitle('test');



But it will still use the default category name in the page title (no custom page title is set for the URL in sh404sef)
Well, it depends on where you're putting this.

If in your template, this cannot work. It has to be inserted at the right moment in the Joomla order of processing of the page. For that you'll need a content plugin and you can do that setTitle() at onBeforeContent (I think).

As it happens, sh404SEF does not have a filter for that unfortunately.

Best regards

Yannick Gaultier
weeblr.com
@weeblr
 
Thursday, 04 June 2020 17:43 UTC
TheSDHotel
Yeah, I was trying in a template file.

I'm also alternatively trying with Regular Labs' ReReplacer, which can be set to be run at any of these three stages:

- Content Prepare
- After Dispatch
- After Render

But it also seems that any of these three stages is not a good moment either, as none of these three work unfortunately.
Thursday, 04 June 2020 18:24 UTC
wb_weeblr
Hi

Rereplacer cannot work either in that case because it replaces things in the content, but the page title is not in the content, it's in the head section of the document.
It could work at "AfterRender" but sh404SEF also does the last insert at AfterRender and I can only guess the sh404SEF system plugin is located after the ReReplacer one. Try setting ReReplacer after the sh404SEF system plugin.

Best regards

Yannick Gaultier
weeblr.com
@weeblr
 
Thursday, 04 June 2020 18:30 UTC
TheSDHotel
No luck unfortunately. I'll try to see if I can come up with another workaround :)
Monday, 08 June 2020 10:29 UTC
wb_weeblr

Hi

I just made a quick addition to the filters list in sh404SEF that should let you do what you need:

Added 3 filters, sh404sef_meta_page_title, sh404sef_meta_description and sh404sef_meta_robots to allow programmatically updating page title, meta description and meta robots on the fly



You can download the latest dev version with those changes from the download area.

Then in the /libraries/weeblr/sh404sef_functions.php file, append at the end:

/**
 * Filter the page title just before it's inserted into the page.
 *
 * @api
 * @package sh404SEF\filter\output
 * @var sh404sef_meta_page_title
 * @since 4.20.4
 *
 * @param string                $title The page title to be inserted
 * @param Sh404sefClassPageinfo $pageInfo An object describing the current page.
 *
 * @return string
 */
//ShlHook::add(
//	'sh404sef_meta_page_title',
//	function ($title, $pageInfo) {
//
//		// change title here
//
//		return $title;
//	}
//);

/**
 * Filter the meta description just before it's inserted into the page.
 *
 * @api
 * @package sh404SEF\filter\output
 * @var sh404sef_meta_description
 * @since 4.20.4
 *
 * @param string                $description The meta description to be inserted
 * @param Sh404sefClassPageinfo $pageInfo An object describing the current page.
 *
 * @return string
 */
//ShlHook::add(
//	'sh404sef_meta_description',
//	function ($description, $pageInfo) {
//
//		// change description here
//
//		return $description;
//	}
//);

/**
 * Filter the meta robots just before it's inserted into the page.
 *
 * @api
 * @package sh404SEF\filter\output
 * @var sh404sef_meta_robots
 * @since 4.20.4
 *
 * @param string                $robots The meta robots to be inserted
 * @param Sh404sefClassPageinfo $pageInfo An object describing the current page.
 *
 * @return string
 */
//ShlHook::add(
//	'sh404sef_meta_robots',
//	function ($robots, $pageInfo) {
//
//		// change meta robots here
//
//		return $robots;
//	}
//);



Now you have those 3 filters that let you modify page title, meta description and meta robots on the fly. To alter the title, you should uncomment the corresponding section above:

ShlHook::add(
	'sh404sef_meta_page_title',
	function ($title, $pageInfo) {

		// change title here

		return $title;
	}
);



Let me know how it goes.

Best regards

Yannick Gaultier
weeblr.com
@weeblr

 
Monday, 08 June 2020 11:08 UTC
TheSDHotel
Hey there, thanks!

One thing I don't understand though, how do I only execute this code in a certain file where I want the override to happen?

Do I just use this code in my custom file?

ShlHook::add(
	'sh404sef_meta_page_title',
	function ($title, $pageInfo) {

		// change title here

		return $title;
	}
);


And if so, what should be in /libraries/weeblr/sh404sef_functions.php?

Thanks!
Monday, 08 June 2020 11:39 UTC
wb_weeblr
Hi

Do I just use this code in my custom file?
Yes.

One thing I don't understand though, how do I only execute this code in a certain file where I want the override to happen?
This is not about a file but you have to decid based on the requested URL.

There are 2 basic ways to do that:

1 - the $pageInfo variable that's provided to you has a field that gives you the current SEF and the current non-SEF URL . So you can do something like this, based on the current SEF url:

ShlHook::add(
	'sh404sef_meta_page_title',
	function ($title, $pageInfo) {

		$title .= ' modified with filter';

		if(wbEndsWith($pageInfo->currentSefUrl, '/apples')) {
			$title = 'Some specific title';
		}
		
		return $title;
	}
);



2 - An alternative is to use the Joomla input API:

ShlHook::add(
	'sh404sef_meta_page_title',
	function ($title, $pageInfo) {

		$title .= ' modified with filter';

		$input = JFactory::getApplication()->input;
		if (
			$input->getCmd('option') == 'com_content'
			&&
			$input->getCmd('view') == 'article'
			&&
			$input->getInt('id') > 20
			&&
			$input->getInt('id') < 30
		)
		{
			$title = 'Some specific title';
		}

		return $title;
	}
);



In this example, you check that the component on the current page is com_content (ie an article or a category), that we are on a "view=article" page and if the "id" is between 20 and 30, then we change the title.

That's just to get you an idea, you'd need to adjust the criteria to change the title and how you change the title to your needs.

[EDIT] In both these examples, the title will always be modified by appending to it ' modified with filter'. Then depending on the condition checked, the title may be replaced entirely by 'Some specific title' (the first part does $title .= 'modified with filter' which adds to the title, while the second part does $title = 'Some specific title' which replaces the title with a new value.

Best regards

Yannick Gaultier
weeblr.com
@weeblr

 
Monday, 08 June 2020 11:51 UTC
TheSDHotel
Gotcha!

Do I still need the if conditions if my modified title will be dynamic (based on input vars) in the file where I call it from?

Or would it be simply something like this:

ShlHook::add(
	'sh404sef_meta_page_title',
	function ($title, $pageInfo) {

		$title = 'Bla bla ' . $inputvar1 . '  - ' . $inputvar2 . '  Some fixed text';
		return $title;
	}
);


However this is calling the ShlHook:: class. So do I need to require_once a file that includes this class, before I'm able to use this?
Monday, 08 June 2020 11:59 UTC
wb_weeblr
HI

Do I still need the if conditions if my modified title will be dynamic (based on input vars) in the file where I call it from?
Yes, they are needed to decide whether you are going to modify the title or not. That's a step you need to take. If the conditions are not satisfied, then you need to return the $title unmodified.

You can however do something like:

$id = $input->getInt('id')
if ($id > 20 and $id <30) {
$title = 'This is page #' . $id
}


This avoids repeating the calls to $input->getxxxx

However this is calling the ShlHook:: class. So do I need to require_once a file that includes this class, before I'm able to use this?
Nope, we stopped doing that a long time ago, all of sh404SEF classes and functions are autoloaded.

Best regards

Yannick Gaultier
weeblr.com
@weeblr
 
Monday, 08 June 2020 12:01 UTC
TheSDHotel
Awesome! I will try all of this when I get back home later :)
Monday, 08 June 2020 12:38 UTC
wb_weeblr

Cool, I will leave this ticket open in case you need to add something. It will automatically close in 2 weeks if no further comment is made.

Best regards

Yannick Gaultier
weeblr.com
@weeblr

 
Monday, 08 June 2020 15:14 UTC
TheSDHotel
Hey man, I'm testing it out and it looks like it's working!

Just one question. How do I pass it variables from outside the hook?

So if I have:

public static function myFunction()
{
	
	// Some code above this
	// ...
	// ....
	
	$page_heading = $promotion_text . ' - ' . $date_text;
	
	// Set sh404sef_meta_page_title
	ShlHook::add(
		'sh404sef_meta_page_title',
		function ($title, $pageInfo) {

			$title = $page_heading;
			
			return $title;
		}
	);

}	


But that doesn't work because $page_heading is not passed to the hook I assume. How can I do this?

Thanks again!
Monday, 08 June 2020 15:39 UTC
wb_weeblr
HI

Ideally, you do all the work from the inside of the function, that's the simplest.

You can also "inject" variables from the outside to the inside of the function with the keyword use as described here in the PHP doc.

function ($title, $pageInfo) use($page_heading ) {


To be honest, I have not tested that, it's a rare use case, you should nearly always be able to do everything inside the function you pass.

Best regards

Yannick Gaultier
weeblr.com
@weeblr
 
Monday, 08 June 2020 16:10 UTC
TheSDHotel
The reason why I ask is because I've already "done all the work" outside of the function, as the variables that make up the page_heading alre already used somewhere else.
So I would have to repeat the code inside the hook function.

BUT "use()" worked!

:)

It's the first time I see this type of functions (anonymours functions), I was indeed a bit confused about how it worked :)

Thanks!
Monday, 08 June 2020 16:59 UTC
wb_weeblr
Hi

It's the first time I see this type of functions (anonymours functions), I was indeed a bit confused about how it worked :)
It's pretty common, many cases you don't need to give a name to a function and code it separately. The alternative is to put the function in a variable and pass it to the shlHook function later like:

$f = function ($title, $pageInfo) {

			$title = $page_heading;
			
			return $title;
		};

ShlHook::add(
		'sh404sef_meta_page_title',
                $f
);




Glad use worked as you needed!

Best regards

Yannick Gaultier
weeblr.com
@weeblr
 
Tuesday, 16 June 2020 15:19 UTC
TheSDHotel
By the way, just out of curiosity, how do these filters work?

How does sh404sef take them into consideration even if they are executed at a later stage? :)
Tuesday, 16 June 2020 15:31 UTC
wb_weeblr
Hi

By the way, just out of curiosity, how do these filters work?



How does sh404sef take them into consideration even if they are executed at a later stage? :)
Filters are a thing from WordPress that I think is one of the major reasons why it took over the web.

When you write any code, at any moment, say to compute the page current title, just before using that result, I assign it a filter ('sh404sef_meta_page_title) and I can call a special function doing something like:

$pageTitle = filter( 'sh404sef_meta_page_title', $pageTitle);

What this says is "I have just computed the value of the page title, does any one wants to change it or do something with it before I use it myself?"

Now if any 3rd party has registered a filter handler (by calling ShlHook::add()) before I call the "filter" function, then they get a chance to modify the page title before sh404SEF uses it.

sh404SEF provides a file, /libraries/weeblr/sh404sef_functions.php, where 3rd-parties can put their filters handler and that file will be read by sh404SEF when starting up, making sure any filter handler you put in that file will be taken into account.

There are several filters in sh404SEF but I don't document them publicly at this stage because they can also be fairly dangerous if you're not familiar with PHP at least a little. A missing semicolon or something and your site is down with a PHP Fatal error...

Best regards

Yannick Gaultier
weeblr.com
@weeblr
 
Tuesday, 16 June 2020 15:50 UTC
TheSDHotel
Awesome, thanks for the clarification!

You can close this ticket for now :)

Best,
Andy
Tuesday, 16 June 2020 16:29 UTC
wb_weeblr
HI

Cool, Closing this ticket now, feel free to open a new one as needed. If you do so, please mention this ticket number in the new one.

Best regards

Yannick Gaultier
weeblr.com
@weeblr
 
This ticket is closed, therefore read-only. You can no longer reply to it. If you need to provide more information, please open a new ticket and mention this ticket's number.