Make WordPress Core

Opened 5 years ago

Closed 12 months ago

Last modified 12 months ago

#45920 closed enhancement (fixed)

Twenty Nineteen: Add fragment ID to paginated links

Reported by: laurelfulford's profile laurelfulford Owned by: audrasjb's profile audrasjb
Milestone: 6.3 Priority: normal
Severity: normal Version: 5.0.2
Component: Bundled Theme Keywords: has-patch has-testing-info needs-testing
Focuses: Cc:

Description

Originally reported by @joyously in the Twenty Nineteen GitHub repo:

When the post/page has a featured image and is paginated, it shows the big image on each page.
That is fine, but we could use code such as this so that the page links go to #content.

/**
 * Add a fragment identifier (to the content) to paginated links.
 */
function twentynineteen_link_pages_link( $link, $i ) {
	if ( $i > 1 && preg_match( '/href="([^"]*)"/', $link, $matches ) ) {
		if ( false === strpos( $matches[1], '#' ) ) {
			$link = str_replace( $matches[1], $matches[1] . '#content', $link );
		}
	}
	return $link;
}
add_filter( 'wp_link_pages_link', 'twentynineteen_link_pages_link', 10, 2 );

Original ticket: https://github.com/WordPress/twentynineteen/issues/381

Attachments (1)

45920.diff (794 bytes) - added by mukesh27 5 years ago.
Patch tested for Plain, Day and name, Month and name, Numeric, Post name and Custom Structure permalink structures.

Download all attachments as: .zip

Change History (15)

#1 @mukesh27
5 years ago

  • Keywords has-patch added; needs-patch removed

@laurelfulford and @joyously above code is not working for Plain permalink structure. please try below code also attached patch for same in theme.

/**
 * Add a fragment identifier (to the content) to paginated links.
 */
function twentynineteen_link_pages_link( $link, $i ) {
	if ( $i > 1 && preg_match( '/href="([^"]*)"/', $link, $matches ) ) {
		$link = str_replace( $matches[1], $matches[1] . '#content', $link );
	}
	return $link;
}
add_filter( 'wp_link_pages_link', 'twentynineteen_link_pages_link', 10, 2 );

@mukesh27
5 years ago

Patch tested for Plain, Day and name, Month and name, Numeric, Post name and Custom Structure permalink structures.

#2 @mukesh27
5 years ago

  • Keywords needs-testing added

#3 @joyously
5 years ago

I am getting the same result (not working on Plain permalinks), but I don't see why.

Do you know why?

The case that check is for is if there is already some other filter(like a plugin) for the link adding a fragment ID. It shouldn't make it into an invalid link.

#4 @poena
14 months ago

  • Milestone changed from Awaiting Review to 6.3

I have tested the patch on WordPress 6.2, theme version 2.5.
I did two tests, one with the page break block and one with a classic block and manually adding

<p><!--nextpage--></p>

Targetting the #content works well for all permalinks.

#5 @alvitazwar052
13 months ago

  • Keywords has-testing-info added; needs-testing removed

Test Report

Environment

  • OS: macOS 12.4
  • Web Server: Nginx
  • PHP: 7.4.33
  • WordPress: 6.3-alpha-55505-src
  • Browser: Chrome
  • Theme: Twenty-nineteen

Screenshots

Pagination:

  1. https://prnt.sc/NjIM9JiNo3bi
  2. https://prnt.sc/RV--wuCvTNBL

Results

  • I've tested with block editor with Plain, Day and name, Month and name, Numeric, Post name, and Custom Structure permalink structures. Targeting the #content works well for all permalinks. Pagination is also working as expected

#6 @oglekler
12 months ago

I wonder if this needs Unit-tests as well. @mukesh27 what do you think?

#7 @audrasjb
12 months ago

I don't think we need unit tests here, as this function would only be used by this specific theme :)

#10 @mukesh27
12 months ago

@audrasjb i open PR added missing document for the function. Feel free to take a look.

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


12 months ago

#12 @oglekler
12 months ago

  • Keywords needs-testing added

Patch is ready for testing

#13 @audrasjb
12 months ago

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

In 56057:

Twenty Nineteen: Add fragment ID to paginated links.

This changeset adds the #content fragment identifier to paginated links so the screen scrolls down to the content section when clicking on pagination links
located on paginated pages.

Props laurelfulford, mukesh27, joyously, poena, alvitazwar052, oglekler, audrasjb.
Fixes #45920.

Note: See TracTickets for help on using tickets.