WordPress.org

Make WordPress Core

Opened 7 years ago

Closed 5 years ago

#4209 closed enhancement (fixed)

Add rel="prev" to previous_post_link() and rel="next" to next_post_link()

Reported by: FekketCantenel Owned by:
Milestone: 2.9 Priority: normal
Severity: normal Version:
Component: Template Keywords: has-patch
Focuses: Cc:

Description

Hi,

I'm wondering if you could code link-template.php to include a rel="next" in the next_post_link()-generated link, and do the same with its previous counterpart.

This could even possibly be an option in the function's arguments. See also, this page.

My main use for this would be Opera's 'Flip Forward' and 'Flip Back' mouse gestures, which can use rel="" to detect where to go.

Attachments (5)

4209.diff (875 bytes) - added by Nazgul 7 years ago.
4209-nextgen.diff (3.8 KB) - added by rob1n 7 years ago.
4209.1.diff (637 bytes) - added by vladimir_kolesnikov 5 years ago.
Patch #1 (plugins have to check if rel is already there)
4209.2.diff (887 bytes) - added by vladimir_kolesnikov 5 years ago.
Patch 2 (we check if rel is already there)
link-template.php.diff (601 bytes) - added by vladimir_kolesnikov 5 years ago.
Updated version of 4209.1

Download all attachments as: .zip

Change History (38)

comment:1 andy7 years ago

I dig it.

comment:2 rob1n7 years ago

  • Milestone changed from 2.4 to 2.3

Yeah, I like it too. +1

comment:3 rob1n7 years ago

  • Owner changed from anonymous to rob1n

Nazgul7 years ago

comment:4 Nazgul7 years ago

  • Keywords has-patch added

comment:5 FekketCantenel7 years ago

Um, actually, I am stupid. This is an attribute for <LINK>, not <A>. I think.

Either way, I applied the mod but it isn't working with my flip gestures. I'm wondering if there might have to be a separate '<link rel="next" href="whatever">' added after the <A> tag.

comment:6 FekketCantenel7 years ago

Sure enough, this code works in Opera 9.20.8767:

	$string = '<link rel="prev" href="'.get_permalink($post->ID).'" /><a href="'.get_permalink($post->ID).'">'; 
	$string = '<link rel="next" href="'.get_permalink($post->ID).'" /><a href="'.get_permalink($post->ID).'">'; 

comment:7 Nazgul7 years ago

According to *1 the rel="next" is valid for an <a> tag.

Adding a <link> tag after/before the <a> tag may work, but isn't allowed, because <link> tags are only allowed in the <head> section *2.

*1 http://www.w3.org/TR/html4/struct/links.html#adef-rel
*2 http://www.w3.org/TR/html4/struct/links.html#edef-LINK

comment:8 FekketCantenel7 years ago

I think it must be a support issue in Opera itself. I'll keep my <link> usage for my personal use (since I'm the only one who sees my WordPress), and I hope other people will benefit from this patch. Thanks for all the info.

comment:9 rob1n7 years ago

We could put the <link /> tags for next/prev into the <head> automatically on is_single().

comment:10 rob1n7 years ago

  • Status changed from new to assigned

comment:11 FekketCantenel7 years ago

Interesting idea. Would that have to be added in the template itself?

comment:12 rob1n7 years ago

We can just add it as an action to wp_head. I'll work on a patch that incorporates Nazgul's as well as the <link /> ones.

comment:13 rob1n7 years ago

  • Keywords has-patch removed

comment:14 rob1n7 years ago

  • Keywords has-patch added

comment:15 rob1n7 years ago

  • Keywords commit added

Patch fully functional.

comment:16 rob1n7 years ago

I tested it in Opera, works fine. At first I needed to tweak something, but after that it was smooth sailing.

comment:17 rob1n7 years ago

  • Keywords commit removed

Never mind. Found an issue.

rob1n7 years ago

comment:18 rob1n7 years ago

  • Keywords commit added

