WordPress.org

Make WordPress Core

Opened 5 years ago

Last modified 3 months ago

#26667 reopened enhancement

Improve filter in single_term_title()

Reported by: clifgriffin Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 3.8
Component: Taxonomy Keywords: has-patch, needs-refresh, bulk-reopened
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 5 years ago.
26667b.diff

Download all attachments as: .zip

Change History (8)

@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
5 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
5 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
5 years ago

26667b.diff

#4 @chriscct7
4 years ago

  • Keywords has-patch needs-refresh added

#5 @iseulde
5 months ago

  • Milestone Future Release deleted
  • Resolution set to wontfix
  • Status changed from new to closed

This ticket has not seen any activity in over *two* years, so I'm closing it as "wontfix".

The ticket may lack decisiveness, may have become irrelevant, or may not have gathered enough interest.

If you think this ticket does deserve some attention again, feel free to reopen.

For bugs, it would be great if you could provide updated steps to reproduce against the latest version of WordPress (5.0.2 at the time of writing). Remember images or a video can be superior to explain a problem. At the very least, quickly test again to make sure the bug still exists.

If it’s an enhancement or feature, some extra motivation may help.

Thank you for your contributions to WordPress! <3

#6 @JeffPaul
3 months ago

  • Keywords bulk-reopened added
  • Milestone set to Awaiting Review
  • Resolution wontfix deleted
  • Status changed from closed to reopened

A decision was made to reopen tickets that were closed in the bulk edit that this ticket was affected by. This ticket is being placed back into the Awaiting Review milestone so it can be individually evaluated and verified to determine if it is still relevant/valid or reproducible.

Note: See TracTickets for help on using tickets.