TL;DR
Somewhere appropriate, e.g. ‘<theme>/functions.php‘, add the following code to change (here: remove completely) the prefix of tag pages:
add_filter('get_the_archive_title_prefix', function($prefix) {
if(is_tag()) {
return '';
}
return $prefix;
});
The problem
Discover: What does the user want?
While navigating through WordPress posts, I noticed that some pages have a prefix in front of the page’s title. For example, the overview page for a tag adds “Tag: “. It seems that these prefix cannot be configured neither in WordPress’ UI nor through a setting.
This is annoying because I want my page titles to look exactly like I configured them: without any prefix!
Define: What is the problem?
WordPress defines default ‘prefix’ for every page type. The definition of those can be found in ‘/wp-includes/general-template.php‘. The following code shows an excerpt of this file.
[...]
function get_the_archive_title() {
if ( is_category() ) {
$prefix = _x( 'Category:', 'category archive title prefix' );
} elseif ( is_tag() ) {
$prefix = _x( 'Tag:', 'tag archive title prefix' );
} elseif ( is_author() ) {
$prefix = _x( 'Author:', 'author archive title prefix' );
} elseif ( is_year() ) {
$prefix = _x( 'Year:', 'date archive title prefix' );
} elseif ( is_month() ) {
$prefix = _x( 'Month:', 'date archive title prefix' );
} elseif ( is_day() ) {
$prefix = _x( 'Day:', 'date archive title prefix' );
} elseif ( is_tax( 'post_format' ) ) {
[...]
As we can see, tags are prefixed with ‘Tag: ‘ by default.
Since version 5.5.0 however, the same function calls a filter to modify the prefix before returning it:
/**
* Filters the archive title prefix.
*
* @since 5.5.0
*
* @param string $prefix Archive title prefix.
*/
$prefix = apply_filters( 'get_the_archive_title_prefix', $prefix );
What we learned:
- WordPress is adding a prefix to certain page types
- This behaviour is not configurable WordPress’ UI
The solution
Develop: How can it be solved?
In order to change the prefix of any given page type, we could apply to following solutions:
- Modify the function in WordPress’ code
- Override the entire function through a plugin or theme
- Amend the function’s behaviour by adding a filter
Modifying WordPress’ code directly is not a good solution for various reasons:
- each update of WordPress will reset the changes and they have to be applied again
- most hosters do not allow to modify WordPress files for security and maintenance reasons
Overriding the entire function is similar to modifying the code directly. In addition:
- code will be duplicated
Which leaves us with amending the functions behaviour somehow. As we have learned, filters get applied in the function before returning the prefixed page title. Let’s try to use that.
Deliver: Step-by-step solution
We need to do 2 steps:
- hook into ‘get_the_archive_title_prefix’ – or similar – function
- override the prefix or final title
Add a filter
There are multiple filter to choose from:
- get_the_archive_title( $title, $original_title, $prefix )
- get_the_archive_title_prefix( $prefix )
The latter is the perfect fix if only the prefix shall be changed.
In order to add a handler to WordPress’ filter chain, the following code needs to be applied:
add_filter('<filter_name>', '<function_name>');
OR
add_filter('<filter_name>', function($arg1, $arg2, ...) {
[CODE]
});
Set prefix
// add handler to filter chain
add_filter(
// use the prefix filter
'get_the_archive_title_prefix',
// pass anonymous function
function($prefix) {
// change only tag pages
if(is_tag()) {
// clear it to get rid of "Tag:"
return '';
}
// leave the prefix intact for everything else
return $prefix;
}
);
This code can be added (almost) everywhere. The major place to add this to is the ‘<theme>/functions.php‘ or similar places.
Bonus tip
Instead of changing theme files directly, you can create a child theme. This is especially advisable if your theme comes from a third party. A child theme allows the original theme to get updates without overriding the custom changes.