0

Does anyone know if it's possible to change wp_link_pages, I want to change the order of the pages Example : i have an Post consisting of 5 pages i want when you click on "Next page", it takes you to a random page of the five pages that we have in the article. without repeat

Any ideas/help greatly appreciated, S.

3
  • What do you mean by, you have a post containing 5 pages? how can a post contain pages exactly? May 24, 2021 at 9:49
  • 1
    @Buttered_Toast by adding <!--nextpage--> 5 time in a single post you get one post with 5 pages right ? May 24, 2021 at 10:37
  • Would it be possible to show an example? maybe add your code that you have done so far, even provide a link to what you are working on. If it's local, provide us with a way to replicate what you currently have May 24, 2021 at 10:47

1 Answer 1

2

Changing the wp_link_pages() function is an option, but randomizing either the page numbers or page links may be detrimental to the user's experience as he's expecting a normal page link structure while noticing random page numbers, e.g. in the post's URL.

However, there exists a content_pagination filter that lets you alter the post's $pages array. Simply shuffling this array would suffice, however, this does neither guarantee a static page order nor does it exclude the possibility of page repetition, as the shuffle is executed on each page load.

You could solve both these problems by using array_multisort() in combination with mt_srand(), as explained in this answer, and seed the random number generator with e.g. the post's ID—but then, every user is served the same (pseudo-random) page order, and one could argue what's the use of shuffling the pages at all.

You could mix in the user ID (if any) in the seed, e.g. by multiplying the post ID with the user ID, to increase the chances of giving each registered user their "own" static page order that varies across posts. In the end, it really depends on what you want, and how far you are willing to go with it.

Code example (seeded with post ID):

function shuffle_pages( array $pages, WP_Post $post ): array {
    mt_srand( $post->ID );
    $order = array_map( function ( $val ) { return mt_rand(); }, range( 1, count( $pages ) ) );
    array_multisort( $order, $pages );
    return $pages;
}
add_filter( 'content_pagination', 'shuffle_pages', 10, 2 );
10
  • 1
    Sorry for this late reply, i've test Code example that you suggested, But it didn't work, function.php: i.imgur.com/p3bvhHh.png Please check if i used it properly? or i should I change anything wp-links() ? May 27, 2021 at 14:36
  • 1
    My apologies, I had not tested my code and made two type declaration errors. I have now fixed them and the code runs correctly on my end. May 27, 2021 at 16:59
  • 1
    What version of PHP are you using? May 28, 2021 at 18:46
  • 1
    I sent you a private message, and will report back here once we resolve the issue. May 31, 2021 at 11:36
  • 1
    Problem solved, code was working, but we made some changes to satisfy the use case. May 31, 2021 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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