Skip to content

Instantly share code, notes, and snippets.

@danielbachhuber
Last active September 5, 2024 01:43
Show Gist options
  • Select an option

  • Save danielbachhuber/6691084 to your computer and use it in GitHub Desktop.

Select an option

Save danielbachhuber/6691084 to your computer and use it in GitHub Desktop.

Revisions

  1. danielbachhuber revised this gist Oct 13, 2013. 1 changed file with 13 additions and 2 deletions.
    15 changes: 13 additions & 2 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -12,6 +12,12 @@
    if ( 900 > str_word_count( $content ) )
    return;

    if ( false !== stripos( $content, '<!--nextpage-->' )
    || false !== stripos( $content, '<!--nopage-->' ) )
    return;

    $content = wpautop( $content );

    $content_array = str_split( $content );
    $word_array = str_word_count( $content, 2 );
    $word_count = 0;
    @@ -28,11 +34,16 @@
    }

    // Found a '<p>'
    if ( '<' == $content_array[$i-1] )
    if ( '<' == $content_array[$i-1] ) {
    $k = $i-2;
    }
    // Found a '</p>'
    else
    else if ( '<' == $content_array[$i-2] ) {
    $k = $i+3;
    } else {
    $word_count++;
    continue;
    }

    $k = $k + ( $next_page_count * 15 );
    $next_page_count++;
  2. danielbachhuber revised this gist Sep 24, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -9,14 +9,14 @@

    $content = $query->posts[0]->post_content;

    if ( 800 > str_word_count( $content ) )
    if ( 900 > str_word_count( $content ) )
    return;

    $content_array = str_split( $content );
    $word_array = str_word_count( $content, 2 );
    $word_count = 0;
    $next_page_count = 0;
    while ( count( $word_array ) > 700 ) {
    while ( count( $word_array ) > 900 ) {

    $word_array = array_slice( $word_array, 500 + $word_count, null, true );
    $word_count = 0;
  3. danielbachhuber created this gist Sep 24, 2013.
    45 changes: 45 additions & 0 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    <?php
    /**
    * Auto-paginate after 500 words
    */
    add_action( 'loop_start', function( $query ) {

    if ( ! is_single() || 'post' != get_post_type() || ! $query->is_main_query() )
    return;

    $content = $query->posts[0]->post_content;

    if ( 800 > str_word_count( $content ) )
    return;

    $content_array = str_split( $content );
    $word_array = str_word_count( $content, 2 );
    $word_count = 0;
    $next_page_count = 0;
    while ( count( $word_array ) > 700 ) {

    $word_array = array_slice( $word_array, 500 + $word_count, null, true );
    $word_count = 0;
    foreach( $word_array as $i => $word ) {

    if ( 'p' != $word ) {
    $word_count++;
    continue;
    }

    // Found a '<p>'
    if ( '<' == $content_array[$i-1] )
    $k = $i-2;
    // Found a '</p>'
    else
    $k = $i+3;

    $k = $k + ( $next_page_count * 15 );
    $next_page_count++;

    $content = substr( $content, 0, $k ) . '<!--nextpage-->' . substr( $content, $k );
    break;
    }
    }
    $query->posts[0]->post_content = $content;
    });