Make WordPress Core

Opened 11 years ago

Last modified 25 hours ago

#35562 new defect (bug)

Single post pagination redirect back to page one with nextpage tag - WordPress 4.4.1

Reported by: pietergoosen Owned by:
Priority: normal Milestone: Future Release
Component: Canonical Version: 4.4
Severity: normal Keywords: needs-patch
Cc: Focuses:

Description

When a single post post content, $post->post_content, does not contain the <!--nextpage--> tag, and we append more pages to the single post through the content_pagination filter, pagination works as expected

Sample code to test

add_filter( 'content_pagination', function ( $pages )
{
    $pages[] = 'This is another page we want to append';
	
    return $pages;
});

However, when we add the <!--nextpage--> tag inside the post content ($post->post_content}}} and run the same sample code, any appended page simply redirects back to page one as a 404 header is set on appended pages.

I tracked this down to the following block of code (lines 623 - 628) inside the WP class (wp-includes/class-wp.php which seems to be introduced in either v4.4.0 or 4.4.1

// check for paged content that exceeds the max number of pages
$next = '<!--nextpage-->';
if ( $p && false !== strpos( $p->post_content, $next ) && ! empty( $this->query_vars['page'] ) ) {
	$page = trim( $this->query_vars['page'], '/' );
	$success = (int) $page <= ( substr_count( $p->post_content, $next ) + 1 );
}

Removing those lines fixes the issue.

What I recommend is that instead of simply checking for the <!nextpage--> and returning a 404 according to that, we must rather have a check that acts on the amount of pages we have counted immediately after the content_pagination filter and then return a 404 if the current page exceeds the amount of pages after the content_pagination filter

Attachments (1)

wporg-35562.php​ (321 bytes ) - added by tremidkhar 25 hours ago.
Test plugin: adds an extra page to every post with the content_pagination filter, to reproduce the issue.

Download all attachments as: .zip

Change History (8)

#1 @Milamber
11 years ago

#35544 was marked as a duplicate.

#2 @dd32
11 years ago

  • Component General → Canonical
  • Keywords needs-patch added
  • Milestone Awaiting Review → Future Release
  • Version 4.4.1 → 4.4

Caused by #11694 / [34494] because it wasn't updated to respect [35285] / #9911

@chriscct7 @DrewAPicture @wonderboymusic Any chance one of you could take a peak at making it work?

Last edited 11 years ago by dd32 (previous) (diff)

#3 follow-up: @birgire
11 years ago

The problem is that WP::handle_404() is executed long before setup_postdata() where the content_pagination filter currently lives.

I don't think we want to remove a restriction in WP::handle_404() that makes sure the current page number doesn't exceed the maximum number of content pages.

If we want to keep it, I suggest we try to move the content pagination logic (including the content_pagination filter) from the setup_postdata() up to the WP::handle_404() method.

The current content pagination isn't supposed to work for secondary loops anyways, as far as I understand it, even though the content_pagination filter is available to all loops through setup_postdata().

To support such a restriction for secondary loops, I think the corresponding post ID would need to be part of the GET request.

... or do you see any other way ?

Version 0, edited 11 years ago by birgire (next)

#4 in reply to: ↑ 3 @pietergoosen
11 years ago

Replying to birgire:

The problem is that WP::handle_404() is executed long before WP_Query::setup_postdata() where the content_pagination filter currently lives.

I don't think we want to remove a restriction in handle_404() that makes sure the current page number doesn't exceed the maximum number of content pages.

If we want to keep it, I suggest we try to move the content pagination logic (including the content_pagination filter) from the setup_postdata() up to the handle_404() method.

The current content pagination isn't supposed to work for secondary loops anyways, as far as I understand it, even though the content_pagination filter is available to all loops through setup_postdata().

To support such a restriction for secondary loops, I think the corresponding post ID would need to be part of the GET request.

... or do you see any other way ?

What bothers me about this whole issue, and why I suggested to remove that section of code for now is that we can successfully pages to any added pages (through the content_pagination filter) when a <!--nextpage--> tag is not set in the post content. Why can we page past page 1 and any other page after that when we do not have the <!--nextpage--> tag. Is that because there is nothing concretely set to test for a 404 when we page a single post. From what I make from all relevant code is that there is nothing that catches or set a 404 page when we page a single post. Paging a single post is definitely not something new. I really did not have time to test anything after the submission of this ticket due to a massive power failure, so I'm stuck on a mobile device for now.

I think that we will need to look at the whole process of paging a single post and when to set a 404. We should not just look at the <!--nextpage--> tag.

#5 @Milamber
10 years ago

Any update on a fix for this?

This ticket was mentioned in ​PR #16023 on ​WordPress/wordpress-develop by ​@tremidkhar.


25 hours ago
#6

  • Keywords has-patch added; needs-patch removed

Pages added through the content_pagination filter redirect to the first page, because WP::handle_404() counts the tags in the raw post content.

This PR uses the page count from WP_Query::generate_postdata(), which applies the filter.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Investigating the bug, drafting the fix and this description; the final change was reviewed and edited by human.

#7 @tremidkhar
25 hours ago

  • Keywords needs-patch added; has-patch removed

I can confirm this is still happening on trunk. WP::handle_404() counts the <!--nextpage--> tags in the raw post content to check whether the requested page exists, so pages added through the content_pagination filter are treated as out of range and redirected to the first page.

The patch uses the page count from WP_Query::generate_postdata(), which applies the content_pagination filter. Pages past the filtered count still redirect as before.

Testing instructions

  1. Install and activate the attached wporg-35562.php plugin.
  2. Publish a post with the content Page one.<!--nextpage-->Page two.
  3. View the post. It shows "Pages: 1 2 3".
  4. Click "3".

Before the patch: you are redirected to the first page ("Page one.").
After the patch: page 3 shows "This page was added by the content_pagination filter." /4/ still redirects to the post.

Note: the content_pagination filter now runs one extra time on requests for a paged post.

@tremidkhar
25 hours ago

Test plugin: adds an extra page to every post with the content_pagination filter, to reproduce the issue.

Note: See TracTickets for help on using tickets.