Make WordPress Core

Opened 9 years ago

Closed 4 years ago

Last modified 4 years ago

#31237 closed enhancement (fixed)

improve get_the_archive_title() with new filter

Reported by: ramiy's profile ramiy Owned by: ocean90's profile ocean90
Milestone: 5.5 Priority: normal
Severity: normal Version: 4.1
Component: Themes Keywords: has-patch has-dev-note
Focuses: template Cc:

Description

The new get_the_archive_title() function introduces in wordpress 4.1 is very handy but it's very basic.

In some themes we prefer to hide the page prefix (i.e. Category, Tag, ect.).

I was thinking of a boolean parameter/filter in the function to show/hide the prefix.

if true, use:

__( 'Category: %s' )

else, use:

__( '%s' )

Attachments (3)

31237.patch (2.9 KB) - added by ramiy 8 years ago.
31237.2.patch (2.9 KB) - added by ramiy 8 years ago.
31237.3.patch (2.9 KB) - added by ramiy 8 years ago.
phpDocs: by default the filter is set to true

Download all attachments as: .zip

Change History (20)

#1 follow-up: @philiparthurmoore
9 years ago

Why not just use the get_the_archive_title filter as it's already there and works perfectly well?

#2 in reply to: ↑ 1 @ramiy
9 years ago

With the current get_the_archive_title filter, you have to write a very long code to filter each and every page type to remove the prefix. It's not developer friendly.

And as a developer, I prefer to write my own function to replace get_the_archive_title() than using this filter. Writing my own function is faster and easier than filtering the current function.

On the other hand, a boolean filter is simpler, faster and developer friendly.


For example, using this simple snippet in your functions.php file:

// Remove archive title prefix
add_filter( 'archive_title_prefix', '__return_false' );

Theme authors can remove the title prefix, and another developer can create child-theme to add the prefix back (with return true). As i said, boolean filters are simpler and developers friendly.

#3 @BinaryMoon
9 years ago

Rather than add a filter I would like to see a span wrapped around the prefix. Then users can optionally hide the prefix with CSS, or I can hide the prefix in the theme.

I sell premium themes on wordpress.com and hiding the prefix is a very frequent request.

Last edited 9 years ago by BinaryMoon (previous) (diff)

#4 follow-up: @ramiy
9 years ago

@BinaryMoon, I like your css approach but I think a boolean filter is simpler and search-engine-friendly.

#5 in reply to: ↑ 4 ; follow-up: @BinaryMoon
8 years ago

Replying to ramiy:

@BinaryMoon, I like your css approach but I think a boolean filter is simpler and search-engine-friendly.

I'd disagree that introducing more code is simpler. I think a span is a very simple solution that means we can all get the functionality we want. It also means that all users will get the functionality added to their sites without a theme update.

My concern is that I have users who are happy with it as is, and a small handful of users who want it changed. So I don't want to remove it for everyone as that will mean I am changing their website. I also don't want to introduce yet another option in the theme. If a span is added then those who really don't want it can use css to hide it. Everyone else can continue as is.

#6 in reply to: ↑ 5 @ramiy
8 years ago

Replying to BinaryMoon:

I'd disagree that introducing more code is simpler. I think a span is a very simple solution that means we can all get the functionality we want. It also means that all users will get the functionality added to their sites without a theme update.

The css approach is not semantic and not accessible. With css span you don't remove the prefix text you just hide it. The search engine still see the text, the blind person still hear the text with his device. I think we should totally remove it. We don't need to echo the prefix text if it's not being presented.

My concern is that I have users who are happy with it as is, and a small handful of users who want it changed. So I don't want to remove it for everyone as that will mean I am changing their website.

The new filter don't remove the prefix text for everyone, it only adds the ability not to show the prefix to those how chose to use the filter.

I also don't want to introduce yet another option in the theme. If a span is added then those who really don't want it can use css to hide it. Everyone else can continue as is.

No mater what approach we chose, you don't have to update your theme. users can remove the prefix text with css (in style.css) or with the the filter (in functions.php).

#7 @BinaryMoon
8 years ago

I'm coming at this from the point of view of a theme developer who sells themes. My users do not (generally) modify themes. Many of the themes I sell are on wordpress.com. As such - if I chose to use the toggle - it will affect everyone.

I chose the span entirely because it's not semantic. I didn't want to add semantic meaning unnecessarily. It doesn't have to be a span.

As for accessibility - I would argue that hiding the span with css is more accessible since it gives context to what is normally the page title. If you hide the prefix with css then users with screenreaders will more easily be able to tell what page they are on. I assume this is the reason for the prefix in the first place. Not printing it to the page at all removes this context.

Note - I don't use screen readers so don't know if there's other ways of getting the same info :)

How would existing users be able to hide the prefix with css if there's no span (or other element) to target the prefix?

@ramiy
8 years ago

#8 @ramiy
8 years ago

  • Keywords has-patch added

The patch adds a filter that allows you to show/hide the archive page prefix.

By default it shows the prefix. for backwards-compatibility.

Use this filter to hide the prefix:

// Remove archive title prefix
add_filter( 'archive_title_prefix', '__return_false' );

@ramiy
8 years ago

This ticket was mentioned in Slack in #core by ramiy. View the logs.


8 years ago

@ramiy
8 years ago

phpDocs: by default the filter is set to true

#10 @swissspidy
7 years ago

  • Keywords dev-feedback added

Related: #38545.

This ticket was mentioned in Slack in #themereview by joyously. View the logs.


6 years ago

#12 @mfgmicha
6 years ago

Also related #42768

#13 @Confridin
5 years ago

I've just created a patch that might solve this issue in #42768

#14 @ocean90
4 years ago

#42768 was marked as a duplicate.

#15 @ocean90
4 years ago

  • Keywords dev-feedback removed
  • Milestone changed from Awaiting Review to 5.5

#16 @ocean90
4 years ago

  • Owner set to ocean90
  • Resolution set to fixed
  • Status changed from new to closed

In 48294:

Themes: Introduce get_the_archive_title_prefix filter for changing the prefix used for archive titles.

In get_the_archive_title() split the internal $title variable into $title and $prefix. By using the new get_the_archive_title_prefix filter the prefix can now wrapped with custom elements or removed completely by using

add_filter( 'get_the_archive_title_prefix', '__return_empty_string' );

Also, wrap the title part with a span element and pass the original title and prefix to the existing get_the_archive_title filter, allowing further customization to the archive titles.

Props Kaira, milindmore22, shireling, grapplerulrich, audrasjb, desrosj, Confridin, ramiy, ocean90.
Fixes #31237.
Fixes #38545.

Note: See TracTickets for help on using tickets.