14

Update 2016-01-21

All current testing on my end is being done on fresh installs of 4.4.1 with the following settings: Plain permalinks Twentysixteen Theme No plugins activated

If the post only has 1 page (ie <!--nextpage--> doesn't appear in the post) then the extra pages are appended successfully (even if you append multiple extra pages¹).

Welcome to WordPress. This is your first post. Edit or delete it, then start writing!

If the post has 2+ pages then the extra pages 404 and canonical redirect to page 1 of the post.

Welcome to WordPress. This is your first post. Edit or delete it, then start writing!

<!--nextpage-->

This is page 2

In the second case $wp_query->queried_object is empty once you hit the extra pages. You'll need to disable the canonical redirect to see this remove_filter('template_redirect', 'redirect_canonical');

Both of the following core fixes have been tried, separately and together, with no change in behavior: https://core.trac.wordpress.org/ticket/35344#comment:16

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

For ease of use this is the code I'm currently testing with:

add_action('template_redirect', 'custom_content_one');
function custom_content_one() {
    global $post;
    $content = "\n<!--nextpage-->\nThis is the extra page v1";
    $post->post_content .= $content;
}

add_filter('content_pagination', 'custom_content_two', 10, 2);
function custom_content_two($pages, $post) {
    if ( in_the_loop() && 'post' === $post->post_type ) {
        $content = "This is the extra page v2";

        $pages[] = $content;
    }
    return $pages;
}

add_action('the_post', 'custom_content_three');
function custom_content_three() {
    global $multipage, $numpages, $pages;
    $content = "This is the extra page v3";

    $multipage = 1;
    $numpages++;
    $pages[] = $content;
}

¹This is the code I used to test multiple extra pages on a single page post

add_action('template_redirect', 'custom_content_one');
function custom_content_one() {
    global $post;
    $content = "\n<!--nextpage-->\nThis is the extra page v1-1\n<!--nextpage-->\nThis is the extra page v1-2\n<!--nextpage-->\nThis is the extra page v1-3";
    $post->post_content .= $content;
}

Original Question

Prior to 4.4 I was able to append an additional page to a mutlipage post with the following:

add_action('template_redirect', 'custom_content');
function custom_content() {
    global $post;
    $content = html_entity_decode(stripslashes(get_option('custom_content')));
    $post->post_content .= $content;
}

With get_option('custom_content') being something like:

<!--nextpage-->
Hello World

Since upgrading to 4.4 the code has not worked; navigating to the additional page triggers a 404 error and redirect_canonical sends them back to the permalink of the post. Disabling redirect_canonical allows me to view the extra page and the additonal content is there, but it still triggers a 404 error.

I've tried a number of workarounds, none of which resolve the 404 error, including:

add_action('the_post', 'custom_content');
function custom_content() {
    global $multipage, $numpages, $pages;
    $content = html_entity_decode(stripslashes(get_option('custom_content')));

    $multipage = 1; // ensure post is considered multipage: needed for single page posts
    $numpages++; // increment number of pages
    $pages[] = $content;
}

Also tried leveraging the new content_pagination filter that was added in 4.4:

add_filter('content_pagination', 'custom_content', 10, 2);
function custom_content($pages, $post) {
    $content = html_entity_decode(stripslashes(get_option('custom_content')));

    $pages[] = $content;
    return $pages;
}

At this point I'm out of ideas on how to restore this functionality and any assistance would be appreciated.

  • Alright so I have an update to this, unfortunately it's not a positive one. Apparently it works on the fresh install if the extrapage is page 2. However, if the extra page is page 3 (or higher, then it breaks). /wrist – Milamber Jan 19 '16 at 18:46
  • 1
    FOUND THE BUG!!!!! YEAH!!!!. Will update my answer soon – Pieter Goosen Jan 21 '16 at 17:37
8
+450

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

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

Interesting and quite relevant patches from the above trac ticket

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

  • 1
    Hopefully this is the issue for the OP, sounds very likely (not able to dig into this today ;-). – birgire Jan 19 '16 at 20:00
  • 1
    Tried the fix, also disabled permalinks; no change. – Milamber Jan 19 '16 at 20:30
  • 1
    I will look into this as soon as I have time. I do feel that the answer lies in the bug, it is just to track it down. Hope you find a solution soon – Pieter Goosen Jan 20 '16 at 4:30
  • 2
    Check my update, I have found the bug and also filed a bug report – Pieter Goosen Jan 21 '16 at 18:27
  • 2
    Confirmed that all 3 methods work once those lines are commented out :) closed ticket 35544 in favor of yours – Milamber Jan 21 '16 at 18:39
4

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'));
  • 1
    My mistake, the extra ) were left over from copying and pasting them out of the class they're in with my actual implementation. I've removed them from the op. Unfortunately the content_pagination version doesn't correct the 404 error either. – Milamber Dec 12 '15 at 19:51
  • 1
    There's also a missing ) in the $content line - see the update. @Milamber – birgire Dec 12 '15 at 20:05
  • 1
    Thanks, fixed. Any syntax errors you're seeing in the op code are from simplifying it for the post. – Milamber Dec 12 '15 at 20:44
  • 2
    ok, I've tested this on a vanilla WP 4.4 install with the Twenty Sixteen theme and it works as expected there, so I think it may be something else in your setup that's giving you the problem. @Milamber – birgire Dec 12 '15 at 21:06
  • 1
    Strange, I tried it in 2 different dev environments with Twentyfifteen and they both had the same result. I'll take another look at it, thanks @birgire – Milamber Dec 13 '15 at 0:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.