Change page title in the Thematic theme with the thematic_doctitle filter

For a blog I use the Thematic Theme. It provides the basis for own so-called Child Themes and can be customized in many ways. In addition to the normal WordPress actions and filters Thematic provides its own Theme Action Hooks and Theme Filter.

Some of them are very well documented and there are examples on the net. But now I append a text to the page title (i.e. the text inside the <title> tags in <head>) of every single page of the blog. Although the function thematic_doctitle() is discussed on the page of the Theme Filters in detail in an example, I could not imagine that it has to be written so complicated and with so many lines of source code. Therefore, I again check the source code to find the lines where the function is defined.

The thematic_doctitle filter

The source code of the filter can be found in the files „wp-content/themes/thematic/library/extensions/header-extensions.php“ at line 26 (in Thematic version 0.9.6.2). The interesting part looks like this:

	// Filters should return an array
	$elements = apply_filters('thematic_doctitle', $elements);
	// But if they don't, it won't try to implode
	if(is_array($elements)) {
		$doctitle = implode(' ', $elements);
	}
	else {
		$doctitle = $elements;
	}
	$doctitle = "t" . "<title>" . $doctitle . "</title>" . "nn";
	echo $doctitle;

As I could find out, the return of a separate filter function may be either a string or an array. If it is an array, each element will be connected to a string using the implode() function. I have also analyzed the contents of the arrays. Depending on the page there is only one element in the array, or even more parts that are separated by a „Separator“ element.

Extending the title with a function

Basically, it was irrelevant how the array looks like to my purposes, I just wanted to add something behind the original title. So I wrote a simple function that extends the title using the filter:

function my_doctitle($doctitle) {
	return array_merge($doctitle, array('suffix' => '| example.com'));
}
add_filter('thematic_doctitle','my_doctitle');

This tiny function add an array with the key „suffix“ to the $doctitle array. Using the array_merge() it will merged to the original array and then returned directly.

Conclusion

So instead of copying almost the complete thematic_doctitle filter to attach only a small string afterward, it has paid off, to look at the source code. This enabled us to achieve the desired result in just four lines.

Since I use so many filters and action hooks in my functions.php file, it’s even more important to see whether it couldn’t be done a lot easier. Otherwise, at the end, I would have copied the half (or even more) of the filters source code used in the Thematic Theme into my functions.php, without really having to.

I hope that I could show you once again, the you should not be afraid to study the source code of a product you want to use or extend from time to time. Not only in this case has it saved me a lot of work.

Veröffentlicht von

Bernhard is a full time web developer who likes to write WordPress plugins in his free time and is an active member of the WP Meetups in Berlin and Potsdam.

1 Kommentar » Schreibe einen Kommentar

  1. Guter Tipp. Ich benutze jetzt zwar schon eine ganze Weile Thematic, aber auf diese einfache Lösung war ich bisher nicht gekommen.

    Vielen Dank, dass kann ich sicher beim nächsten Child Theme einsetzen.

Schreibe einen Kommentar

Pflichtfelder sind mit * markiert.