I need a method to automatically create pages (using <!--nextpage-->) in posts based on number of words.

For example... A post containing 1200 words should be broken down into 6 pages with page break coming after every 200 words.

I understand that I can do it manually. But this functionality is needed for a site powered by WordPress that would contain archives of imported data in multiple languages.

Tried searching for a plugin that would accomplish something like this. Could not find it.


This is tough to do programmatically because of potential variations in html and how the tags balance. However, if you were to try, here's how I'd suggest doing it.

First of all, WordPress sets up the post pagination in setup_postdata(), which is called at the end of the_post(). That means you need to get those <!--nextpage--> lines in the posts before the end of the_post(). The 'loop_start' action should work for those purposes. It even passes a copy of the current WP_Query object by reference, so you can make changes directly to the queried posts!

Something like this is a start:

add_action( 'loop_start', 'wpse14677_loop_start' );

function wpse14677_loop_start( $query ) {
    foreach( $query->posts as $index => $post ) {
        $words = preg_split( '/ +/', $post->post_content, PREG_SPLIT_NO_EMPTY );
        $pages = array();
        while ( $words ) {
            $word_count = count ( $words );
            if ( 200 >= $word_count ) {
                $pages[] = implode( ' ', $words );
                $words = array();
            } else {
                $pages[] = implode( ' ', array_slice( $words, 0, 200 ) );
                $words = array_slice( $words, 200 );
        $page_count = count( $pages );
        if( 1 < $page_count ) {
            $query->posts[ $index ]->post_content = implode( "\n<!--nextpage-->\n", $pages );

Hopefully that gives you an idea of what you would need to do. I'd suggest finding some way of temporarily stripping html tags and replacing them after inserting the nextpage flags, because the function above will also count spaces inside HTML tags, and could even put a page break inside one.

  • nice! +1. The only thing i'd change would be to set the count($words) before the if statement (faster). – kaiser Apr 13 '11 at 18:49
  • total novice here guys. can you tell me where do i place this code? in functions.php file in the template directory? – OperaManiac Apr 16 '11 at 8:31
  • 1
    Functions.php should do just right, but as I said, this right now will cause some very eratic behavior if there's a lot of html in your posts. I'll update the answer if I think of a good way to fix that problem. – John P Bloch Apr 16 '11 at 13:40
  • @kaiser wrt to your comment above, can you please add an answer or edit the existing one? (whichever is appropriate) It would be very helpful. :) – its_me Jul 6 '12 at 2:54

Thanks for saving my time. While migrating non wordpress site in to wordpress i was trying to replace PAGEBREAK in to wordpress I have done this with following syntax.

function make_pagebreak_nextpaglink( $query  ) {
   foreach( $query->posts as $index => $post ) {
      $content = str_replace('__PAGEBREAK__', '<!--nextpage-->',   $post->post_content);
      $query->posts[ $index ]->post_content = $content;
add_action('loop_start', 'make_pagebreak_nextpaglink');

Studio Hyperset's Page-Links Plus "auto pagination" module can help you accomplish this.

It allows WordPress users to trade tedious in-line tags for a site-wide management tool that paginates posts and pages quickly and uniformly.

The module automatically prevents widows, orphans, trailing headers, and avoids splitting sentences and individual words and can be configured to override or ignore existing in-line tags.

Check it out here: http://pagelinksplus.com/wordpress-pagination-modules/wordpress-pagination-auto-pagination

  • Welcome to WPSE Quimby! Please ensure you disclose any affiliation with plug-ins you refer to. Also, please read this - some might consider this post bordering between advert and answer :). – Stephen Harris Sep 24 '12 at 10:27

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.