Fully functional. Tested in Opera. Works.

comment:19 rob1n7 years ago

  • Keywords commit removed
  • Status changed from assigned to new

comment:20 rob1n7 years ago

  • Owner rob1n deleted

comment:21 ryan7 years ago

  • Milestone changed from 2.3 to 2.4

comment:22 jatyap6 years ago

I believe you can place a "rel" attribute inside the anchor (<a>) tag...

Check this: http://webdesign.about.com/od/htmltags/p/blatrel.htm

So the patch should work.

comment:23 ffemtcj6 years ago

  • Milestone changed from 2.5 to 2.6

comment:24 mrmist5 years ago

  • Keywords needs-patch added; has-patch removed

If this is still desired I'd expect the patch would need a fresh look.

comment:25 follow-up: mtekk5 years ago

The filters "next_posts_link_attributes" and "previous_posts_link_attributes" make implementing this quite difficult to do with out breaking any plugins that may add rel attributes using them.

comment:26 in reply to: ↑ 25 vladimir_kolesnikov5 years ago

  • Cc vladimir@… added

Replying to mtekk:

The filters "next_posts_link_attributes" and "previous_posts_link_attributes" make implementing this quite difficult to do with out breaking any plugins that may add rel attributes using them.

IMHO, plugins should also check if the rel attributte has already been added in order not to add it again :-)

vladimir_kolesnikov5 years ago

Patch #1 (plugins have to check if rel is already there)

vladimir_kolesnikov5 years ago

Patch 2 (we check if rel is already there)

comment:27 follow-up: vladimir_kolesnikov5 years ago

  • Component changed from Administration to Template
  • Keywords has-patch added; needs-patch removed

Attached two patches.

The first one adds rel="prev/next" before xxx_posts_link_attributes is fired, thus the plugins have to check if rel is already there (IMHO they should do that anyway).

The second one lets xxx_posts_link_attributes run, then checks if rel= is already there; if not, it is added.

Personally I like the first one more, as it is simplier and plugins in theory can add their own <a> tags in xxx_posts_link_attributes, in which case rel will be added to too many places.

comment:28 in reply to: ↑ 27 mtekk5 years ago

Replying to vladimir_kolesnikov:

Attached two patches.

The first one adds rel="prev/next" before xxx_posts_link_attributes is fired, thus the plugins have to check if rel is already there (IMHO they should do that anyway).

The second one lets xxx_posts_link_attributes run, then checks if rel= is already there; if not, it is added.

Personally I like the first one more, as it is simplier and plugins in theory can add their own <a> tags in xxx_posts_link_attributes, in which case rel will be added to too many places.

Looks like the functions dealing with this have changed, and that "next_posts_link_attributes" and "previous_posts_link_attributes" are now gone.

+1 on the first patch.

When using "next_post_link"/"previous_post_link" plugin authors should be able to see what is in the rel="" attribute. The first patch should be all that is needed for it to work since no one should be adding in rel="" before the new filters are run.

In the codex we should show how to intelligently add rel="" attributes, or others using the new filters.

comment:29 Denis-de-Bernardy5 years ago

I'm getting a malformed patch error with 4209.2.diff. 4209.1.diff applies clean and seems to do the right thing. is it rel=prev or rel=previous that we want?

comment:30 follow-up: sivel5 years ago

See #10180 and #8703.

WordPress 2.8 added home, index, next, prev.

comment:31 in reply to: ↑ 30 vladimir_kolesnikov5 years ago

Replying to sivel:

WordPress 2.8 added home, index, next, prev.

Maybe, but not to adjacent_post_link().

vladimir_kolesnikov5 years ago

Updated version of 4209.1

comment:32 vladimir_kolesnikov5 years ago

Updated the patch.

comment:33 automattor5 years ago

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

(In [11866]) Add rel attribute to next/previous post links. Fixes #4209 props vladimir_kolesnikov

Note: See TracTickets for help on using tickets.