WordPress.org

Make WordPress Core

Opened 7 years ago

Last modified 3 months ago

#4004 new enhancement

New filter for page title attribute in wp_list_pages output

Reported by: Dickie Owned by: ryan
Milestone: Future Release Priority: normal
Severity: normal Version: 2.1.3
Component: Posts, Post Types Keywords: needs-patch filter wp_list_pages
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 7 years ago.
Patch File for Filter Change
4004.diff (794 bytes) - added by rob1n 7 years ago.
A different, Page_Walker-based filter name.
Filter change-02.patch (1019 bytes) - added by Dickie 7 years ago.
Updated to included latest trunk filters.

Download all attachments as: .zip

Change History (21)

Dickie7 years ago

Patch File for Filter Change

comment:1 Dickie7 years ago

  • Keywords has-patch added

comment:2 westi7 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.

comment:3 westi7 years ago

  • Cc westi added

rob1n7 years ago

A different, Page_Walker-based filter name.

comment:4 Dickie7 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.

comment:5 westi7 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.

comment:6 follow-ups: rob1n7 years ago

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

comment:7 in reply to: ↑ 6 westi7 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.

comment:8 Dickie7 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)

Dickie7 years ago

Updated to included latest trunk filters.

comment:9 in reply to: ↑ 6 Dickie7 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".

comment:10 Dickie7 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.

comment:11 foolswisdom7 years ago

  • Milestone changed from 2.2 to 2.3

comment:12 rob1n7 years ago

  • Keywords commit removed

comment:13 ryan7 years ago

  • Milestone changed from 2.3 to 2.4

comment:14 westi7 years ago

Relates to #4893

comment:16 ffemtcj6 years ago

  • Milestone changed from 2.5 to 2.6

comment:17 Denis-de-Bernardy5 years ago

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

comment:18 nacin3 months ago

  • Component changed from Template to Posts, Post Types
  • Focuses template added
Note: See TracTickets for help on using tickets.