WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 12 days ago

#31237 new enhancement

improve get_the_archive_title() with new filter

Reported by: ramiy Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.1
Component: Themes Keywords: has-patch dev-feedback
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 2 years ago.
31237.2.patch (2.9 KB) - added by ramiy 2 years ago.
31237.3.patch (2.9 KB) - added by ramiy 2 years ago.
phpDocs: by default the filter is set to true

Download all attachments as: .zip

Change History (14)

#1 follow-up: @philiparthurmoore
3 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
3 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
3 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 3 years ago by BinaryMoon (previous) (diff)

#4 follow-up: @ramiy
3 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
3 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
3 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
3 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
2 years ago

#8 @ramiy
2 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
2 years ago

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


2 years ago

@ramiy
2 years ago

phpDocs: by default the filter is set to true

#10 @swissspidy
16 months ago

  • Keywords dev-feedback added

Related: #38545.

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


12 days ago

Note: See TracTickets for help on using tickets.