Wordpress - Appending content with <!--nextpage--> broken in 4.4

UPDATE 21-01-2016 19:35 SA TIME - BUG FOUND!!!!! YEAH!!!!!!

I finally found the bug. As you stated in your last update, the failure only happens when $post_content has a <!--nextpage--> tag in the content. I tested it, and did confirm that any other page after the page after the <!--nextpage--> returns a 404 and then the page gets redirects back to the first page.

This is due to the following lines of code in the handle_404() method which was introduced in the WP class in WordPress 4.4

// 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 );
}

What this code does is, whenever the <!--nextpage--> tag is set in the post_content, it will return a 404 when any page is accessed which is appended after the content via the content_pagination filter. Because of a 404 being set, redirect_canonical() redirects any appended page back to the first page

I have filed a trac ticket about this issue which you can check out here

  • trac ticket #35562

At the time of writing there weren't any feedback yet, so make sure to regularly check out the status of the ticket

CURRENT SOLUTION - A/W TRAC TICKET FEEDBACK

For now, until we get any feedback and possible fixes in future releases, simply delete those lines from the WP class until further notice

WHAT TIME IS IT......IT'S DEBUGGING TIME!!!!!

I had time to fully test this. I took your code and tested it on:

  • My v4.3 local install

  • My v4.4.0 local install

  • My v4.4.1 local install

  • Complete new v4.4.1 local install with only the Hello World post and the Sample Page page

with my permalinks set to

  • default and

  • Post Name

Here is my test code to create 4 pages with inside my test post.

add_filter('content_pagination', 'custom_content', 10, 2);
function custom_content($pages, $post) {
    $pages_to_add = [
        'Hello World Page 2',
        'Hello World Page 3',
        'Hello World Page 4',
    ];

    foreach ( $pages_to_add as $page_to_add ){
        $pages[]  = html_entity_decode(
            stripslashes(
                $page_to_add
            )
        );
    }

    return $pages;
}

I also tested

add_filter('content_pagination', 'custom_content', 10, 2);
function custom_content($pages, $post) {
    $pages_to_add = [
        '<!--nextpage--> Hello World Page 2',
        '<!--nextpage--> Hello World Page 3',
        '<!--nextpage--> Hello World Page 4',
    ];

    foreach ( $pages_to_add as $page_to_add ){
        $pages[]  = html_entity_decode(
            stripslashes(
                $page_to_add
            )
        );
    }

    return $pages;
}

for good measure

On each and every install and permalink structure, all of your code work (except the content_pagination on v4.3 which is expected).

I also set Sample Page as a static front page, but that failed on page 2 as conformation to the bug as described in my ORIGINAL ANSWER and **EDIT

So the conclusion is that this has nothing to do with the bug in core or any other bug in core. From comments, something is unsetting the queried object on paged post pages, and that is something we need to debug. Unfortunately as this problem is now localized, I cannot give exact solutions.

DEBUGGING THE ISSUE

You need to use the following works flow to debug the issue

  • Get yourself a huge amount of high caffeine coffee with lots of sugar

  • Take a backup of you db

  • Download and install the following plugins (I have no affiliation to any plugin)

    • Debug Objects for normal debugging. Once installed and setup, repair all obvious bugs that might be highlighted by the plugin. Do not continue to the next main bullet point if you have obvious bugs. Fix them first

    • DB Manager which you will use to repair and cleanup your DB before you continue to the next bullet point

  • Clear all caches, browsers and plugins

  • Deactivate all plugins and clear all caches again for good measure. Because this issue looks like a redirection issue, I would probably first deactivate all plugins which might have something to do with redirection. It might be that one plugin is not yet compatible with v4.4. Check if the issue persist, if it does, continue to the next bullet point, else, lets look at this in more detail

    Start by deactivating all plugins, you can also start of by just deactivating the plugins that might be obvious for causing the issue. Properly test your install after activation of each and every plugin. The first plugin activated that caused the issue will be the culprit. In that case, contact the plugin author with the debugging details. Just make sure to clear your caches after each and every plugin activation just for good measure

  • If you reached this point, the previous bullet point did not solve your issue. The next step should be to switch to a bundled theme to eliminate your theme as the issue. Just again, clear caches.

  • If everything failed, you are left with two more options

    • Delete .htaccess and let WordPress create a new one

    • Reinstall WordPress

This should solve your issue. If it does not, you need to consider a bug in WordPress core which might be causing the issue.

I hope this helps in catching the bug

UPDATE

I should have actually linked to the fiollowing trac ticket which has seems to explain everything more in detail

  • trac ticket 35344

Interesting and quite relevant patches from the above trac ticket

  • https://core.trac.wordpress.org/ticket/35344#comment:16

  • https://core.trac.wordpress.org/ticket/35344#comment:34

I cannot concretely test anything as such at this moment, but you should work through the suggested patches and test them. What I can pick up is that the same code in redirect_canonical() which is responsible for the pagination of static front pages is also responsible for pagination on single pages.

ORIGINAL ANSWER

Single pages (like static front pages) uses get_query_var( 'page' ) to paginate. With WordPress 4.4 (and in v4.4.1), there came a bug which causes issues with pagination when using get_query_var( 'page' ) for pagination.

The current bug reports, like trac ticket # 35365, only mentions static front pages which have problems with pagination, but as the bug relates to get_query_var( 'page' ), I would think that this would also cause issues with single post pagination which also uses get_query_var( 'page' ).

You should try the patches as described in the trac tickets. If this work, you can apply the patch and wait for v4.4.2 which will have this bug fixed


Note that there's a syntax error for all those three examples you provided:

add_filter('content_pagination', 'custom_content'), 10, 2);

add_action('the_post', 'custom_content'));

add_action('template_redirect', 'custom_content'));

where an extra ) is added.

Replace these lines to:

add_filter( 'content_pagination', 'custom_content', 10, 2);

add_action( 'the_post', 'custom_content' );

add_action( 'template_redirect', 'custom_content' );

I would not recommend messing around with global objects in general, so I think your last example with the content_pagination filter is the way to go here.

You might also want to avoid appending empty pages with:

if( ! empty( $content ) )
    $pages[] = $content;

There's also a missing ) here:

$content = html_entity_decode(stripslashes(get_option('custom_content'));

Tags:

Posts

Nextpage