WordPress.org

Make WordPress Core

Opened 5 years ago

Last modified 3 years ago

#26667 new enhancement

Improve filter in single_term_title()

Reported by: clifgriffin Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 3.8
Component: Taxonomy Keywords: has-patch needs-refresh
Focuses: template Cc:

Description

There are some oddities to how taxonomy titles are handled in wp_title().

For categories and tags, the title is set to the current term title, which is desirable in my opinion:

	if ( is_category() || is_tag() ) {
		$title = single_term_title( '', false );
	}

But for all other taxonomies, the taxonomy label is injected into the title:

	if ( is_tax() ) {
		$term = get_queried_object();
		if ( $term ) {
			$tax = get_taxonomy( $term->taxonomy );
			$title = single_term_title( $tax->labels->name . $t_sep, false );
		}
	}

This seems oddly specific and not the typical WordPress way, in my opinion. It seems to assume a lot in terms of what users will want. Ideally I'd take this block completely out and simply add is_tax() to the first block.

Being a pragmatist though, I will assume no one will take me up on that particular solution. :)

This really gets messy when you get into get_term_title(). The filters provided in this function do not filter the result of the operations but rather the input, which is a bit curious given the normal usage of filters.

The code:

function single_term_title( $prefix = '', $display = true ) {
	$term = get_queried_object();

	if ( !$term )
		return;

	if ( is_category() )
		$term_name = apply_filters( 'single_cat_title', $term->name );
	elseif ( is_tag() )
		$term_name = apply_filters( 'single_tag_title', $term->name );
	elseif ( is_tax() )
		$term_name = apply_filters( 'single_term_title', $term->name );
	else
		return;

	if ( empty( $term_name ) )
		return;

	if ( $display )
		echo $prefix . $term_name;
	else
		return $prefix . $term_name;
}

So, my recommended solution is to simply add a filter here for the result which passes in the $prefix in case we would like to remove it.

Like so:

function single_term_title( $prefix = '', $display = true ) {
	$term = get_queried_object();

	if ( !$term )
		return;

	if ( is_category() )
		$term_name = apply_filters( 'single_cat_title', $term->name );
	elseif ( is_tag() )
		$term_name = apply_filters( 'single_tag_title', $term->name );
	elseif ( is_tax() )
		$term_name = apply_filters( 'single_term_title', $term->name );
	else
		return;

	if ( empty( $term_name ) )
		return;

	$result = apply_filters( 'single_term_title_result', $prefix . $term_name, $term_name, $prefix );

	if ( $display )
		echo $result;
	else
		return $result;
}

The attached patch accomplishes this.

However, I think more sweeping changes might be warranted here.

Clif

Attachments (2)

26667.diff (550 bytes) - added by clifgriffin 5 years ago.
26667b.diff (1.1 KB) - added by clifgriffin 4 years ago.
26667b.diff

Download all attachments as: .zip

Change History (6)

@clifgriffin
5 years ago

#1 @SergeyBiryukov
5 years ago

  • Component changed from General to Template
  • Type changed from defect (bug) to enhancement

#2 follow-up: @nacin
4 years ago

  • Component changed from Template to Taxonomy
  • Focuses template added
  • Milestone changed from Awaiting Review to Future Release

Ideally I'd take this block completely out and simply add is_tax() to the first block.

Hmm. I *think* I could go for this.

#3 in reply to: ↑ 2 @clifgriffin
4 years ago

Replying to nacin:

Ideally I'd take this block completely out and simply add is_tax() to the first block.

Hmm. I *think* I could go for this.

Woot! That would make me very happy.

Attaching a diff to implement it that way, in case we proceed down this path.

Leaving the new filter to single_term_title() in, as this is the only way anyone would ever undo a prefix via a filter. I'd like others thoughts on this though, because the result is certainly inelegant. Just don't think there's a better way to do it and still maintain the same functionality on the existing filters.

@clifgriffin
4 years ago

26667b.diff

#4 @chriscct7
3 years ago

  • Keywords has-patch needs-refresh added
Note: See TracTickets for help on using tickets.