WordPress.org

Make WordPress Core

Opened 15 years ago

Closed 8 years ago

#4004 closed enhancement (invalid)

New filter for page title attribute in wp_list_pages output

Reported by: Dickie Owned by: ryan
Milestone: Priority: normal
Severity: normal Version: 2.1.3
Component: Posts, Post Types Keywords: needs-patch
Focuses: template Cc:

Description

While developing a new theme I realised that the default title attribute for wp_list_pages was to just to reuse the title of the page with no option to change it. Not exactly useful...

I therefore propose a new filter on the title attribute to allow a theme/plugin developer the option of changing the text to whatever they like.

The new filter takes the title attribute and the $page variable as well, so that any post/page related information can also be used... for example the post meta.

Attachments (3)

Filter change.patch (825 bytes) - added by Dickie 15 years ago.
Patch File for Filter Change
4004.diff (794 bytes) - added by rob1n 15 years ago.
A different, Page_Walker-based filter name.
Filter change-02.patch (1019 bytes) - added by Dickie 15 years ago.
Updated to included latest trunk filters.

Download all attachments as: .zip

Change History (24)

@Dickie
15 years ago

Patch File for Filter Change

#1 @Dickie
15 years ago

  • Keywords has-patch added

#2 @westi
15 years ago

  • Keywords commit added
  • Milestone changed from 2.1.3 to 2.2
  • Owner changed from anonymous to ryan
  • Summary changed from New filter for page title attribute to New filter for page title attribute in wp_list_pages output

+1

My only question is regarding the naming of the filter as it is being called from within the Page_Walker class - maybe it should reference that instead.

#3 @westi
15 years ago

  • Cc westi added

@rob1n
15 years ago

A different, Page_Walker-based filter name.

#4 @Dickie
15 years ago

I am interested to know why have you decided to remove the $page attribute added to the filter. Surely this is useful for plugin developers to get more info about this post, as just passing in the title gives no extra information.

#5 @westi
15 years ago

I agree with Dickie here. We need to pass $page through to the filter otherwise it is pretty useless as it has no other context.

If a plugin author wanted to use this filter they need some context to work with.

#6 follow-ups: @rob1n
15 years ago

In the trunk we now run it through the_title, both on the attribute and the page title.

#7 in reply to: ↑ 6 @westi
15 years ago

Replying to rob1n:

In the trunk we now run it through the_title, both on the attribute and the page title.

I don't think that helps much.

How can we tell the context of the call? i.e. attribute/content.

I agree adding the the_title filter to the title when used as content makes sense.

I feel we still need a new filter for when it is the title attribute - and we still need to pass the post object through for anyone that wants it here as they can't easily get the context in any other way as posts titles are not unique.

#8 @Dickie
15 years ago

Just to give some context to this.

For example... I set the page title to be "About" but when the visitor to my website hovers over the link in the menu bar for the About page I do not want it to say "About" but perhaps
"Visit my about page" or "Go to the page with information about me", this could either be hard coded in the plug-in (not good perhaps) or gleaned from the page meta data, or a mixture of both.
The point is, I need different text for both the Page name itself AND the page title attribute.
The way it was working in the supplied patch file was fine, the only issue I could see was the name of the filter itself (and to be honest I was not sure about that as it was not directly in wp_list_pages, but I went with it anyway as it seemed the most logical)

@Dickie
15 years ago

Updated to included latest trunk filters.

#9 in reply to: ↑ 6 @Dickie
15 years ago

Replying to rob1n:

In the trunk we now run it through the_title, both on the attribute and the page title.

In this patch I have also taken to opportunity to only run "the_title" filter once (as it was being run twice), then run the result of that filter through the attribute title filter "walker_page_title".

#10 @Dickie
15 years ago

I am not sure about my last patch now, if we really must pass the title Attribute through "the_title" filter, then the attribute filter should go second. (like this)

// As the post title is needed in two places, rather than process the filter twice, store it in $title
$title = apply_filters('the_title', $page->post_title);
// The title will also be run through another filter to determine the title attribute for the link
$output .= $indent . '<li class="' . $css_class . '"><a href="' . get_page_link($page->ID) . '" title="' . attribute_escape(apply_filters('walker_page_title',$title, $page)) . '">' . $title . '</a>';

But I cant help feeling that this is a bad idea... the anchor title attribute is a label for the link, and as such is likely to be completely different from the Title of the post itself. therefore I think that this solution is the better one, which is more similar to my first patch before the "the_title" filters were added

$output .= $indent . '<li class="' . $css_class . '"><a href="' . get_page_link($page->ID) . '" title="' . attribute_escape(apply_filters('walker_page_title',$page->post_title, $page)) . '">' . apply_filters('the_title', $page->post_title) . '</a>';


Also, I still feel strongly and agree with westi on this, that the $page var needs to be sent to the filter to give it some context.

#11 @foolswisdom
15 years ago

  • Milestone changed from 2.2 to 2.3

#12 @rob1n
15 years ago

  • Keywords commit removed

#13 @ryan
14 years ago

  • Milestone changed from 2.3 to 2.4

#14 @westi
14 years ago

Relates to #4893

#16 @ffemtcj
14 years ago

  • Milestone changed from 2.5 to 2.6

#17 @Denis-de-Bernardy
12 years ago

  • Keywords needs-patch added; has-patch removed
  • Milestone changed from 2.9 to Future Release

#18 @nacin
8 years ago

  • Component changed from Template to Posts, Post Types
  • Focuses template added

#19 @mikkelbreum
8 years ago

I would like to see this implemented. Without the filter, you need a custom walker to change the titles. What's needed to get this into core, a new patch?

#20 @mikkelbreum
8 years ago

  • Keywords 2nd-opinion added

Walker_PageDropdown::start_el already has a dedicated 'list_pages' filter for modifying only the title text: https://core.trac.wordpress.org/browser/tags/3.9.1/src/wp-includes/post-template.php#L1362

It seems inconsistent then, that the related Walker_Page::start_el only use the 'the_title' filter: https://core.trac.wordpress.org/browser/tags/3.9.1/src/wp-includes/post-template.php#L1282

Wouldn't it make sense to use the 'list_pages' filter for that one too?

#21 @obenland
8 years ago

  • Keywords filter wp_list_pages 2nd-opinion removed
  • Milestone Future Release deleted
  • Resolution set to invalid
  • Status changed from new to closed

This ticket is about the title attribute, which was removed in r18739.

The inconsistency of filter names between the Page walker and the page dropdown walker was introduced in r16446, to keep page dropdown consistent with category dropdown.

Post titles are filterable here, so there is no need to add a new filter. Changing the filter name will be impossible for back compat reasons obviously.

Note: See TracTickets for help on using tickets.